Project Analysis

 

 

Starting Out With C++ Early Objects
Lab 5

Name _____________________________
To begin
• Log on to your system and create a folder named Lab5 in your work space.
• Copy all the files in the Chapter05 lab folder to your Lab5 folder.
• Start the C++ IDE running and create a project named Lab5.
LAB 5.1 – TRY IT: Using the Increment and Decrement Operators
Step 1: Add the tryIt5A.cpp program in your Lab5 folder to the project. Below is a copy of the source code lines for int main().
1 // Lab 5 tryIt5A

8 int main()
9 {
10 int a = 5, b = 5,
11 c = 3, d = 7;
12
13 cout << a– << ‘ ‘;
14 cout << a << ‘ ‘;
15 cout << a– << ‘ ‘ << a– << ‘ ‘;
16 cout << a– << ‘ ‘ << a << endl;
17
18 cout << ++b << ‘ ‘;
19 cout << b << ‘ ‘;
20 cout << ++b << ‘ ‘ << ++b << ‘ ‘;
21 cout << ++b << ‘ ‘ << b << endl;
22
23 a = c++ * d–;
24 cout << a << ” ” << c << ‘ ‘ << d << endl;
25
26 return 0;
27 }
Expected Output

 

 

 

_______________

 

_______________

_______________
Observed Output

 

 

 

_______________

 

_______________

 

_______________

Step 2: Read the source code, paying special attention to the increment and decrement operators, noticing whether each is used in prefix or postfix mode. Then complete the “Expected Output” column, writing down the output you think each set of cout statements will produce.
Step 3: Now compile and run the tryIt5A.cpp program, and look at the output it creates. If the actual output from a cout statement matches what you wrote down, just place a checkmark in the “Observed Output” column. If it is not the same, write down the actual output.
Step 4: Why, in line 24, were double quotes needed to print 2 blank spaces, but single quotes used to print one blank space? ______________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

LAB 5.2 – TRY IT: Examining Looping Constructs
Step 1: Remove tryIt5A.cpp from the project and add the tryIt5B.cpp program in your Lab5 folder to the project. Below is a copy of the source code lines for int main().
1 // Lab 5 tryIt5B

8 int main()
9 {
10 int i, // Loop control variable
11 sum; // Accumulator
12
13 i = 1;
14 while (i < 10)
15 { cout << i << ‘ ‘;
16 i +=2;
17 }
18 cout << “\nAfter loop i = ” << i
19 << endl << endl;
20
21 i = 5;
22 while (i > 0)
23 cout << i– << ‘ ‘;
24 cout << “\nAfter loop i = ” << i
25 << endl << endl;
26
27 i = 1;
28 do
29 { cout << i * i << ‘ ‘;
30 i++;
31 } while (i <= 3);
32 cout << “\nAfter loop i = ” << i
33 << endl << endl;
34
35 sum = 0;
36 for (i = 0; i < 4; i++)
37 sum += i;
38 cout << “After loop i = ” << i << endl;
39 cout << “sum = ” << sum << endl << endl;
40
41 for (i = 0; i++ < 4;)
42 cout << i << ‘ ‘;
43 cout << “\nAfter loop i = ” << i << endl;
44
45 return 0;
46 }
Expected Output

 

 

 

_______________

After loop i = ___

 

_______________
After loop i = ___

 

_______________

After loop i = ___

 

After loop i = ___
sum = _________

_______________
After loop i = ___

Observed Output

 

 

 

_______________

After loop i = ___

 

_______________
After loop i = ___

 

_______________

After loop i = ___

 

After loop i = ___
sum = _________

_______________
After loop i = ___

Step 2: Read the source code, paying special attention to how each loop is controlled. Then complete the “Expected Output” column, writing down the output you think each cout statement will produce.

Step 3: Now compile and run the tryIt5B.cpp program, and look at the output it creates. If the actual output from a cout statement matches what you wrote down, just place a checkmark in the “Observed Output” column. If it is not the same, write down the actual output.

LAB 5.3 – Working with Looping Structures
Step 1: Remove tryIt5B.cpp from the project and add the loops.cpp program in your Lab5 folder to the project. Below is a copy of the source code for int main().
1 // Lab 5 – loops.cpp Working with looping structures
2 // PUT YOUR NAME HERE.

