Before coming to the lab you should read through pages 83-94 in our text, then answer the following questions in writing:
In the first example that was introduced in lab2, you computed the average of three integers. In that example, you initially declared the three numbers as: int x, y, z; then asked users to input the value for each one of them, and at last, you computed the average of these numbers as: average = (x + y + z)/3; We mentioned in that lab that it was more accurate to declare these 3 variables as real values (double or float). So we defined them as double x, y, z;
Now, I have some questions for you.
1. What if you wanted to compute the average
of 6 numbers? What changes would you make in that program?
Write the new program.
2. What if you wanted to compute the average
of many numbers (100, 1000, or even more numbers)? Would you declare 100,
1000, or even more variables, read their values, then compute their average?
Do you see anything wrong with this method? Explain your answer briefly.
3. What if different users wanted to compute
the average of different number of grades? (assuming the user provides
the number of grades for which he/she wants to compute the average and
If we look into these questions more carefully,
we will realize that we need to repeat some calculations more than once.
Thus, we should be able to somehow make the program to repeat the steps
without duplicating the code many times. The looping is what
we use to make the computation simpler in such cases.