Topics:
a. Today's lab will begin with a demonstration of how to use the CodeWarrior Debugger. The debugger is an extremely valuable tool for the programmer. The debugger lets us step through our code one line at a time and shows how the value of our program variables change as the program executes. The debugger is also very useful for tracing the flow of control through our programs. If you miss this part or need more help, see the code warrior help menu. (Help->Index->type in the word debugger). Learning how to use the debugger will save you lots of time when programming.
b. An example program for computing the odds of hitting the jackpot was given in lecture. We will now use that program to practice using the debugger. Create a Code Warrior project (yourLastNameLab5). Add the following code to a C++ program (yourLastNameLab5Part1b.cpp). Enable the debugger (Project -> Enable Debugger). Run the program and use the debugger to trace the value of each variable as the program executes. Record the value of each variable as the program executes. For example if the user enters 10 for the range and 2 for the number of matches the variable would change as noted below.
i |
num |
odds |
range |
2 |
2 |
1 |
10 |
1 |
2 |
5 |
9 |
0 |
2 |
45 |
8 |
Test Case 1 - range = 15 - num = 3
Test Case 1 - range = 36 - num = 5
Test Case 1 - range = 50 - num = 6
Disable the debugger (Project -> Disable Debugger) before going on.
/**********************************************************/ //odds.cpp //Used by: Add your name here //Written by: R. A. Hillyard //Last Modified 09/24/2001 // //Program to compute the odds of hitting the jackpot when the //number of matches and the range are input by the user. //For example what are the odds of picking 6 out of 50??? /*********************************************************/ #include<iostream> using namespace std; int main() { long odds; int i; int range; int num; cout << " enter a range: "; cin >> range; cout << " enter number to match: "; cin >> num; odds = 1; for (i=num; i > 0; i--) { odds = odds * range / i; range--; } cout << "odds are " << odds << " to 1\n"; return 0; }//end main /***********************************************************/
a. What is printed by the following program fragments? Work out the answers without using the computer. For each part hand trace the value of each variable as the program executes(draw a chart like part 1). Verify your answers by incorporating each loop into a program. (yourLastNameLab5Part2a.cpp)
1.
|
2.
|
3.
|
4.
|
5.
|
Enter price for CD #1: 16.50 Enter price for CD #2: 15.28 Enter price for CD #3: 14.99 Enter price for CD #4: 15.15 Enter price for CD #5: 17.01 Enter price for CD #6: 15.95 Low price: 14.99 High price: 17.01 Total price: 94.88 Avg price: 15.81
c. Write a C++ program (yourLastNameLab5Part2c.cpp) that raises x to the y power. The program should prompt the user to enter two integer values, then compute x to the y power (x * x * ... y times). Use a for loop to solve this problem.
a.) Write a C++ program (yourLastNameLab5Part3a.cpp) that prints a menu of choices - asking the user to enter a single letter representing a shirt size(S, M, L, X). The program then prints a message that echo the shirt size based on the letter entered (S -> Small, M - > Medium, L -> Large, X -> Extra Large). Use a switch statement to print the messages. The switch statement should have a default case for invalid input. See Sample run below (user input in bold).
//Sample run Enter one of the following S for Small M for Medium L for Large X for Extra Large Your choice: L You asked for a Large shirt.
Enter one of the following S for Small M for Medium L for Large X for Extra Large Your choice: C C is not a valid entry.
b.) Modify the program in part a to run in a loop (SaveAs - yourLastNameLab5Part3b.cpp) . The program must run until the user chooses to quit. Allow user input to be upper or lower case. See Sample run below (user input in bold). Use a do - while loop.
//Sample Run Enter one of the following S for Small M for Medium L for Large X for Extra Large Q to quit Your choice: L
You asked for a Large shirt.
Enter one of the following S for Small M for Medium L for Large X for Extra Large Q to quit Your choice: Q
Bye for now!!!