6 int main()
7 {
8 cout << “PUT YOUR NAME HERE. \n”;
9 cout << “\nActivity 1 \n==========\n”;
10 // Change the following do-while loop to a while loop.
11 int inputNum;
12 do
13 { cout << “Enter a number (or 0 to quit): “;
14 cin >> inputNum;
15 } while (inputNum != 0);
16
17 cout << “\nActivity 2 \n==========\n”;
18 // Change the following while loop to a do-while loop.
19 char doAgain = ‘y’;
20 while (doAgain == ‘Y’ || doAgain == ‘y’)
21 { cout << “Do you want to loop again? (y/n) “;
22 cin >> doAgain;
23 }
24
25 cout << “\nActivity 3 \n==========\n”;
26 // Change the following while loop to a for loop.
27 int count = 0;
28 while (count++ < 5)
29 cout << “Count is ” << count << endl;
30
31 cout << “\nActivity 4 \n==========\n”;
32 // Change the following for loop to a while loop.
33 for (int x = 5; x > 0; x–)
34 cout << x << ” seconds to go. \n”;
35
36 cout << “\nActivity 5 \n==========\n”;
37 // Make the following changes to the code below that uses nested loops:
38 // 1. The code is supposed to print 3 lines with a $ and 5 stars on
39 // each line, but it contains a logic error. Find and fix the error.
40 // 2. Then revise the code to follow each $ with just 4 stars, like this:
41 // $****
42 // $****
43 // $****
44 // 3. Change the two loop control variable names to be more descriptive.
45 for (int i = 1; i <= 3; i++)
46 { cout << ‘$’;
47 for (int j = 1; j <= 5; j++)
48 cout << ‘*’;
49 }
50 cout << endl;
51
52 return 0;
53 }

Step 2: Put your name on lines 2 and 8. Then compile and run the program to see what it does.
• When Activity 1 asks for inputs use the following inputs:
5
2
0
• When Activity 2 asks for input use the following inputs:
y
Y
n
• Print a copy of the output to compare to the output the program creates after you revise it.

Step 3: Make all the modifications requested in the source code. Then recompile the program and rerun it. If you have done everything correctly, you should get the same results as before for Activities 1 – 4. You should get the following output for Activity 5:
$****
$****
$****
Step 4: If your professor asks you to do so, print the final, revised source code and the output to hand in.

LAB 5.4 – 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 <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 **** \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.

LAB 5.5 – Using do-while with a Menu
Step 1: Make a copy of the completed areas.cpp program you wrote for Lab 4.7 and place it in your Lab5 folder. Name it areas2.cpp
Step 2: Remove cookies.cpp from the project and add the areas2.cpp program to the project.
Step 3: Add a do-while loop to the program so that the user can repeatedly display the menu, make a choice, and have the appropriate steps for that choice carried out. The loop should continue iterating until the user enters 4 for the menu choice. Have the program print several blank lines after each case is carried out before the menu displays again.
Step 4: Once the program compiles with no errors, test it with a single run that tries all menu choices before entering 4 to quit.

LAB 5.6 – Complete Program
Step 1: Remove areas2.cpp from the project and add the summation.cpp program in your Lab5 folder to the project. This file contains just a program shell in which you will write the programming statements needed to complete the program described below. Here is a copy of the file.
1 // Lab 5 – summation.cpp
2 // This program displays a series of terms and computes its sum.
3 // PUT YOUR NAME HERE.
4 #include <iostream>
5 #include <cmath>
6 using namespace std;
7
8 int main()
9 {
10 int denom, // Denominator of a particular term
11 finalDenom = 64; // Denominator of the final term
12 double sum = 0.0; // Accumulator that adds up all terms in the series
13
14 cout << “PUT YOUR NAME HERE. \n”;
15
16 // WRITE THE CODE TO START A FOR LOOP THAT LOOPS ONCE FOR EACH TERM.
17 // I.E., FOR TERMS WITH DENOMINATORS FROM 2 TO THE FINAL DENOMINATOR.
18 {
19 // WRITE THE CODE TO PRINT THIS TERM.
20 // IF IT IS NOT THE LAST TERM, FOLLOW IT WITH A +.
21 // IF IT IS THE LAST TERM, FOLLOW IT WITH A =.
22
23 // WRITE THE CODE TO ADD THE VALUE OF THIS TERM TO THE ACCUMULATOR.
24 }
25
26 // WRITE A LINE OF CODE TO PRINT THE SUM.
27
28 return 0;
29 }

