In: Computer Science
Q18.
// This program finds the average time spent programming by a student // each day over a three day period. // PLACE YOUR NAME HERE #include using namespace std; int main() { int numStudents; float numHours, total, average; int student, day = 0; // these are the counters for the loops cout << "This program will find the average number of hours a day" << " that a student spent programming over a long weekend\n\n"; cout << "How many students are there ?" << endl << endl; cin >> numStudents; for (student = 1; student <= numStudents; student++) { total = 0; for (day = 1; day <= 3; day++) { cout << "Please enter the number of hours worked by student " << student << " on day " << day << "." << endl; cin >> numHours; total = total + numHours; } average = total / 3; cout << endl; cout << "The average number of hours per day spent programming by " << "student " << student << " is " << average << endl << endl << endl; } return 0; }
Note that the inner loop of this program is always executed exactly three times—once for each day of the long weekend. Modify the code so that the inner loop iterates n times, where n is a positive integer input by the user. In other words, let the user decide how many days to consider just as they choose how many students to consider.
Sample Run:
This program will find the average number of hours a day that a student spent programming over a long weekend How many students are there? 2 Enter the number of days in the long weekend 2 Please enter the number of hours worked by student 1 on day 1 4 Please enter the number of hours worked by student 1 on day 2 6 The average number of hours per day spent programming by student 1 is 5 Please enter the number of hours worked by student 2 on day 1 9 Please enter the number of hours worked by student 2 on day 2 13 The average number of hours per day spent programming by student 2 is 11
Q19. Modify the program from Q18 so that it also finds the average number of hours per day that a given student studies biology as well as programming. For each given student include two prompts, one for each subject. Have the program print out which subject the student, on average, spent the most time on.
Program:
#include <iostream>
using namespace std;
int main()
{
int numStudents;
float numHours, totalp,totalb, averagep,averageb;
int student, day = 0; // these are the counters for the loops
cout << "This program will find the average number of
hours a day"
<< " that a student spent on biology and programming over a
long weekend and prints the subject on which more time was
spent\n\n";
cout << "How many students are there ?" << endl
<< endl;
cin >> numStudents;
for (student = 1; student <= numStudents; student++)
{
totalp = 0;
totalb=0;
for (day = 1; day <= 3; day++)
{
cout << "Please enter the number of hours worked by student
"
<< student << " in programming on day " << day
<< "." << endl;
cin >> numHours;
totalp = totalp + numHours;
cout << "Please enter the number of hours worked by student
"
<< student << " in biology on day " << day
<< "." << endl;//read input for biology as well
cin >> numHours;
totalb = totalb + numHours;
}
averagep = totalp / 3;
averageb = totalb / 3;//calculate average for biology
cout << endl;
cout << "The average number of hours per day spent studying
programming by "
<< "student " << student << " is " <<
averagep
<< endl << endl << endl;
cout << "The average number of hours per day spent studying
biology by "
<< "student " << student << " is " <<
averageb
<< endl << endl << endl;
if(averagep>averageb)//check for greater value and print message
on console
cout<<"The student spent most time on average on programming
subject"<<endl;
else
cout<<"The student spent most time on average on biology
subject"<<endl;
}
return 0;
}
Expected Output:
This program will find the average number of hours a day that a student spent on biology and programming over a long weekend and prints the subject on which more time was spent
How many students are there ?
1
Please enter the number of hours worked by student 1 in programming
on day 1.
3
Please enter the number of hours worked by student 1 in biology on
day 1.
4
Please enter the number of hours worked by student 1 in programming
on day 2.
3
Please enter the number of hours worked by student 1 in biology on
day 2.
4
Please enter the number of hours worked by student 1 in programming
on day 3.
3
Please enter the number of hours worked by student 1 in biology on
day 3.
4
The average number of hours per day spent studying programming by student 1 is 3
The average number of hours per day spent studying biology by
student 1 is 4
The student spent most time on average on biology subject
Screenshots:
The screenshots are attached below for reference.
Please follow them for output.
Please upvote my answer. Thank you.