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 );
?>
|
Javascript
<script>
function average(a, n)
{
var sum = 0;
for ( var i = 0; i < n; i++) sum += a[i];
return parseFloat(sum / n);
}
var arr = [10, 2, 3, 4, 5, 6, 7, 8, 9];
var n = arr.length;
document.write(average(arr, n));
document.write( "<br>" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
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 ) ;
?>
|
Javascript
<script>
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);
}
let arr = [ 10, 2, 3, 4, 5, 6, 7, 8, 9 ];
let n = arr.length;
document.write(average(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Related Article :
Average of a stream of numbers
This article is contributed by Sahil Rajput. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!