Question: Study the following code, and then answer Questions 1 4. The complete program is saved in the file named Rainfall.cpp . You may want to
Study the following code, and then answer Questions 1 4.
The complete program is saved in the file named Rainfall.cpp .
You may want to compile and execute the program to help you answer the questions.
const int DAYS_IN_WEEK = 7;
for(counter = 1; counter <= DAYS_IN_WEEK; counter++)
{
cout << "Enter rainfall amount for Day " + counter << ": ";
cin >> rainfall; cout << "Day " << counter << "rainfall amount is " <<
rainfall << " inches" << endl;
sum += rainfall;
}
// calculate average
average = sum / DAYS_IN_WEEK;
1. What happens when you compile this program if the variable sum is not initialized with the value 0 ?
2. Could you replace sum += rainfall; with sum = sum + rainfall; ?
3. The variables sum , rainfall , and average should be declared to be what data type to calculate the most accurate average rainfall?
4. Could you replace DAYS_IN_WEEK in the statement average = sum / DAYS_IN_WEEK; with the variable named counter and still get the desired result? Explain.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
