CMSC 104, Spring 2002
Project 7 - Rectangles
Due Date: Midnight, Thursday, May 2
Note: No late projects will be accepted.
Point Value:
This project is worth 75 points.
Objectives:
- To practice writing functions
- To practice using nested for loops
- To practice using the switch statement
- To practice manipulating character data
Assignment:
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 may assume that the user will only enter integer values greater than zero
for the height and width.
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.
- 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 user enters a 5 to quit. If the user enters a number not in the
range of 1 to 5, inclusive, you should display the message "Menu choice must be
in the range of 1 to 5" and redisplay the menu.
Notes:
- Your program MUST use a switch statement.
- Display the length of the diagonal in floating point format with three
decimal places.
- The output from your program must match the format of the Sample Output below
EXACTLY.
- Give your program source code file the name proj7.c.
Sample Output
Enter the width: 7
Enter the height: 5
Enter a character to be used to draw the rectangle: &
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
Enter your choice (1 to 5): 0
Menu choice must be in the range of 1 to 5
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
Enter your choice (1 to 5): 6
Menu choice must be in the range of 1 to 5
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
Enter your choice (1 to 5): 1
The area is: 35
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
Enter your choice (1 to 5): 2
The perimeter is: 24
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
Enter your choice (1 to 5): 3
The length of the diagonal is: 8.602
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
Enter your choice (1 to 5): 4
A rectangle with 7 columns and 5 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
Enter your choice (1 to 5): 5
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:
gcc -ansi -Wall -lm proj7.c
Coding Standards:
Note that you MUST adhere to the coding
standards and indentation style
given on the Projects home page. Don't forget your function header
comments!
Project Submission:
Submit your project by e-mailing your
source code (proj7.c) to Zhipeng (zhipeng1@cs.umbc.edu) as an attachment. Make
the Subject of the e-mail Project 7 so that Zhipeng knows what it is.
Do NOT send your executable file (a.out).