C++ Program For Average of an Array (Iterative and Recursive) Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Given an array, the task is to find the average of that array. Average is the sum of the array elements divided by the number of elements. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Sum 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.83333Sum of the elements is 5+3+6+7+5+3 = 29and total number of elements is 6.So average is 29/6 = 4.83333. 1. Iterative ApproachThe iterative program is easy. We need to find the sum and divide the sum by the total number of elements. ApproachIterate each element of an array using a loop.Sum up each element of the array till we reach the end of the array.Divide the sum by the total number of elements and return the average. Below is the C++ program to find the average of that array using the Iterative approach: C++ // C++ program to calculate average // of array elements #include <iostream> using namespace std; // Function that return average // of an array. double average(int a[], int n) { // Find sum of array element int sum = 0; for (int i = 0; i < n; i++) sum += a[i]; return (double)sum / n; } // Driver code 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; } Output6 The complexity of the method aboveTime Complexity: O(n), The time complexity of the above code is O(n) as it loops through all elements of the array to calculate the sum. Auxiliary Space: O(1), The space complexity is O(1) as no extra space is used. 2. Recursive ApproachThe idea is to pass the index of the element as an additional parameter and recursively compute the sum. After computing the sum, divide the sum by n. ApproachWe will call the function again and again till we reach the end of an array.We will sum every element of the array and when we reach the end of an array, we will return the average of the array.Below is the C++ program to find the average of the array using the Recursive approach: C++ // C++ program to calculate average // of array elements #include <iostream> using namespace std; // Recursively computes average // of a[] double avgRec(int a[], int i, int n) { // Last element if (i == n - 1) return a[i]; // When index is 0, divide sum // computed so far by n. if (i == 0) return ((a[i] + avgRec(a, i + 1, n)) / n); // Compute sum return (a[i] + avgRec(a, i + 1, n)); } // Function that return average // of an array. double average(int a[], int n) { return avgRec(a, 0, n); } // Driver code 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; } Output6 The complexity of the above programTime Complexity: O(n) Auxiliary Space: O(n) Related Article: Average of a stream of numbers Comment More infoAdvertise with us Next Article C++ Program For Average of an Array (Iterative and Recursive) K kartik Follow Improve Article Tags : C++ Programs C++ DSA Arrays Arrays C Array Programs +2 More Practice Tags : CPPArraysArrays Similar Reads Program for average of an array (Iterative and Recursive) 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, 7 min read Program for Variance and Standard Deviation of an array Given an array, we need to calculate the variance and standard deviation of the elements of the array. Examples : Input : arr[] = [1, 2, 3, 4, 5]Output : Variance = 2 Standard Deviation = 1 Input : arr[] = [7, 7, 8, 8, 3]Output : Variance = 3 Standard Deviation = 1We have discussed program to find m 9 min read Generate an Array such that Average of triplet exist in given Array Given an array A[] of length N and your task is to find an array B[] of size N+2, such that Average of (B[i], B[i+1], B[i+2]) equals to A[i] for each i (1 <= i <= N) . Note: First two elements B[] must be zero. Examples:Input: N = 1, A[] = {3} Output: B[] = {0, 0, 9} Explanation: Let us unders 5 min read Count of Unique elements after inserting average of MEX and Array Max K times Given an array A[] of N non-negative integers and an integer K. Each time, you can do the following two operations Find Average of MAX and MEX(rounded up to closest greater integer) of the array.Insert calculated average in the array. After performing the above operation K times, find the count of u 9 min read Maximum average sum partition of an array Given an array, we partition a row of numbers A into at most K adjacent (non-empty) groups, then the score is the sum of the average of each group. What is the maximum score that can be scored? Examples: Input : A = { 9, 1, 2, 3, 9 } K = 3 Output : 20 Explanation : We can partition A into [9], [1, 2 10 min read Like