C++ Program to Calculate Average of Numbers Using Arrays

C++ Program to Calculate Average of Numbers Using Arrays
Average of numbers is calculated by adding all the numbers and then dividing the sum by count of numbers available.

This program calculates the average of marks if the data are from 1 to 100. If user enters value of n above 100 or below 1 then, while loop is executed which asks user to enter value of n until it is between 1 and 100. When the numbers are entered by the user, subsequently the sum is calculated and stored in the variable sum. After storing all the numbers, average is calculated and displayed.

Example: Look at these numbers:

3, 7, 5, 13, 20, 23, 39, 23, 40, 23, 14, 12, 56, 23, 29
The sum of these numbers is 330
There are fifteen numbers.
The mean is equal to 330 / 15 = 22
The mean of the above numbers is 22

C++ Program to Calculate Average of Numbers Using Arrays

For loop is started from 0 to n-1. This loop adds all the elements of the array. The code snippet demonstrating this is as follows.
for(i = 0; i < n; i++)
sum += num[i];
The average of the numbers is obtained by dividing the sum by n i.e amount of numbers. This is shown below
average= sum / n; 
Finally the average is displayed.


Output

Enter the numbers of data: 5
1. Enter number: 125
Error! number should in range of (1 to 100).
Enter the number again: 100.0
2. Enter number: 75.0
3. Enter number: 65.0
4. Enter number: 65.36
5. Enter number: 75.98
Average = 76.268

Post a Comment

Thank you for vising

أحدث أقدم