The program being used is C++ Visual studios. Please help right the program and
ID: 3558640 • Letter: T
Question
The program being used is C++ Visual studios. Please help right the program and be specific with the syntax language step by step if possible.
Using a Counter, an Accumulator, and an End Sentinel in a Loop
Step 1: Add the partially completed cookies.cpp program (given below) in your Lab6 folder to the project. Below is a copy of the source.
1 // Lab 5 - cookies.cpp
2 // This program finds the average number of boxes of cookies
3 // sold by the children in a particular scout troop.
4 // It illustrates the use of a counter, an accumulator,
5 // and an end sentinel.
6 // PUT YOUR NAME HERE.
7 #include <iostream>
8 using namespace std;
9
10 int main()
11 {
12 int numBoxes, // Number of boxes of cookies sold by one child
13 totalBoxes, // Accumulates total boxes sold by the entire troop
14 numSeller; // Counts the number of children selling cookies
15
16 double averageBoxes; // Average number of boxes sold per child
17
18 // WRITE CODE TO INITIALIZE THE totalBoxes ACCUMLATOR TO 0 AND
19 // THE numSeller COUNTER TO 1.
20
21 cout << " **** Cookie Sales Information **** ";
22
23 // Get the first input
24 cout << "Enter number of boxes of cookies sold by seller " << numSeller
25 << " (or -1 to quit): ";
26 cin >> numBoxes;
27
28 // WRITE CODE TO START A while LOOP THAT LOOPS WHILE numBoxes
29 // IS NOT EQUAL TO -1, THE SENTINEL VALUE.
30 {
31 // WRITE CODE TO ADD numBoxes TO THE totalBoxes ACCUMULATOR.
32 // WRITE CODE TO ADD 1 TO THE numSeller COUNTER.
33
34 // WRITE CODE TO PROMPT FOR AND INPUT THE NUMBER OF BOXES
35 // SOLD BY THE NEXT SELLER.
36 }
37 // WHEN THE LOOP IS EXITED, THE VALUE STORED IN THE numSeller COUNTER
38 // WILL BE ONE MORE THAN THE ACTUAL NUMBER OF SELLERS. SO WRITE CODE
39 // TO ADJUST IT TO THE ACTUAL NUMBER OF SELLERS.
40
41 if (numSeller == 0) // If true, -1 was the very first entry
42 cout << " No boxes were sold. ";
43 else
44 { // WRITE CODE TO ASSIGN averageBoxes THE COMPUTED AVERAGE NUMBER
45 // OF BOXES SOLD PER SELLER.
46 // WRITE CODE TO PRINT OUT THE NUMBER OF SELLERS AND AVERAGE NUMBER
47 // OF BOXES SOLD PER SELLER.
48 }
49
50 return 0;
51 }
Step 2: Read through the code and the instructions in order to understand the steps that must be carried out to correctly count the number of sellers and accumulate the total number of cookie boxes sold. Notice how lines 41-42 handle the special case where -1 is the very first input, indicating there are no sellers and no boxes sold. This must be handled as a special case to avoid a divide by zero when the number of sellers equals zero.
Step 3: Complete the program by following the instructions in the capitalized comments. Then compile it. Once it compiles with no errors, test it using the following test cases. You should get the results shown.
Run Inputs Expected Output
1 -1 No boxes were sold
2 41
33
19
64
42
-1 The average number of boxes sold by the 5 sellers was 39.8.
3 10
-10
24
-1 The average number of boxes sold by the 3 sellers was 8.
Step 4: Notice that runs 1 and 2 produce desirable results, but run 3 does not. This is because the program does not validate the input data. Only non-negative numbers and -1 (to quit) should be allowed. Add while loops in the appropriate places to validate that the input for number of boxes sold is -1 or greater. Then re-test your program with the same three test cases. The results of test cases 1 and 2 should be the same as before. However, now when test case 3 is run, the -10 input should be rejected and the program should generate the following output:
The average number of boxes sold by the 2 sellers was 17.
Explanation / Answer
// Lab 5 - cookies.cpp
// This program finds the average number of boxes of cookies
// sold by the children in a particular scout troop.
// It illustrates the use of a counter, an accumulator,
// and an end sentinel.
// PUT YOUR NAME HERE.
#include <iostream>
using namespace std;
int main()
{
int numBoxes, // Number of boxes of cookies sold by one child
totalBoxes, // Accumulates total boxes sold by the entire troop
numSeller; // Counts the number of children selling cookies
double averageBoxes; // Average number of boxes sold per child
totalBoxes=0;
numSeller=1;
// WRITE CODE TO INITIALIZE THE totalBoxes ACCUMLATOR TO 0 AND
// THE numSeller COUNTER TO 1.
cout << " **** Cookie Sales Information **** ";
// Get the first input
cout << "Enter number of boxes of cookies sold by seller " << numSeller
<< " (or -1 to quit): ";
cin >> numBoxes;
// WRITE CODE TO START A while LOOP THAT LOOPS WHILE numBoxes
// IS NOT EQUAL TO -1, THE SENTINEL VALUE.
while(numBoxes != -1)
{
// WRITE CODE TO ADD numBoxes TO THE totalBoxes ACCUMULATOR.
// WRITE CODE TO ADD 1 TO THE numSeller COUNTER.
totalBoxes += numBoxes;
numSeller += 1;
// WRITE CODE TO PROMPT FOR AND INPUT THE NUMBER OF BOXES
// SOLD BY THE NEXT SELLER.
cout << "Enter number of boxes of cookies sold by seller " << numSeller
<< " (or -1 to quit): ";
cin >> numBoxes;
}
// WHEN THE LOOP IS EXITED, THE VALUE STORED IN THE numSeller COUNTER
// WILL BE ONE MORE THAN THE ACTUAL NUMBER OF SELLERS. SO WRITE CODE
// TO ADJUST IT TO THE ACTUAL NUMBER OF SELLERS.
numSeller = numSeller-1;
if (numSeller == 0) // If true, -1 was the very first entry
cout << " No boxes were sold. ";
else
{
// WRITE CODE TO ASSIGN averageBoxes THE COMPUTED AVERAGE NUMBER
// OF BOXES SOLD PER SELLER.
averageBoxes = totalBoxes/numSeller;
// WRITE CODE TO PRINT OUT THE NUMBER OF SELLERS AND AVERAGE NUMBER
// OF BOXES SOLD PER SELLER.
cout << "The number of sellers was " << numSeller <<" Average number of boxes sold: " << averageBoxes;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.