The program will compute statistics about the type of drink people prefer. The p
ID: 3558378 • Letter: T
Question
The program will compute statistics about the type of drink people prefer. The program will read in a survey group's id number and the responses of people within that group to which type of drink they prefer. The program will compute various things, and it will print everything out. Then the program will repeat the process for a new survey group, over and over, for the entire set of data. At the end, the program will print the number of survey groups processed.
Here are the details:
1. The program will read in the id number of a survey group (see step 6 below). The program will read in the number of people within that survey group who prefer coffee, the number of people who prefer tea, and the number of people who have no preference. For example, the program could read in (and then print everything read in) the following four pieces of data:
1234 7 5 5 [ for survey group 1234, 7 prefer coffee, 5 prefer tea, 5 no preference]
2. The program will compute (and print) the total number of people interviewed in this survey group, which is simply the number who prefer coffee plus the number who prefer tea plus the number who have no preference.
If the total number of people interviewed in the group is 25 or above, the program will print a message saying this survey group is complete. If the total is less than 25, the program will print a message saying how many more people need to be interviewed to reach 25.
For example, for the group shown above, the total number of people surveyed is 17 (why?); there are 8 more needed to reach 25 (the calculation should be 25
Explanation / Answer
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int group_id;
float coffee, tea, no, total;
float drink_score;
while(cin >> group_id >> coffee >> tea >> no){
total = coffee + tea + no;
cout << "survey group " << group_id << " results: " << coffee << " prefers coffee "
<< tea << " prefers tea " << no << " have no preference " << endl;
cout << "total number of people is " << total << endl;
if(total >= 25){
cout << "This survey group is complete" << endl;
}
else{
cout << (25-total) << " people remaining to reach 25 in the survey group" << endl;
}
cout << setprecision(4) << "coffee ratio is " << coffee/total << " tea ratio is " << tea/total << " no preference ratio is " << no/total << endl;
if(no > tea){
cout << "number who have no preference is greater than who prefer tea" << endl;
}
else{
cout << "number who have no preference is not greater than who prefer tea" << endl;
}
if(no >= coffee){
cout << "number who have no preference is greater than or equal to who prefer coffee" << endl;
}
else{
cout << "number who have no preference is not greater than or equal to who prefer coffee" << endl;
}
drink_score = tea + coffee - 3*no;
if(drink_score < 0) drink_score = 0;
cout << "final drink score is " << drink_score << endl << endl << endl;
}
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------
Input:
1 26 0 0
2 0 3 0
3 0 0 7
4 10 5 10
5 5 5 5
6 7 5 5
7 20 25 26
8 11 0 15
9 10 10 5
---------------------------------------------------------------------------------------------------------------------------------------------------
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.