
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Average of an Array: Iterative and Recursive in C++
Given an array of N integers arr[N], the task is to find the average of arr[N]. To achieve the result we can either use iterative approach or the recursive approach. We will be showing both in the given solution.
Average of an array will be sum of all the elements of an array divided by the number of elements.
Iterative method
In iterative approach, we use loops like for-loop, while-loop or do-while loop which executes the statements till the condition holds true which means 1.
Let’s take an example and then discuss how it can be obtained using iterative approach.
Input
arr[] = {1, 2, 4, 5, 8}
Output
4
Explanation
sum of all the elements =1+2+4+5+8 = 20, total number of elements = 5 Average will be = 20/5 = 4
Input
arr[] = {10, 20, 30, 40}
Output
25
Approach used below is as follows to solve the problem (Iterative)
Iterate 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 with total number of elements and return the average.
Algorithm
Start Step 1→ Declare function to calculate average using iterative method double avg(int arr[], int size) declare int sum = 0 Loop For int i=0 and i<size and i++ Set sum += arr[i] End return sum/size Step 2→ In main() Declare int arr[] = {2,3,1,6,8,10} Declare int size = sizeof(arr)/sizeof(arr[0]) Call avg(arr, size) Stop
Example
#include <iostream> using namespace std; //calculate average using an iterative method double avg(int arr[], int size){ int sum = 0; for (int i=0; i<size; i++) sum += arr[i]; return sum/size; } int main(){ int arr[] = {2,3,1,6,8,10}; int size = sizeof(arr)/sizeof(arr[0]); cout<<"average of an array using iterative method : "&l<t;avg(arr, size) << endl; return 0; }
Output
If run the above code it will generate the following output −
average of an array using iterative method : 5
Recursive method
So, what is a Recursive approach? In Recursive approach we recursively call a function again and again until we get the desirable result. In recursive approach the values returned by function is stored in stack memory.
Let’s take an example and then discuss how it can be obtained using iterative approach.
Input
arr[] = {2, 4, 6, 8}
Output
5
Explanation
sum of all the elements =2+4+6+8 = 20, total number of elements = 4 Average will be = 20/4 = 5
Input
arr[] = {12, 23, 45, 15}
Output
19
Approach used below is as follows to solve the problem (Recursive
We 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.
Algorithm
Start Step 1→ Declare function to calculate average using recursive method double recursive(int arr[],int i, int size) IF (i == size-1) return arr[i] End IF (i == 0) return ((arr[i] + recursive(arr, i+1, size))/size) End return (arr[i] + recursive(arr, i+1, size)) Step 2→ double avg(int arr[], int size) return recursive(arr, 0 , size) Step 3→ In main() Declare int arr[] = {1,5,3,2,6,7} Declare int size = sizeof(arr)/sizeof(arr[0]) Call average(arr, size) Stop
Example
#include <iostream> using namespace std; //function for calculating average recusively double recursive(int arr[],int i, int size){ if (i == size-1) return arr[i]; if (i == 0) return ((arr[i] + recursive(arr, i+1, size))/size); return (arr[i] + recursive(arr, i+1, size)); } //function for returning average double average(int arr[], int size){ return recursive(arr, 0 , size); } int main(){ int arr[] = {1,5,3,2,6,7}; int size = sizeof(arr)/sizeof(arr[0]); cout<<average of an array using a recursive approach is : "<<average(arr, size) << endl; return 0; }
Output
If run the above code it will generate the following output −
average of an array using a recursive approach is : 4