Project 3 - Rectangles
Due: Wednesday, May 3 BEFORE CLASS
Objectives:
- To practice writing functions
- To use predefined math library functions
- To practice using nested for loops
- To practice using the switch statement
Your program will display a filled-in rectangle of a given ASCII character. The width of the rectangle will be displayed by letting each ASCII character represent one/half of a unit across in a row (so 2 characters per unit across). The height of the rectangle will be displayed by letting the height of each ASCII character be one unit.
########## Here is an example of a rectangle that
########## has a width of 5 and a height of 4 and
########## is drawn using the ASCII character '#'.
##########
Using two characters across for a unit and one character per unit down will
give you a rectangle that more closely resembles the dimensions you'd like to
see. This is because printed ASCII characters are approximately twice as tall
as they are wide.
The program will ask the user to enter the width of the rectangle, the
height of the rectangle, and the ASCII character to be used to display the rectangle. For example, if the user enters a width of 5, a height of 2, and the character '*', the function DrawRect() will display:
**********
**********
You SHOULD NOT allow the user to enter negative numbers as the dimensions
of the rectangle. You should error check the input and make the user
keep entering a number until he/she enters a positive number. See the
sample output below.
After getting the user input, you must present a menu to the user. The
menu should have the five options shown in the sample output below. The functions you will write, in addition to the main function, are:
- int Area(int width, int height), which returns the area of the rectangle.
- int Perimeter(int width, int height), which returns the perimeter of the rectangle.
- double Diagonal(int width, int height), which calculates and returns the diagonal of the rectangle. (Hint: Use the Pythagorean theorem to
calculate the diagonal of the rectangle.)
- void DrawRect(int width, int height, char display), which displays the rectangle using the ASCII character the user entered.
Each function corresponds to a menu option. The menu should continue to display until the users enters a Q for quit. You should only allow the user to enter UPPERCASE menu options.
Give your program source code file the name proj3.c
. E-mail this file to Evelyn (cwang3@cs.umbc.edu). Do NOT send your executable file (a.out
).
Sample Output
Enter the width: -4
Please enter a positive integer: -2
Please enter a positive integer: 4
Enter the height: 3
Enter a character to be used to draw the rectangle: #
Menu
A -- Calculate the Area of the Rectangle
P -- Calculate the Perimeter of the Rectangle
L -- Calculate the Length of the Diagonal of the Rectangle
D -- Display the Rectangle
Q -- Quit
Selection: L
The length of the diagonal is: 5.000
Menu
A -- Calculate the Area of the Rectangle
P -- Calculate the Perimeter of the Rectangle
L -- Calculate the Length of the Diagonal of the Rectangle
D -- Display the Rectangle
Q -- Quit
Selection: T
T is an invalid menu option.
Menu
A -- Calculate the Area of the Rectangle
P -- Calculate the Perimeter of the Rectangle
L -- Calculate the Length of the Diagonal of the Rectangle
D -- Display the Rectangle
Q -- Quit
Selection: r
r is in invalid menu option.
Menu
A -- Calculate the Area of the Rectangle
P -- Calculate the Perimeter of the Rectangle
L -- Calculate the Length of the Diagonal of the Rectangle
D -- Display the Rectangle
Q -- Quit
Selection: D
A rectangle with 4 columns and 3 rows
########
########
########
Menu
A -- Calculate the Area of the Rectangle
P -- Calculate the Perimeter of the Rectangle
L -- Calculate the Length of the Diagonal of the Rectangle
D -- Display the Rectangle
Q -- Quit
Selection: Q
End of program.
Important Note : You must tell the compiler where to find the math
library, or your program will not compile. This is accomplished by
using the -lm (the lowercase letter 'l', not the number one) option when
compiling. So, in order to compile a program that calls a function in the math library, not only do you have to #include the math.h header file, but you also must compile with the -lm option. So, compile your program as follows:
cc proj3.c -lm