Engineering
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.

answer
Answers: 3

Other questions on the subject: Engineering

image
Engineering, 03.07.2019 15:10, EmilySerna
Heat is added to a piston-cylinder device filled with 2 kg of air to raise its temperature 400 c from an initial temperature of t1 27 cand pressure of pi 1 mpa. the process is isobaric process. find a)-the final pressure p2 b)-the heat transfer to the air.
Answers: 1
image
Engineering, 04.07.2019 18:10, Talos02
Burgers vector is generally parallel to the dislocation line. a)-true b)-false
Answers: 2
image
Engineering, 04.07.2019 18:20, karatsgrande3772
Determine the damped natural frequencies and the steady state response of a decoupled damped forced two degrees of freedom system. 10ä1 + 2q1 20q1 10 cos t; 10q2 +4q2 + 40q2 10 cos t
Answers: 3
image
Engineering, 04.07.2019 19:10, gabrielaperezcz
Air inially occupying a volume of 1 m2 at 100 kpa, 27 c undergoes three internally reversible processes in series. process 1-2 compression to 500 kpa during which pv constant process 2-3 adiabatic expanslon to 100 kpa process 3-1: constant-pressure expansion to 100 kpa (a) calculate the change of entropy for each of the three processes. (b) calculate the heat and work involved in each process. (c) is this cycle a power cycle or refrigeration cycle?
Answers: 3
Do you know the correct answer?
Using a Counter, an Accumulator, and an End Sentinel

Step 1: Remove loops. cpp from the...

Questions in other subjects:

Konu
Mathematics, 21.04.2021 02:40
Konu
History, 21.04.2021 02:40