Mean of array using recursion
To find the mean of the elements of the array.
Mean = (Sum of elements of the Array) /
(Total no of elements in Array)
Examples:
Input : 1 2 3 4 5 Output : 3 Input : 1 2 3 Output : 2
To find the mean using recursion assume that the problem is already solved for N-1 ie you have to find for n
Sum of first N-1 elements =
(Mean of N-1 elements)*(N-1)
Mean of N elements = (Sum of first N-1 elements +
N-th elements) / (N)
Note : Since array indexing starts from 0, we access N-th element using A[N-1].

C++
// Recursive C program to find mean of array #include<stdio.h> // Function Definiton of findMean function float findMean(int A[], int N) { if (N == 1) return (float)A[N-1]; else return ((float)(findMean(A, N-1)*(N-1) + A[N-1]) / N); } // Main Calling Function int main() { float Mean = 0; int A[] = {1, 2, 3, 4, 5}; int N = sizeof(A)/sizeof(A[0]); printf("%.2f\n", findMean(A, N)); return 0; } |
chevron_right
filter_none
Java
// Recursive Java program to find mean of array class CalcMean { // Function Definiton of findMean function static float findMean(int A[], int N) { if (N == 1) return (float)A[N-1]; else return ((float)(findMean(A, N-1)*(N-1) + A[N-1]) / N); } // main Function public static void main (String[] args) { float Mean = 0; int A[] = {1, 2, 3, 4, 5}; int N = A.length; System.out.println(findMean(A, N)); } } |
chevron_right
filter_none
Python3
# Recursive Python3 program to # find mean of array # Function Definiton of findMean function def findMean(A, N): if (N == 1): return A[N - 1] else: return ((findMean(A, N - 1) * (N - 1) + A[N - 1]) / N) # Driver Code Mean = 0A = [1, 2, 3, 4, 5] N = len(A) print(findMean(A, N)) # This code is contributed by Anant Agarwal. |
chevron_right
filter_none
C#
// Recursive C# program to find mean of array using System; class CalcMean { // Function Definiton of findMean function static float findMean(int []A, int N) { if (N == 1) return (float)A[N - 1]; else return ((float)(findMean(A, N - 1) * (N - 1) + A[N - 1]) / N); } // Driver code public static void Main() { //float Mean = 0; int []A = {1, 2, 3, 4, 5}; int N = A.Length; Console.WriteLine(findMean(A, N)); } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
PHP
<?php // Recursive PHP program to find mean of array // Function Definiton of findMean function function findMean($A, $N) { if ($N == 1) return $A[$N - 1]; else return ((findMean($A, $N - 1) * ($N - 1) + $A[$N - 1]) / $N); } // Driver Code $Mean = 0; $A = array(1, 2, 3, 4, 5); $N = sizeof($A); echo findMean($A, $N); // This code is contributed by ajit. ?> |
chevron_right
filter_none
Output:
3
This article is contributed by Prakhar Agrawal. 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.
Recommended Posts:
- Sum of even elements of an Array using Recursion
- Sum of array elements using recursion
- Sum of array Elements without using loops and recursion
- Program to check if an array is palindrome or not using Recursion
- Solving f(n)= (1) + (2*3) + (4*5*6) ... n using Recursion
- Product of 2 Numbers using Recursion
- Sum of natural numbers using recursion
- Generating subarrays using recursion
- Decimal to binary number using recursion
- Programs for printing pyramid patterns using recursion
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Print modified array after multiple array range increment operations
Improved By : jit_t



