Topics:
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
int1
is even or odd. Hint: use the mod (%) operator.
int1, int2,
and int3
.
int1
< int2
< int3
.
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.
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; |
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.