Programming Project # 3

Rectangles

Due: Thursday, July 9, 1998 before class


This project is designed to give you experience writing your own functions and using functions from the math and stdlib libraries, as well as giving you practice with nested for loops.

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 use the srand() function from the stdlib library along with the time() function from the time library to seed the random number generator. The statement you will use is srand (time (NULL)); You will then use the rand() function to generate two integers in the range of 1 to 9. These two integers will be the lengths of the sides of the rectangle. You will pass the lengths of these two sides to a function called PrintRect(), which you will write. This function will print out a filled rectangle with the ascii character '#' as described above. The first number generated will be the width of the rectangle and the second number will be the height. If the two integers generated were 5 and 2, the function PrintRect() would print:

########## ##########

You will then write another function called Diagonal(), which will take the width and height as arguments and will calculate and return the length of 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).

The last function you will write is called PrintResults(), which will print out the width, height and the length of the diagonal of the rectangle to three decimal places. The diagonal should be of type double.


Sample Output:

######## ######## ######## Rectangle's width = 4 height = 3 diagonal = 5.000


As a head start, here are the function prototypes:

void PrintRect (int width, int height); double Diagonal (int width, int height); void PrintResults (int width, int height, double diagonal);

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 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