Programming Project # 3 - Rectangles
Due: Midnight, Monday 12/4/00
This project is worth 100 points
This project is designed to give you experience writing your own functions
and using functions from the math library, as well as to give you
practice with nested for loops and switch statements.
The program you write will deal with rectangles. You will plot a filled-in
rectangle using an ascii character, where the width of the rectangle will be
plotted by letting each ascii character express one/half a unit across in a
row (so 2 characters per unit across) and where the height of the rectangle
will be plotted by letting the height of each ascii character be one unit.
########## Here's an example of a rectangle that
########## has a width of 5 and a height of 4 and
########## is plotted using the '#' 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. All rectangles will be printed using the '#' character.
You will ask the user to input the width of the rectangle, and the
height of the rectangle. For example, if the user entered a
width of 5, a height of 2, the function PrintRect() would print:
##########
##########
You SHOULD NOT allow the user to enter negative numbers as the dimensions
of the rectangle. You should error check the input and allow the user
to keep entering numbers 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 five options. Each option will perform a different task.
The functions you will write 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...it is the hypotenuse of the triangle formed by the width, the height and the diagonal)
(Bigger hint -- look up the math function sqrt() which can be used to calculate the square
root of a number.)
- void DrawRect(int width, int height, char display) which displays the rectangle using the ascii character specified.
Each function should correspond to a menu option. There should also be a Quit option on the menu. The menu should continue to display until the users enters the number for quit.
Also, your program must use a #define to represent the character used to display the
rectangle. Note that this character is passed into the function DrawRect().
This will be discussed more in class.
Sample Output
Your personal greeting goes here
Enter the width: -4
Please enter a positive integer: -2
Please enter a positive integer: 4
Enter the height: 3
Menu
1 -- Calculate the Area of the Rectangle
2 -- Calculate the Perimeter of the Rectangle
3 -- Calculate the Length of the Diagonal of the Rectangle
4 -- Display the Rectangle
5 -- Quit
Selection: 3
The length of the diagonal is: 5.000
Menu
1 -- Calculate the Area of the Rectangle
2 -- Calculate the Perimeter of the Rectangle
3 -- Calculate the Length of the Diagonal of the Rectangle
4 -- Display the Rectangle
5 -- Quit
Selection: 7
7 is an invalid menu option.
Menu
1 -- Calculate the Area of the Rectangle
2 -- Calculate the Perimeter of the Rectangle
3 -- Calculate the Length of the Diagonal of the Rectangle
4 -- Display the Rectangle
5 -- Quit
Selection: 99
99 is in invalid menu option.
Menu
1 -- Calculate the Area of the Rectangle
2 -- Calculate the Perimeter of the Rectangle
3 -- Calculate the Length of the Diagonal of the Rectangle
4 -- Display the Rectangle
5 -- Quit
Selection: 4
A rectangle with 4 columns and 3 rows
########
########
########
Menu
1 -- Calculate the Area of the Rectangle
2 -- Calculate the Perimeter of the Rectangle
3 -- Calculate the Length of the Diagonal of the Rectangle
4 -- Display the Rectangle
5 -- Quit
Selection: 5
Have a nice day!
Important Note : You must tell the compiler where to find the math
library, or else your program will not work. 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 also must compile with the -lm option. For a
source file called proj3.c, that command would be :
cc proj3.c -lm