|
CMSC 104
Sections 103/105/401
Programming Project Three
The 2-Number Menu
Assigned: Wednesday 4/21/99
Due: Before Midnight Tuesday 5/4//99
|
The Objective
The purpose of this project is to give you practice
writing C programs with while loops, switches and functions.
The Background
In systems which do not support a Graphical User Interface (GUI),
it is necessary to present the user with a list of commands in
the form of a menu. The user chooses a selection from the menu and
the program performs the associated task.
The Task
Your task is to write a program that implements a menu of commands
which operate on two integers. Your menu must support the following commands:
- Enter two new integers, N1 and N2
- Determine which integer is the larger
- Compute the average of N1 and N2 and print with 2 decimal places
- Determine if N1 is a multiple of N2 or if N2 is a multiple of N1
- Compute N1 to the N2 power
- Determine if N1 is a multiple of N2
- Draw a rectangle of *s with N1 rows and N2 columns
- Quit
The user should input a letter which corresponds to his
menu choice. Use the getchar() function to input the menu
selection (watch out for those newlines!).
Your program should accept both uppercase and lowercase letters
for the commands. Use the following letters:
- use I to input 2 new integers
- use L to determine the larger number
- use A to calculate the average of the numbers
- use M to determine if N1 is a multiple of N2
- use P to raise N1 to the N2 power
- use R to print a rectangle with N1 rows and N2 columns
- use Q to Quit
Your program may assume the following that the user will input integers
(not floating point) where required (but watch out for negatives and zero),
and will input a single character and ENTER when choosing from the menu.
Required Functions
Your program must include functions with the following prototypes
- char GetMenuOption (void) that outputs the menu, inputs the users
selection and returns the choice
- int GetPositiveInteger (void) which prompts the user for a
positive integer, validates the input and returns the positive integer entered.
- int Larger (int n1, int n2) which returns the larger of n1 and n2
- float Average (int n1, int n2) which calculates and returns
the average of the two integers
- int Multiple (int n1, int n2) which determines if n1 is a
multiple of n2. The function returns TRUE (1) if n1 is a multiple of
n2 and returns FALSE (0) is n1 is not a multiple of n2.
- void DrawRectangle (int nrRows, int nrColumns) that draws a
rectangle of *s with nrRows rows and nrColumns columns.
- Your program must use the math function pow() to calculate
N1 to the N2 power.
Other program requirements
Be sure that your program is "robust", meaning
- it validates the user's menu selection
- it validates that the integers input by the user are positive
- it requires that menu option I (enter integers) be the very first
command.
Trying any other command first should result in a suitable
error message.
Be sure that your program has a file header comment that includes
- the name of the file
- your name
- the date the file was written
- a description of your program
- your algorithm or pseudo-code to solve the problem
and all functions have appropriate function header comments as discussed
in class.
Your program must adhere to the
coding standards and
indentation styles discussed in class.
Be sure your project is well commented.
See course syllabus for project grading guidelines and
university policies concerning sharing of code.
Sample Output
retriever[102] a.out
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: t
Invalid Menu Selection: t
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: L
Please execute command 'I' first
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: i
Please enter a postive integer: -4
-4 is not Postive, please try again
Please enter a postive integer: 5
N1 = 5
Please enter a postive integer: 6
N2 = 6
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: L
6 is larger than 5
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: a
The average of 5 and 6 is 5.50
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: p
5 to the 6 power is 15625.00
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: r
A rectangle with 5 rows and 6 columns
******
******
******
******
******
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: R
A rectangle with 5 rows and 6 columns
******
******
******
******
******
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: M
5 is NOT a multiple of 6
Menu List
I -- Enter Integers
L -- Determine Larger Integer
A -- Calculate the average of N1 and N2
P -- Raise N1 to the N2 power
M -- Is N1 a multiple of N2?
R -- Draw a rectangle with N1 rows and N2 columns
Q -- Quit
Selection: q
bye for now
retriever[103]
Although your output need not be identical to the above,
all information must be present.
Compiling your code
Since this project requires the use of the C math library
(because we are using the pow() function), the command
for compiling your program is slightly different than before.
Use the command:
cc proj3.c -lm
The -lm at the end tells the linker that we wish to use the math library
Submitting the Program
Your C source code file for this project MUST be called
proj3.c.
To submit your project, type the following at the Unix prompt:
submit cs104-103 proj3 proj3.c
To verify that your project was submitted, you can execute the
following command at the Unix prompt. It will show all files that
you submitted in a format similar to the Unix 'ls' command.
submitls cs104-103 proj3