Step 2: Design and implement the summation.cpp program so that it generates and prints the terms and the sum of the following series: 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64
Sample Run:
1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 = .984375
Step 3: Modify the program so that it generates and prints the terms and the sum of this series up through the nth term, where the user enters a value for n between 2 and 10.
For example, if the user enters 5, the program will display the terms and compute and print the summation of the following terms: 1/21 + 1/22 + 1/23 + 1/24 + 1/25
This will require the addition of much more program logic as well as some additional variables.
Sample Run:
This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n
What should n be in the final term (2 – 10)? 5
1/2 + 1/4 + 1/8 + 1/16 + 1/32 = .96875
Step 4: Once your program is working correctly, add a bottom test loop to the program that asks the user if he or she wishes to compute another series, and which continues to iterate so long as the user enters ‘y’ or ‘Y’. You will need additional program logic and a new variable to do this.

Step 5: Test your program with the following inputs before entering ‘y’ to quit.:
5
6
8
10
If your program is working correctly, all the sums will be less than 1. If they are not, you have a logic error which you need to find and fix.

Step 6: If your professor asks you to do so, print the final, revised source code and the output created by the inputs shown in step 5.

 

 

 

Sample Solution

In Homer’s’ The Iliad, King Agamemnon can give instances of how not to be a compelling pioneer. Agamemnon depicts negative initiative abilities and poor basic leadership all through the story. Towards the start of the lyric, it gets clear to the peruser that Agamemnon is anything but an exceptionally kind man. In the primary book, the dad of a lady being held hostage by Agamemnon moved toward Agamemnon with a truly sensible solicitation just as payoff. Agamemnon not just denied the man, he taunted and affronted him. The reaction was cold and unjustifiable.

I see Agamemnon’s choice to deny the man a poor initiative choice on two records. Initially, Agamemnon didn’t have a lot to lose in the circumstance. By tolerating the payoff he would diminish both the dad and girl just as enhance himself with the endowments he was advertised. As I would like to think, I accept a sufficient pioneer consistently has space in his/her heart for more prominent’s benefit of humankind. Agamemnon plainly depicted he is more intrigued by his status and personality than doing great for other people. What settled on his choice significantly increasingly less than ideal was that Agamemnon appeared to daze himself with narrow-mindedness. The man who mentioned for the sheltered return of his little girl happened to be a cleric to the god Apollo. Agamemnon knew this. He likewise may have realized that there would be ramifications for affronting a cleric of a divine being. Because of being denied and affronted the cleric (Chryses) argued Apollo to look for retribution upon Agamemnon. Before long, plague fell upon Agamemnon’s military, and proceeded for nine days. The plague brought about numerous dead Achaian troopers. Far more detestable, Agamemnon didn’t look for activity until his best warrior, Achilleus mentioned that he return the lady to her dad. By and by blinded by his very own wants, Agamemnon would possibly restore the lady on the off chance that we was repaid with another lady consequently. Achilleus volunteers his hostage lady to Agamemnon who acknowledges the arrangement.

Agamemnon’s inevitable destruction – (not in reference to the Iliad) can be connected to numerous poor choices and faulty character qualities. Absence of responsibility is an attribute that would be unsafe to any pioneer. I think taking responsibility for one’s activities is an honorable go about just as an important represent a satisfactory pioneer to take. It tends to be seen that Agamemnon likes to censure others for his own disasters. When attempting to legitimize Achilleus relinquishing the Achaian armed force he expresses, “This is the word the Achaians have verbally expressed frequently against me and criticized me in it, yet I am not capable but rather Zeus is, and Destiny, and Erinys the fog strolling who in get together captured my heart in savage daydream on that day I myself took from the prize of Achilleus. However what might I be able to do? The god achieves all things.” Book 19. Without Achilleus’ arrival to the Achaian armed force, almost certainly, Agamemnon and the Achaian armed force would’ve been compelled to give up.

In the same way as other pioneers ever, Agamemnon governed with an iron clench hand. In the wake of dissecting The Iliad, I would characterize Agamemnon as a ruler instead of a pioneer. Be that as it may, I would prefer not to ruin him altogether. Agamemnon had positive snapshots of administration. When driving his military into fight without the assistance of Achilleus he says, “Be men now, dear companions, and take up the core of mental fortitude, and have thought for one another in the solid experiences, since more come through alive when men think of each as other, and there is no magnificence when they give way, nor warcraft either.” Book 5 All altogether, I see Agamemnon as an egotistical ruler with little respect for the prosperity of his subjects. He’s more like a brutish dictator than a successful pioneer.

This question has been answered.

Get Answer
WeCreativez WhatsApp Support
Our customer support team is here to answer your questions. Ask us anything!
👋 Hi, Welcome to Compliant Papers.