subject
Engineering, 25.02.2020 21:50 Jroc23

Using a Counter, an Accumulator, and an End Sentinel

Step 1: Remove loops. cpp from the project and add the cookies. cpp program in your Lab5 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
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 \n\n";
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 << "\nNo boxes were sold.\n";
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.

Step 5: If your professor asks you to do so, print the final, revised source code and the output of the three test cases to hand in.

ansver
Answers: 3

Another question on Engineering

question
Engineering, 04.07.2019 18:10
Acompressor receives the shaft work to decrease the pressure of the fluid. a)- true b)- false
Answers: 3
question
Engineering, 04.07.2019 18:10
During a steady flow process, the change of energy with respect to time is zero. a)- true b)- false
Answers: 2
question
Engineering, 04.07.2019 18:10
The drive force for diffusion is 7 fick's first law can be used to solve the non-steady state diffusion. a)-true b)-false
Answers: 1
question
Engineering, 04.07.2019 18:10
Ajournal bearing has a journal diameter of 3.250 in with a unilateral tolerance of 20.003 in. the bushing bore has a diameter of 3.256 in and a unilateral tolerance of 0.004 in. the bushing is 2.8 in long and supports a 700-lbf load. the journal speed is 900 rev/min. find the minimum oil film thickness and the maximum film pressure for both sae 20 and sae 20w-30 lubricants, for the tightest assembly if the operating film temperature is 160°f. a computer code is appropriate for solving this problem.
Answers: 3
You know the right answer?
Using a Counter, an Accumulator, and an End Sentinel

Step 1: Remove loops. cpp from the...
Questions
question
English, 26.03.2021 18:10
question
Mathematics, 26.03.2021 18:10
Questions on the website: 13722360