Your local scout troop is selling cookies. The troop leader would like you to wr
ID: 3682747 • Letter: Y
Question
Your local scout troop is selling cookies. The troop leader would like you to write a program that accepts the number of boxes of cookies sold by each scout and displays the following output:
- the total number of boxes sold by the troop
- the total number of scouts selling cookies
- the average number of boxes sold by a scout.
Write a complete C++ program that produces the desired output. Hint: Use a sentinel-controlled while loop.
A sample run of the program is shown.
Bonus: Find and display the highest number of boxes sold by a scout along with the scout's number. Given the input above, your output would look like this: Most boxes of cookies sold was by scout 2 with 50 boxes.
Follow these steps for developing both Part A and Part B of the assignment.
1. Follow the steps used for previous assignments (make sure you understand the problem, identify any constants and input/output variables, write the steps in the algorithm, etc.)
2. When you are ready to type in your program, create a new Visual C++ project using your last name, first initial and pgmexta (or pgmextb) as the project name and C++ file name (e.g., LowJ_pgmextb and LowJ_pgmextb.cpp).
3. Type in your header and inline comments (following guidelines for previous programs) and C++ statements.
4. Run, test and debug your program until it correctly displays the results.
5. When you are satisfied that your program is producing the correct output, turn in ONLY the .cpp file from each project on Canvas.
CAUserslrex DesktoplCS2010 C++ 2013 Projects pgm4b_cookies De Cookie Sales Enter boxes sold by scout 1 o end: se Enter boxes sold by scout 1 (-1 to end): 30 nter boxes sold by scout 2 -1 to end): 50 nter boxes sold by scout 3 -1 to end): 15 nter boxes sold by scout 4 (-1 to end): -1 otal boxes of cookies sold: 95 Number of scouts selling cookies: 3 Average number of boxes sold by a scout: 31.6667 ress any key to continue . . .Explanation / Answer
/*** C++ code to demostrate cookies sale ****/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
cout << " Cookie Sales ";
vector<int> sales;
int boxes;
int sum = 0;
int max = -1;
int i = 0;
int highest;
double Average;
while(true)
{
i++;
cout << "Enter boxes sold by scout " << i << " (-1 to end): ";
cin >> boxes;
if(boxes == -1) break;
else
{
sales.push_back(boxes);
sum = sum + boxes;
if(boxes > max)
{
max = boxes;
highest = i;
}
}
}
Average = (double)sum / (i-1);
cout << "Total boxes of cookies sold: " << sum << endl;
cout << "Number of scouts selling cookies: " << i-1 << endl;
cout << "Average number of boxes sold by a scout: "<< Average <<endl;
cout << "Most boxes of cookies sold was by scout " << highest << " with " << sales[highest-1] << " boxes.";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.