Currently, the program displays the number of people whose Facebook time exceeds
ID: 3810483 • Letter: C
Question
Currently, the program displays the number of people whose Facebook time exceeds the number of minutes entered by the user. Modify the program to also display the average number of minutes these people spend on Facebook. Display the average with one decimal place. (Hint: Three people spend more than 95 minutes on Facebook. The average for these people is 113.3 minutes.) Save and then run the program. Test the program appropriately.
#include <iostream>
using namespace std;
int main()
{
int pollResults[25]= { 35, 120, 75, 60, 20,
25, 15, 90, 85, 35,
60, 15, 10, 25, 60,
100, 90, 10, 120, 5,
40, 70, 30, 25, 5 };
int minutes = 0;
int numOver = 0;
double average = 0.0;
double total = 0.0;
cout << "Search for minutes over: ";
cin >> minutes;
//search the array
for (int sub = 0; sub < 25; sub += 1)
if (pollResults[sub] > minutes)
numOver += 1;
//end if
//end for
cout << endl << "Number who spend more than " << minutes << " minutes" << endl;
cout << "per day on Facebook: " << numOver << endl;
return 0;
} //end of main function
Explanation / Answer
#include <iostream>
#include<iomanip>//header for setprecision
using namespace std;
int main()
{
int pollResults[25]= { 35, 120, 75, 60, 20,
25, 15, 90, 85, 35,
60, 15, 10, 25, 60,
100, 90, 10, 120, 5,
40, 70, 30, 25, 5 };
int minutes = 0;
int numOver = 0;
double average = 0.0;
double total = 0;
cout << "Search for minutes over: ";
cin >> minutes;
//search the array
for (int sub = 0; sub < 25; sub += 1){
if (pollResults[sub] > minutes){
numOver += 1;
total=total+pollResults[sub]; //get the sum
} //end if
}//end for
cout << endl << "Number who spend more than " << minutes << " minutes" << endl;
cout << "per day on Facebook: " << numOver << endl;
average=total/numOver;//cal average
cout << fixed;
cout <<setprecision(1); //setprecio t 1 decimal
cout<<"The average for these people is "<<average<<" minutes."<<endl;
return 0;
} //end of main function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.