To use functions with parameters and a menu, introducing the technique of stepwise refinement.
c [the already written in Lab 10] greatest-common-factor code is made into a function for use in reducing fractions and finding the common denominator to add/subtract fractions.
c there should be a separate function for each operation
c there should be a function for outputting the problem/solution/reduced fraction
c error checking (Hint: think about where all the errors could come from)
Assignment:
Write a menu-driven program that manipulates fractions. Using the idea of stepwise refinement, you should write a function for each arithmetic operation. This is “stepwise” because we are breaking the problem into more manageable pieces and dealing with each in turn. Additionally, you should have a function that will get a fraction and check its validity (i.e., denominator not equal to zero).
Your program should ask the user to input two fractions and to select an operation (+, -, *, /). Then, it should display the problem along with the solution. The program should recognize division by zero and print an appropriate error message. It should loop around until the user chooses to quit.
Test your program with positive and negative fractions and with zeroes.
Reduce all answers. Displaying the reduced fraction only if different from the original. Do not leave a fraction with denominator of 1.
Sample Run:
Enter the numerator and denominator of the first fraction: 5 6
Enter the numerator and denominator of the second fraction: 1 3
Menu of Operations
Add...................1
Subtract..............2
Multiply..............3
Divide................4
Which operation? 2
5 1 3 1
--- - --- = --- = ---
6 3 6 2
Another problem (y for yes, n for no)? n
Lab 13
Stars
Objective
To explore nested for loops and the use of the index variable.
Assignment
Read the following code carefully. In the box, write out what you predict the output will be.
#include <iostream>
using namespace std;
int main()
{
int row, col;
cout << endl;
for(row = 0; row < 5; row++)
{
for(col = row; col >= 0; col --)
cout << "*";
cout << endl;
}
return 0;
}
Type in and run the code to see if you were right.
Now, write a C++ program that will generate the following patterns. Note that you must use for loops and may not use any if statements! Each of the designs should be implemented in a separate function or functions. This needs to work for any size grid and each generated pattern should have the same number of rows.
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
*
***
*****
*******
*********
***********
*************
For the final pattern, you may use if statements, but you may not simply output the text of the pattern using cout. This pattern should be generated by a function and have the same number of rows as the previous patterns.
*******
** ** (Note: This pattern is
* * * * a square.)
* * *
* * * *
** **
*******
Make sure each function accepts a single integer parameter N and draw your patterns to a height of N rows.
Lab 14
Miniature Golf
Objective:
To gain more experience with one-dimensional arrays of fixed length.
Assignment:
Write a program, containing 3 functions, that asks the user for the number of strokes taken on each hole of an 18-hole miniature golf course, calculates the final score, and prints out a scorecard. Also state how many holes-in-one the player had.
Remember when you pass your array it is a reference parameter, even if you are not changing its contents. Just be careful that you are not accidentally making any modifications.
Output for scorecard should look just like what you see below, with proper spacing and alignment.
Sample Run:
Enter name of player: Dylan
Enter the number of strokes for each hole.
hole[1]: 3
hole[2]: 1
:
hole[18]: 5
Dylan, here is your scorecard:
Hole : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
------------------------------------------------------------
Score: 3 1 6 4 2 3 3 1 2 4 5 3 2 2 2 3 4 5
Your total is 55.
You scored 2 holes-in-one.
Hint:
The field width manipulator setw (found in the header file iomanip) will right-justify the number that follows in a field 3 characters wide.
0 comments:
Post a Comment