const int DAYS_IN_WEEK = 7; for(counter = 1; counter <= DAYS_IN_WEEK; counter++)
ID: 3688620 • Letter: C
Question
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
i have attached the rainfall.cpp file
Explanation / Answer
1.
Garbage value will come in the variable sum
2.
yes, we can replace sum += rainfall; with sum = sum + rainfall
3.
double
4.
No, we can't do that, because the final value of counter will be 8, not 7(which is why the loop ends)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.