UMBC CMSC 104 * CSEE | 104 | current 104

### Programming Project # 3 Rectangles Due: Tuesday, July 13, 1999 before 6:00 P.M.

This project is designed to give you experience writing your own functions and using functions from the math library, as well as 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.

You will ask the user to input the width of the rectangle, the height of the rectangle and also the ascii character you should use to display the rectangle. For example, if the user entered a width of 5, a height of 2 and the character '*', 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)
• void DrawRect(int width, int height, char display) which displays the rectangle using the ascii character the user entered
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 a Q for quit. You should only allow the user to enter UPPERCASE menu options.

## 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 D -- Calculate the Length of the Diagonal of the Rectangle R -- Display the Rectangle Q -- Quit Selection: D The length of the diagonal is: 5.000 Menu A -- Calculate the Area of the Rectangle P -- Calculate the Perimeter of the Rectangle D -- Calculate the Length of the Diagonal of the Rectangle R -- 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 D -- Calculate the Length of the Diagonal of the Rectangle R -- 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 D -- Calculate the Length of the Diagonal of the Rectangle R -- Display the Rectangle Q -- Quit Selection: R A rectangle with 4 columns and 3 rows ######## ######## ######## Menu A -- Calculate the Area of the Rectangle P -- Calculate the Perimeter of the Rectangle D -- Calculate the Length of the Diagonal of the Rectangle R -- Display the Rectangle Q -- Quit Selection: Q 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 -lm proj3.c