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

Topics:

Part 1 - Boolean Expressions and Branching Using if-else


A. Create a Code Warrior project (yourLastNameLab4). Write a C++ program (yourLastNameLab4Part1a.cpp) that computes the area of a triangle given the base and height. (area = (1/2) * (base * height). The program should prompt the user to enter the base and height, calculate the area and display the results. Make sure the program works before proceeding - use some known test data to test your program.

Next modify the program to add the following functionality. (use only if statements - no if - else).


B.Write a C++ program (yourLastNameLab4Part1b.cpp) that prompts the user for three integers, (for example: int1, int2, int3), then add the following functionality to your program - use if, if / else statements to accomplish each task. Label each section of your code with a comment, an example comment for part a might look like: //decide if int1 is even or odd

  1. display a message indicating whether int1 is even or odd. Hint: use the mod (%) operator.
  2. display a message indicating if at least one of the numbers is negative.
  3. display a message indicating whether the three numbers are equal or not.
  4. display the largest of int1, int2, and int3.
  5. display a message indicating whether the three variables are in ascending order. That is, whether: int1 < int2 < int3.


Part 2 - More Branching - Using the if - else statement

In this part, you will write a C++ program (yourLastNameLab4Part2.cpp) that prompts the user to enter a character and then displays a message indicating whether the character is a numeral, an uppercase letter, a lowercase letter, or something else. DO NOT solve this with a giant if-else statements (there are over 100 different characters!) Use the following method instead.

Most modern computers use the ASCII character code for storing individual characters. The ASCII code table is given in Appendix 3 of your book and should be used as a reference. However, you do not need to know the actual ASCII code of a character to solve this problem. Rather, use the following information about the general order of the ASCII character set to deduce what range the character must be in. The general order is:

For example, to decide if the character input is lowercase, we could test if the character is strictly greater than or equal to 'a' yet less than or equal to 'z' if it is, it must be a lowercase letter. Devise a test to check for lowercase, uppercase, and digits, anything else must be something else. You should use a nested if / else statement for this problem. Get one test at a time to work - then add the others one at a time.

Part 3 - Looping using the while statement

a. What is printed by the following program fragments? Work out the answers without using the computer. Verify your answers by incorporating each loop into a program (yourLastNameLab4Part3a.cpp). Add the segments one at a time - testing each before going on - divide each section with a comment separator. For example:

/**************************This is part 1**********************/

1.)

int counter = 1;

while(counter <= 5)
  {
  cout << counter << endl;
  counter++;
  }

2.)

int n = 0;					
int i = 1;

while (i >= n) 
  cout << i--;

3.)

i = 1;  //note: i already declared 
int j = 1;
				
while (i <= 4)
  {
  j = 1;
  while(j>=1)
    cout << j-- << " ";
  cout << i++ << endl;
  }

4.)

int x = 1;
int y = 1;
while ( x < 5 ) 
  {
  x = x + y;
  y++;
  }
cout << x << y << endl;

5.)

x = 1; // note: x already declared
y = 1; // note: y already declared
		
while (y<=5)
  {
  x = x+y;
  x++;
  y++;
  }
cout << " x = " << x << " y = " << y << endl;
 

b. Count Controlled Loops - Write a C++ program (yourLastNameLab4Part3b.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 prices have been entered. Then display the lowest price, the highest price, the total price, and the average price. Use a count controlled while 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. Sentinel controlled loop - Write a C++ program (yourLastNameLab4Part3c.cpp) that takes the program from part 1a and allows the user to keep running the program until they decide to quit. To accomplish this use an outer while loop to ask the user if they want to continue or quit. Use a sentinel value to control the outer loop.

What to turn in

Remember to: