Given an array, the task is to find average of that array. Average is the sum of array elements divided by the number of elements.
Examples :
Input : arr[] = {1, 2, 3, 4, 5}
Output : 3
Sum of the elements is 1+2+3+4+5 = 15
and total number of elements is 5.
So average is 15/5 = 3
Input : arr[] = {5, 3, 6, 7, 5, 3}
Output : 4.83333
Sum of the elements is 5+3+6+7+5+3 = 29
and total number of elements is 6.
So average is 29/6 = 4.83333.
Iterative:
Iterative program is easy. We need to find sum and divide sum by total number of elements.
C++
#include <iostream>
using namespace std;
double average( int a[], int n)
{
int sum = 0;
for ( int i=0; i<n; i++)
sum += a[i];
return ( double )sum/n;
}
int main()
{
int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << average(arr, n) << endl;
return 0;
}
|
Java
class GFG {
static double average( int a[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += a[i];
return ( double )sum / n;
}
public static void main (String[] args)
{
int arr[] = { 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
int n = arr.length;
System.out.println(average(arr, n));
}
}
|
Python3
def average( a , n ):
sum = 0
for i in range (n):
sum + = a[i]
return sum / n;
arr = [ 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
n = len (arr)
print (average(arr, n))
|
C#
using System;
class GFG {
static double average( int []a, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += a[i];
return ( double )sum / n;
}
public static void Main ()
{
int []arr = {10, 2, 3, 4, 5, 6,
7, 8, 9};
int n = arr.Length;
Console.Write(average(arr, n));
}
}
|
PHP
<?php
function average( $a , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $a [ $i ];
return $sum / $n ;
}
$arr = array (10, 2, 3, 4, 5,
6, 7, 8, 9);
$n = count ( $arr );
echo average( $arr , $n );
?>
|
Recursive:
The idea is to pass index of element as an additional parameter and recursively compute sum. After computing sum, divide the sum by n.
C++
#include <iostream>
using namespace std;
double avgRec( int a[], int i, int n)
{
if (i == n-1)
return a[i];
if (i == 0)
return ((a[i] + avgRec(a, i+1, n))/n);
return (a[i] + avgRec(a, i+1, n));
}
double average( int a[], int n)
{
return avgRec(a, 0, n);
}
int main()
{
int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << average(arr, n) << endl;
return 0;
}
|
Java
class CalcAvg
{
static double avgRec( int a[], int i, int n)
{
if (i == n- 1 )
return a[i];
if (i == 0 )
return ((a[i] + avgRec(a, i+ 1 , n))/n);
return (a[i] + avgRec(a, i+ 1 , n));
}
static double average( int a[], int n)
{
return avgRec(a, 0 , n);
}
public static void main (String[] args)
{
int arr[] = { 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
int n = arr.length;
System.out.println(average(arr, n));
}
}
|
Python3
def avgRec( a , i , n ):
if i = = n - 1 :
return a[i]
if i = = 0 :
return ((a[i] + avgRec(a, i + 1 , n)) / n)
return (a[i] + avgRec(a, i + 1 , n))
def average(a, n):
return avgRec(a, 0 , n)
arr = [ 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
n = len (arr)
print (average(arr, n))
|
C#
using System;
class GFG {
static double avgRec( int []a, int i, int n)
{
if (i == n-1)
return a[i];
if (i == 0)
return ((a[i] + avgRec(a, i+1, n))/n);
return (a[i] + avgRec(a, i+1, n));
}
static double average( int []a, int n)
{
return avgRec(a, 0, n);
}
public static void Main ()
{
int []arr = {10, 2, 3, 4, 5, 6, 7, 8, 9};
int n = arr.Length;
Console.Write(average(arr, n));
}
}
|
PHP
<?php
function avgRec( $a , $i , $n )
{
if ( $i == $n - 1)
return $a [ $i ];
if ( $i == 0)
return (( $a [ $i ] +
avgRec( $a , $i +
1, $n )) / $n );
return ( $a [ $i ] +
avgRec( $a , $i + 1, $n ));
}
function average( $a , $n )
{
return avgRec( $a , 0, $n );
}
$arr = array (10, 2, 3, 4,
5, 6, 7, 8, 9);
$n = sizeof( $arr );
echo average( $arr , $n ) ;
?>
|
Related Article :
Average of a stream of numbers
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.