Programming Project # 5
Rectangles
Due: Monday, November 19, 2001 before midnight
The Objective
This project is designed to give you practice with nested for loops and switch
statements, as well as experience working with functions from the math library.
The Task
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 menu option print would
display:
**********
**********
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 'A' option will
calcuate the area of the rectangle and print the result to the screen.
- The 'P' option will calculate the perimeter
of the rectangle and print the result to the screen.
- The 'D' option will calculate the length of the diagonal
of the rectangle and print the result to the screen.
- The 'R' option displays the rectangle to the screen.
- 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 proj5.c, that command would be :
linux2[269]% gcc -ansi -Wall proj5.c -lm
Submitting the Program
Here is a sample submission command. Note that the project name starts with uppercase 'P'.
submit cs104_09 Proj5 proj5.c
To verify that your project was submitted, you can execute the
following command at the Unix prompt. It will show the file that
you submitted in a format similar to the Unix 'ls' command.
submitls cs104_09 Proj5