CS 150 - Intro to Programming - Fall 2001
Lab Exercise 5

Topics:

Part 1 - Using the CodeWarrior Debugger.

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


Try the following three test cases - record your answers on the lab 5 answer sheet and turn in before you leave the lab.

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

/***********************************************************/

Part 2 - Looping Using the for statement

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.
int n = 5;
int i = 1;

for(i=1; i<n; i++) 
  {
  cout << i;
  }
cout << endl;

2.
int x = 1;
int y = 1;

for (; y<=5; y++)
  {
  x = x+y;
  x++;
  }
cout << " x = " << x << " y = " << y << endl;

3.
x = 1;

for(y=1; y<=5; cout << x << " ")
  {
  y = y + 2;
  x += y;
  }
cout << " x = " << x << " y = " << y << endl;

4.
int j = 1;

for (i=4; i>=1; i--)
  {
  for(j=1; j>=1; j--)
    {cout << j << " ";} 
  cout << i << endl;
  }

5.
int row = 1;
int col = 1;

for(row=1; row<=10; row++)
  {
  for(col=1; col<=10-row; col++)
    cout << '*';
  for(col=1; col<=2*row-1; col++)
    cout << ' '; 
  for(col=1; col<=10-row-1; col++)
    cout << '*'; 
  cout << endl;
  }


b. Write a C++ program (yourLastNameLab5Part2b.cpp) that asks the user to enter a series of floating-point values, each of which represents the price of a music CD. (the prices are floating point number with a range from 0.00 to 100.00, such as 19.95, 8.75, etc.). There are six prices, so you'll need to terminate the loop when six scores have been entered. Then display the lowest score, the highest score, the total of the scores, and the average score. Use a for loop to solve this problem.

The following is a sample dialogue (user inputs are in bold):

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.

Part 3 - Multiway selection using the switch statement and the do ... while loop

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!!!


What to turn in

Remember to: