In: Computer Science
Create the flowchart and a C++ .cpp program that calculates the average snowfall for a week. The program should use a loop to execute 7 times asking the user to input the snowfall for each day of the week in inches. The program should calculate the total and average snowfall for the week. The program should then display the result and then ask the user if they wish to repeat the program or close the program.
Here is the flow chart for the above requirements
Here is the c++ code with comments to understand everything
//These are the required header file
#include <iostream>
using namespace std;
int main()
{
//first make an array of 7 elements to store the week snowfall measurement
float arr[7];
//sum and avg will hold the sum and average
float sum, avg;
//Initially we keep the choice as 'y' so that we can enter the loop
char choice = 'y';
while (choice == 'y')
{
sum = 0;
cout << "Enter the snowfall for each day of the week in inches\n";
//Input the measurements using a loop
for (int i = 0; i < 7; i++)
{
cin >> arr[i];
//At the same time update the sum also
sum = sum + arr[i];
}
//Avg will be be sum/7
avg = sum / 7;
cout << "The sum for all days is: " << sum;
cout << "\nThe average is: " << avg;
//Now ask if the user want to input the data again, if user types 'y' then the loop continues
cout << "\nDo you want to repeate the test(y/n): ";
cin.ignore();
cin >> choice;
}
return 0;
}
Here is the output: