 # C++ Programming Code Examples

## C++ > Arrays and Matrices Code Examples

### Program to Calculate Average of Numbers Using Arrays

``` Program to Calculate Average of Numbers Using Arrays This program takes n number of element from user (where, n is specified by user), stores data in an array and calculates the average of those numbers. This program calculates the average if the number of data are from 1 to 100. If user enters value of n above 100 or below 100 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. #include <iostream> using namespace std; int main() { int n, j; float num, sum=0.0, average; cout << "Enter the numbers of data: "; cin >> n; while (n > 100 || n <= 0) { cout << "Error! number should in range of (1 to 100)." << endl; cout << "Enter the number again: "; cin >> n; } for(j = 0; j < n; ++j) { cout << j + 1 << ". Enter number: "; cin >> num[j]; sum += num[j]; } average = sum / n; cout << "Average = " << average; return 0; } ``` 