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) kartik Follow Improve Article Tags : C++ Programs C++ DSA Arrays Arrays C Array Programs +2 More Practice Tags : CPPArraysArrays Similar Reads C++ Program for Mean of range in array Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line. Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4 Output : 2 3 3 Here for 0 to 2 (1 + 2 + 3) / 3 = 2 Input : arr[] = {6, 7, 8, 10} q = 2 0 4 min read C++ Program for Equilibrium index of an array Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist. The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at high 5 min read C++ Program to Find the subarray with least average Given an array arr[] of size n and integer k such that k Examples :Â Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3 Output: Subarray between indexes 3 and 5 The subarray {20, 10, 50} has the least average among all subarrays of size 3. Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2 Output: Subarr 3 min read How to Find the Average of All Elements in a Vector in C++? The average of all elements in vector is defined as the ratio of sum of all elements in vector to the number of elements in vector. In this article, we will learn how to find the average of all elements in vector in C++.The simplest method to find the average of all elements of vector is by using ac 2 min read How to find the sum of elements of an Array using STL in C++? Given an array arr[], find the sum of the elements of this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: 259 Input: arr[] = {1, 7, 5, 4, 6, 12} Output: 35 Approach: Sum can be found with the help of accumulate() function provided in STL. Syntax: accumulate(first_ind 1 min read How to Find the Variance of Numbers in 2D Array in C++? The variance of numbers is a measure of the spread of a set of values which is used to find how much the values in a dataset differ from mean or average. In this article, we will learn how to find the variance of numbers in a 2D array in C++. Example Input: myMatrix = { { 10, 20, 30 }, { 40, 50, 60 3 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read Sum of array Elements without using loops and recursion Given an array of N elements, the task is to find the Sum of N elements without using loops(for, while & doWhile) and recursion.Examples: Input: arr[]={1, 2, 3, 4, 5} Output: 15 Input: arr[]={10, 20, 30} Output: 60 j Approach: Unconditional Jump Statements can be used to solve this problem.Uncon 5 min read How to Find Average of All Elements in a List in C++? In C++, the STL provides a list container that is a doubly-linked list that allows storing the data in non-continuous memory locations. In this article, we will learn how to find the average of all elements in a list in C++. Example: Input:myList = {10, 20, 30, 40, 50};Output: Average of all element 2 min read How to Find Average of All Elements in a Deque in C++? In C++, a deque (double-ended queue) is a container class template that allows fast insertion and deletion at both its beginning and end. In this article, we will learn how to find the average (or mean) of all elements in a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5} Output: mean = 3Find 2 min read Like