HowToProgramC : Lesson 4

Back Home Next

A Sample Program:

Let's try to implement what we have learned so far:

Problem Statement:

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


Lets first sort out the problem. In the problem we will take the ages of five students from the user. To store these ages we will use five variables, one variable for each student’s age. As the age is stored in whole numbers 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;
We have declared all the five 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 five students by dividing this total age by 5. 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 88 then average age will be 17.6). 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.

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 students and store them into respective variables. When cin statement is reached in a program, the program stops execution and expects some input from the user. 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.

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 ;
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:
/* This program calculates the average age of a class of five students after prompting the user to enter the age of each student. */
#include <iostream.h>
main ()
// declaration of variables, the age will be in whole numbers
int age1, age2, age3, age4, age5;
int TotalAge, AverageAge;
// take ages of the students from the user
cout << “Please enter the age of student 1: ”;
cin >> age1;
cout << “Please enter the age of student 2: ”;
cin >> age2;
cout << “Please enter the age of student 3: ”;
cin >> age3;
cout << “Please enter the age of student 4: ”;
cin >> age4;
cout << “Please enter the age of student 5: ”;
cin >> age5;
// calculate the total age and average age
TotalAge = age1 + age2 + age3 + age4 + age5;
AverageAge = TotalAge / 5;
// Display the result ( average age )
cout << “Average age of class is: “ << AverageAge;
A sample output of the above program is given below.
Please enter the age of student 1: 12
Please enter the age of student 2: 13
Please enter the age of student 3: 11
Please enter the age of student 4: 14
Please enter the age of student 5: 13
Average age of class is: 12

In the above output the total age of the students is 63 and the actual average should be 12.6 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.

Examples of Expressions

We have already seen the precedence of arithmetic operators. We have expressions for different calculations in algebraic form, and in our programs we write them in the form of C statements. Let’s discuss some more examples to get a better understanding.

We have no power operator in C, just use * to multiply the same value.

While writing expressions in C we should keep in mind the precedence of the operators and the order of evaluation of the expressions (expressions are evaluated from left to right). Parentheses are used in complicated expressions. Parentheses at wrong place can cause an incorrect result. For example, a statement x = 2 + 4 * 3 results x = 14. As * operator is of higher precedence, 4 * 3 is evaluated first and then result 12 is added to 4 which gives the result 14. We can rewrite this statement, with the use of parentheses to show it clearly, that multiplication is performed first. Thus we can write it as x = 2 + (4 * 3). But the same statement with different parentheses like x = (2 + 4) * 3 will give the result 18, so we have to be careful while using parenthesis and the evaluation order of the expression.

Back Home Next

© All rights Reserved.