





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Deitel & Deitel – C++ How to Program chapter 1
o Sample Program o Examples of Expressions o Use of Operators
umbers. 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
o Tips
Calculate the average age of a class of ten 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 ten students from the user. To store these ages we will use ten variables, one variable 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 n
pr tual result then we should use real numbers (float or double) in our program.
So on the screen the sentence “Please enter the age of first student:” will appear. to enter some information we need to be very clear i.e. write such sentences that are self explanatory and user understands them
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. e user understands precisely that the age would be entered in whole number only.
entered by the user into a ariable , we use the statement:
cin >> age1;
rom 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. ns that data comes from user and is assigned to the variable ere age1 is a variable used for storing the age entered for student1. Similarly get the ages of all the ten students and store them into respective variables. That and so on up rogram stops rom the user. So when cin >> age1; is executed, e program expects from the user to type the age of the student1. After entering the g 'enter key' conveys to the program ns the input value to the variable n the e which is age1 in this case. As we have seen earlier that in an iable on left hand side of the and on right hand side we can have an expression that evaluates e left hand side of assignment perato ror i.e. x = 2 + 4; is a correct statement but x + y = 3+ 5; is an on the left hand side. Similarly e can expression after the >> sign with cin. So we can have one and rect statement and cin >> x + y; an in ent. the variable TotalAge. We use for this purpose. On the right hand side of the assignment d store the result in the variable, side. For this purpose we write the statement as follow:
oduce integer result only and the decimal portion is truncated. If we need the ac
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 : ” ;
Whenever we are requesting user
thoroughly and correctly. Now with the above sentence everyone can understand that age would
or 12 years and 3 months etc. We can refine our sentence such, that th
After this we allow the user to enter the age. To, get the age v
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 f
In our statement it mea age1 , wh we means the age of first student in age1 , the age of second student in age to 10 students. When cin statement is reached in a program, the p execution and expects some input f th age, the user has to press the 'enter key'. Pressin that user has finished entering the input and cin assig o right hand sid assignment statement, we can have only one var assignment operator to a single value. If we have an expression on th o r we get an er incorrect statement as we can not have an expression w not have an only one variable after >> sign i.e. cin >> x; is a cor is correct statem Next, we add all these values and store the result to assignment operator operator, we write the expression to add the ages an TotalAge on left hand
docsity.com
cout << “Please enter the age of student 9: ”; cin >> age9;
cin >> age10;
lculate the total age and average age TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 + age9 +
AverageAge = TotalAge / 10;
cout << “Average age of class is: “ << AverageAge;
ample output of the above program is given below.
the above output the total age of the students is 123 and the actual average should
Examples of Expressions
e have already seen the precedence of arithmetic operators. We have expressions
atements. Let’s discuss some more examples to get a better
ratic equation in algebra, that is y = ax^2 + bx + c. The uation in C will be written as y = a * x * x + b * x + c. In C, it is not an equation but an assignment statement. We can use parentheses in this statement, this
cout << “Please enter the age of student 10: ”;
// ca
age10;
// Display the result ( average age )
}
A s
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 Please enter the age of student 6: 15 Please enter the age of student 7: 12 Please enter the age of student 8: 13 Please enter the age of student 9: 14 Please enter the age of student 10: 11 Average age of class is: 12
In 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 different calculations in algebraic form, and in our programs we write them in the form of C st understanding.
We know about the quad quadratic eq
docsity.com
ote that we have no power operator in C, just use * to multiply the same value.
er expression in algebra: x = ax + by + cz^2. In C the above expression s:
x = a * x + b * y + c * z * z
or will be evaluated before the + operator. We can rewrite the above ent with the use of parentheses. The same expressions can be written as:
ult 12 is added to 4 which gives the
gramming.
will make the expression statement easy to read and understand. Thus we can rewrite it as y = a * (x * x) + (b * y) + c. N
Here is anoth will be a
The * operat statem
x = (a * x) + (b * y) + c * ( z * z)
Lets have an other expression in algebra as x = a(x + b(y + cz^2 )). The parentheses in this equation force the order of evaluation. This expression will be written in C as:
x = a * (x + b * (y + c * z * z))
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. In algebra, there may be curly brackets { } and square brackets [ ] in an expression but in C we have only parentheses ( ). Using parentheses, we can make a complex expression easy to read and understand and can force the order of evaluation. We have to be very careful while using parentheses, as 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 res 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.
Similarly the equation (b 2 – 4ac)/2a can be written as ( b * b – 4 * a * c) / ( 2 * a ). The same statement without using parentheses will be as b * b – 4 * a * c / 2 * a. This is wrong as it evaluates to b^2 – 4ac/2a (i.e. 4ac is divided by 2a instead of (b 2 -4ac) ).
Use of Operators
Here are sample programs which will further explain the use of operators in pro
docsity.com
ow let’s write the program in C by following the analysis we have made. The omplete C program for the above problem is given below. It is easy to understand as e are already familiar with the statements used in it.
separately. */ include main()
cout << digit << ", ";
igit and display it
< ", ";
ber % 10;
sample output of the above program is given below.
c w
/* A program that takes a four digits integer from user and shows the digits on the screen separately i.e. if user enters 7531, it displays 1,3,5,
{ // declare variables int number, digit;
// prompt the user for input cout << "Please enter 4-digit number:"; cin >> number;
// get the first digit and display it on screen digit = number % 10; cout << "The digits are: ";
// get the remaining three digits number number = number / 10;
// get the next d digit = number % 10; cout << digit <
// get the remaining two digits number number = number / 10;
// get the next digit and display it digit = num cout << digit << ", ";
// get the remainingone digit number number = number / 10;
// get the next digit and display it digit = number % cout << digit; }
Please enter 4-digit number: 5678 The digits are: 8, 7, 6, 5
docsity.com
rite a program that takes radius of a circle from the user and calculates the diameter, ircumference and area of the circle and display the result.
problem we take the input (radius of a circle) from the user. For that we can se cin statement to prompt the user to enter the radius of a circle. We store this radius to store diameter, circumference and area riables of type float ,
in our problem the area or circumference of the circle can be in decimal ulae to find the diameter, cum en display these results on the screen. The
/* Following program takes the radius of a circle from the user and calculates the diameter, circumference and area of the circle and displays the result. */ #include main () { // declare variables float radius, diameter, circumference, area;
// prompt the user for radius of a circle cout << "Please enter the radius of the circle " ; cin >> radius ;
// calculate the diameter, circumference and area of the circle // implementing formula i.e. diameter = 2 r circumference = 2 r and area = r 2 diameter = radius * 2 ; circumference = 2 * 3.14 * radius ; // 3.14 is the value of (Pi) area = 3.14 * radius * radius ;
// display the results cout << "The diameter of the circle is : " << diameter ; cout << "The circumference of the circle is : " << circumference ; cout << "The area of the circle is : " << area ; }
A sample output of the above program is given below.
c
In this u in a variable. We also need other variables of the circle. To obtain the correct result, we declare these va instead of int data type, as we know that the int data type stores the whole numbers only. Here values. After getting the radius we use the form cir ference and area of the circle and th solution of this program in coding form is given below.
© Copyright Virtual University of Pakistan
Please enter the radius of the circle 5 The diameter of the circle is : 10
Th i f f th i l i 31 4
The area of the circle is : 78.
docsity.com