Help solving programming C++ assignment
|
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
Grading:
1. 7.1-7.4 25 points each,
100 points totally
2. Your final complete solution report is due before your lab next week.
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 – 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 – 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 – 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 – 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 ‘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: Copy and paste the final, revised source code.
Step 7: Make a screenshot of the output created by the inputs shown in step 5.
1