Help for solving assignment Programming C++

profileomaralekhmimi
firstname_lastname_cpsc120_sec_lab7.doc

CPSC 120

Spring 2014

Lab 7

Name _____________________________

Practice Objectives of this Lab:

1. The while Loop

2. The Increment and Decrement Operators

3. Counters

4. The do-while loop

5. The for loop

6. Nested Loops

7. Using Files for Data Storage

8. Creating Good Test Data

Grading:

1. 7.1 10 points

7.2-7.7 15 points each

100 points totally

2. Your final complete solution report is due before your lab on 04/24/2014.

To begin

· Log on to your system and create a folder named Lab7 in your work space.

· Start the C++ IDE (Visual Studio) and create a project named Lab7.

LAB 7.1 (10 points) – TRY IT: Using the Increment and Decrement Operators

Step 1: Add the tryIt5A.cpp program in your Lab7 folder to the project. Below is a copy of the source code lines for int main().

1 // Lab 7 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 7.2 (15 points) – TRY IT: Examining Looping Constructs

Step 1: Remove tryIt5A.cpp from the project and add the tryIt5B.cpp program in your Lab7 folder to the project. Below is a copy of the source code lines for int main().

1 // Lab 7 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 7.3 (15 points) – Working with Looping Structures

Step 1: Remove tryIt5B.cpp from the project and add the loops.cpp program in your Lab7 folder to the project. Below is a copy of the source code for int main().

1 // Lab 7 - 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: Copy and paste the final, revised source code.

Step 5: Make a screenshot of the output 5.

LAB 7.4 (15 points) – Complete Program

Step 1: Add the summation.cpp program in your Lab7 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 ‘N’ or ‘n’ 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: Copy and paste the final, revised source code.

Step 7: Make a screenshot of the output created by the inputs shown in step 5.

LAB 7.5 (15 points) – Using a Counter, an Accumulator, and an End Sentinel

Step 1: Remove summation.cpp from the project and add the cookies.cpp program in your Lab7 folder to the project. Below is a copy of the source.

1 // Lab 7 - 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: Copy and paste the final, revised source code.

Step 6: Make 3 screenshots of the output created by the inputs shown in step 3.

Please do Programming Challenges in the following way.

1) Pseudocode of your program (20% grading);

2) Your C++ source code (include your name, section number, CSUF email, lab number and date (60% grading);

3) The results gotten (3 Screen Shots) using any 3 testing cases that are reasonable

(20% grading)

7.6 (15 points) –Programming Challenges of Chapter 5 # 18 Rectangle Display (Page 320)

image1.png

7.7 (15 points) –Programming Challenges of Chapter 5 # 24 Total and Average Rainfall (Page 321)

image2.png

7