COP 1220 (C Programming) Project #2

Due:  Monday, June 11, 2001, by the start of class


To reinforce the use of basic programming concepts: functions and 2-dimensional arrays.  To practice good programming techniques and design, and good program documentation.


Your family and friends know you are doing well with computer classes.  But they wonder why you waste your time with loan schedules, instead of doing something useful such as printing "Happy Birthday" banners and posters.  For this project, we will develop a very simple graphics package.  Your program will be able to print lines of stars (asterisks) which will allow you to make simple letter shapes, histograms, and pictures.  (Only please don't waste paper!)

Programming Requirements:

Your program prints a 23 line by 79 column "image", where each point or pixel in the image is represented in a two dimensional array of char by its coordinates---a pair of row and column numbers (integers).  For example, the pixel in the lower left-hand corner has coordinates (0,0), while the pixel in the upper right-hand corner has coordinates (22,78).

The input to this program is a series of commands to draw horizontal lines on the image, vertical lines, to clear the image (erase), and to print the drawn image to stdout (where it can be saved in a file for later printing or viewing.  The program works with only one image at a time.  First you clear the image, then draw a series of lines to build up the picture you want.  When all the elements of the image are complete, a print command will output the completed image.  (This can be followed by a clear command and more line drawing commands, to build a new picture, followed by another print command.)  Note that only the print command produces any output!  The other commands just change the contents of the array.  (This is actually how real graphics programs work: they modify a buffer containing an image with a series of drawing commands, then send the completed image to some output device.)

A real graphics package would include other commands to draw circles and other shapes and patterns.  A well designed program would allow for such future extensions.

The program should read commands from the standard input (stdin) until end-of-file.  Each command is on a separate line.  Typically the commands will be stored in a file, and your program should use I/O redirection to read from that file.  This program is not interactive!  There should not be any prompting for input.  The required input commands are:

The user input command clear should invoke a function called clear_image, which will put a space (blank) in each pixel of the image.  (That is, in each char of the two dimensional array.)

The command print should invoke a function called print_image, which will print the entire image to standard output.  Note that the first line to be printed is row 22, then row 21, ..., row 1, and then row 0.  You must print out the rows in this order, or else your picture will appear upside down! 

The command comment is ignored.  This command allows you to put comments in the input command files.

The command hline should invoke a function called horiz_line, where (row, col1) are the coordinates of the starting point and (row, col2) are the coordinates of the end point of a line.  All coordinates values are separated by white space only.  To draw a line, figure out which pixels would be on the line, and put a star ("*") in those pixels.

The command vline should invoke a function called vert_line, where (row1, col) are the coordinates of the starting point and (row2, col) are the coordinates of the end point of a line.  Otherwise this command is the same as hline.

For both hline and vline, you must decide what arguments to pass to the function and what return value (if any) is appropriate.  (Keep in mind the guidelines for proper function design.)

If an unrecognizable command is read, the program must print an error message to stderr, not stdout, telling the user which command it didn't understand.  The program must also print an error message to stderr if the parameters to the hline or vline commands are erroneous or missing.  For example, the input command "hline -1 20.5 80" contains three bad values: row is too small, col1 is not an integer, and col2 is too large (column numbers must be <= 78).  After printing the error message, your program should continue on rather than stopping.  Note that it is not required that your program should be able to determine exactly what was wrong with the input; it is required that your program detect that something was wrong.  At a minimum, your program must check for too few values (e.g., "hline 10 20") and values outside the legal ranges.  (Checking correctly for all errors is harder than you might think!)  You must also decide how to handle blank lines.

Finally, you must use #define NUM_ROWS 23 and #define NUM_COLS 79 to define the size of your image.  It should be possible to change the size of the image (to fill an 8.5" x 11" sheet of paper for instance) simply by changing these two defines and recompiling your program.  Using the given values the picture will fit onto your computer screen easily.

Test Data:

(Copy and paste into a file or download, then use I/O redirection.  Notice the blank lines shown in yellow.)
comment  The letter H:
vline 15  10 20
hline 15  15 19
vline 19  10 20
comment  The letter A:
vline 23  10 20
hline 20  23 27
hline 15  23 27
vline 27  10 20
comment  The letter P:
vline 31  10 20
hline 20  31 35
hline 15  31 35
vline 35  15 20
comment  The letter P:
vline 39  10 20
hline 20  39 43
hline 15  39 43
vline 43  15 20
comment  The letter Y:
vline 47  15 20
vline 51  15 20
hline 15  47 51
vline 49  10 15

To Be Turned In:

Ask me for help if you get stuck!

Send comments and mail to the WebMaster.