In this tutorial, we will discuss a simple concept of Cpp Program to calculate average of an array
In this topic, we will learn about how to calculate the average of n number of elements (integer or floating point)using the array in C++ programming language.
Average calculates using the operator in Cpp programming language
In this program, we can see step by step procedure for completion of the program.
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[5]={45,34,65,43,23}; //Array declaration and inilization int total=0,i;//variable declaration float avg=0; for(i=0; i<5; i++){//loop for calculating total total=total+arr[i];//calculate total } avg=total/i;//calculate average cout<<"The average is: "<<avg;//Display result on the screen getch(); return 0; }
When the above code is compiled and executed, it produces the following results
The average is: 42
Program 2
In this program, we can see what should be step by step procedure for completion of the program.
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[20];//Array declaration int num,i, sum=0;//Variable declaration double avg=0; cout<<"Enter the numbers of array elements: "; cin>>num;//get input from user for Array size for(i=0; i<num; i++){ cout<<"Enter the numbers: "<<i+1<<" :"; cin>>arr[i];//get input from user for Array elements } for(i=0; i<num; i++){//loop for calculate sum and average sum=sum+arr[i];//calculate sum avg=sum/num;//calculate average } cout<<"Average of entered numbers are: "<<avg; getch();//Display result on the screen return 0; }
When the above code is compiled and executed, it produces the following results
Enter the numbers of array elements: 5 Enter the number 1: 34 Enter the number 2: 43 Enter the number 3: 65 Enter the number 4: 45 Enter the number 5: 76 Average of entered numbers are: 52
Program 3
In this program, we can see what should be step by step procedure for completion of the program.
#include <iostream> #include <conio.h> using namespace std; int main() { float arr[5]={56.45,12.4,65.99,45.6,12.5}; //Array declaration and initialization float total=0.0;//variable declaration float avg=0.0; int i; for(i=0; i<=4; i++){//loop for calculating total total=total+arr[i];//Calculating total } avg=total/5;//calculating average cout<<"The average is: "<<avg;//display result on the screen getch(); return 0; }
When the above code is compiled and executed, it produces the following results
The average is: 38.588
Program 4
In this program, we can see what should be step by step procedure for completion of the program.
#include <iostream> #include <conio.h> using namespace std; int main() { float arr[20];//Array declaration int i,num;//variable declaration double avg=0.0,sum=0.0; cout<<"Enter the number for calculating the average: "; cin>>num;//get input from user for array size for(i=0; i<num; i++){ cout<<"Enter the numbers: "<<i+1<<": "; cin>>arr[i];//get input from user for array elements } for(i=0; i<num; i++){ sum=sum+arr[i];//loop for calculating sum avg=sum/num;//calculate average } cout<<"Average of entered numbers are: "<<avg; getch();//display result on the screen return 0; }
When the above code is compiled and executed, it produces the following results
Enter the number for calculating the average: 6 Enter the number 1: 12.23 Enter the number 1: 23.34 Enter the number 1: 34.45 Enter the number 1: 45.56 Enter the number 1: 67.78 Average of entered numbers are: 40.005
Explanation
This program gets input number of elements in the array and store in the variable num.
Then the for loop takes the elements from the user one by one and stores in the array.
Then for loop calculates the sum of elements using entered elements
Finally, the average calculated by the dividing sum of values by the number of elements
Similar post
Java Program to calculate average of array
C Program to calculate average of array
Java Program to calculate average of odd and even numbers in an array
Java Program to calculate average of odd and even numbers
C Program to calculate average of odd and even numbers in an array
C Program to calculate average of odd and even numbers
C++ Program to calculate average of odd and even numbers in an array
C++ Program to calculate average of odd and even numbers
Suggested for you
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…