**Problem statement:**

Calculate the average age of a class of ten students. Prompt the user to enter the age of each student.

**Solution:**

Lets first sort out the problem. In the problem we will take the ages of ten students from the user. To store these ages we will use ten variables, one for each student’s age. We will take the ages of students in whole numbers (in years only, like 10, 12, 15 etc), so we will use the variables of data type int. The variables declaration statement in our program will be as follow:

int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10;

We have declared all the ten variables in a single line by using comma separator (,). This is a short method to declare a number of variables of the same data type.

After this we will add all the ages to get the total age and store this total age in a variable. Then we will get the average age of the ten students by dividing this total age by 10. For the storage of total and average ages we need variables. For this purpose we use variable TotalAge for the total of ages and AverageAge for average of ages respectively.

int TotalAge, AverageAge;

We have declared AverageAge as int data type so it can store only whole numbers. The average age of the class can be in real numbers with decimal point (for example if total age is 173 then average age will be 17.3). But the division of integers will produce integer result only and the decimal portion is truncated. If we need the actual result then we should use real numbers (float or double) in our program.

Now we have declared variables for storing different values. In the next step we prompt the user to enter the age of first student. We simply show a text line on the screen by using the statement:

cout << “Please enter the age of first student : “; So on the screen the sentence “Please enter the age of first student:” will appear. Whenever we are requesting user to enter some information we need to be very clear i.e. write such sentences that are self explanatory and user understands them thoroughly and correctly. Now with the above sentence everyone can understand that age would be entered for the first student. As we are expecting only whole numbers i.e. age in years only i.e. 10, 12 etc, our program is not to expect ages as 13.5 or 12.3 or 12 years and 3 months etc. We can refine our sentence such, that the user understands precisely that the age would be entered in whole number only. After this we allow the user to enter the age. To, get the age, entered by the user into a variable, we use the statement: cin >> age1;

Lets have a look on the statement cin >> age1; cin is the counter part of the cout. Here cin is the input stream that gets data from the user and assigns it to the variable on its right side. We know that the sign >> indicates the direction of the flow of data. In our statement it means that data comes from user and is assigned to the variable age1, where age1 is a variable used for storing the age entered for student1. Similarly we get the ages of all the ten students and store them into respective variables. That means the age of first student in age1, the age of second student in age2 and so on up to 10 students. When cin statement is reached in a program, the program stops execution and expects some input from the user. So when cin >> age1; is executed, the program expects from the user to type the age of the student1. After entering the age, the user has to press the 'enter key'. Pressing 'enter key' conveys to the program that user has finished entering the input and cin assigns the input value to the variable on the right hand side which is age1 in this case. As we have seen earlier that in an assignment statement, we can have only one variable on left hand side of the assignment operator and on right hand side we can have an expression that evaluates to a single value. If we have an expression on the left hand side of assignment operator we get an error i.e. x = 2 + 4; is a correct statement but x + y = 3+ 5; is an incorrect statement as we can not have an expression on the left hand side. Similarly we can not have an expression after the >> sign with cin. So we can have one and only one variable after >> sign i.e cin >> x; is a correct statement and cin >> x + y; is an incorrect statement.

Next, we add all these values and store the result to the variable TotalAge. We use assignment operator for this purpose. On the right hand side of the assignment operator, we write the expression to add the ages and store the result in the variable, TotalAge on left hand side. For this purpose we write the statement as follow:

TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 + age9 + age10 ;

The expression on the right hand side uses many addition operators ( + ). As these operators have the same precedence, the expression is evaluated from left to right. Thus first age1 is added to age2 and then the result of this is added to age3 and then this result is added to age4 and so on.

Now we divide this TotalAge by 10 and get the average age. We store this average age in the variable i.e. AverageAge by writing the statement:

AverageAge = TotalAge / 10;

And at the end we display this average age on the screen by using the following statement:

cout << “ The average age of the students is : “ << AverageAge; Here the string enclosed in the quotation marks, will be printed on the screen as it is and the value of AverageAge will be printed on the screen. The complete coding of the program is given below.

A snapshot of the execution of the above program:

In the above output the total age of the students is 123 and the actual average should be 12.3 but as we are using integer data types so the decimal part is truncated and the whole number 12 is assigned to the variable AverageAge.

For previous lesson click here: Precedence of Operators

For next lesson click here: Examples of Expressions

the easiest way to learn programming

introduction to programming

Sample Program

## 0 comments:

## Post a Comment