CSCI 1300
Introduction to Computer Science - Spring 2003
Homework III

The first part of this homework must be submitted online by 9am on Wednesday, February 5, and you must bring a printed copy of your work to recitation on that day. The second part must be submitted online by 9am on Wednesday, February 12, and you must bring a printed copy of your work to recitation on that day.

While you are working on the assignment, you must save and test your work every 20 to 40 lines. These are your "restore points" that I will discuss in class. Some of the restore points must be submitted along with your final program.


Make sure you've gone through the preparation list for the week of Feb 4, paying particular attention to the material on reference paramters.

Programming Style

Beginning with this program, you'll need to follow all of our class programming style guidelines. One of the most important things for this program is that you do incremental development and testing. This means that you stop every 20 to 40 lines of code, check to make sure that your work compiles, and do whatever testing is possible at that point. When you are satisfied with the testing, make a copy of your program at that point. For example, if you call your program clock.cxx, then the copies that you make can be named clock-1.cxx, clock-2.cxx, and so on.

Open Assignment

This homework is open, so that you may work with others. However, you need to understand everything that you turn in (otherwise you'll do poorly on later assignments and quizzes).

Submitting to Dora

For all the homework assignments, including this one, you may submit to Dora and then check whether you passed her tests. If you did not pass the tests, then you should fix the problem and resubmit. You may resubmit as often as needed before the deadline. (However, for the lab exams, you may submit only once.)

Purpose of This Work

The purpose of this assignment is to make sure that you know how to write a small program that includes some graphics, a function that another programmer has written, and functions that use reference parameters.

Homework IIIA: A Shaded Window

Write a void function with two parameters. The first parameter, a double number called cc, will control colors in a way that I'll describe in a moment. The second parameter, an int called s, is the size (in pixels) of a graphics window that has already been opened. The function calls putpixel to set the color of every pixel in the graphics window (both the row and column could be from 0 to s-1). For a pixel that is in row number x and column number y, you should calculate the color by these formulas:

The function you write must use putpixel to color every pixel in the window according to the color calculated above. My goal is to make sure that you know how to color every pixel individually using a function that depends on the x-coordinate, the y-coordinate and some other interesting information (the cc value). The formulas that I've written above create two kinds of patterns: If cc is negative, then you'll get a shading from blue (in the bottom left corner) to black (in the top right corner). If cc is not negative, then the shading goes from blue (in the lower right corner) to black (in the upper left corner).

Extra requirement: The function must calculate the number cc/6.0 or -cc/6.0 only once. Don't keep calculating this value over and over. Calculate it once and store the result in a local variable. This makes the function more efficient.

As part of this assignment, you must also write a small main program that opens a graphics window of size 200 x 200. The program then calls your function repeatedly with a value of s=200 and values of cc from -12 to +12 in steps of 0.1. (Start with d=-12, then -11.9, then -11.8, and so on.) Do this inside a loop, and at the end of the loop's body give a small delay by calling delay(1).

When you run the program, the window will open and be shaded with cc=-12 (which goes from blue in the lower left corner to black in the upper right). As the program runs, you'll see the shading change.

Homework IIIB: A Clock

Read Michael's program from, which has a function with four reference parameters. The function ygets the current time and sets four variables (hour, minute, second and a character called a_or_p).

Write a new program that opens a 200 by 200 graphics window and then repeatedly does these things:

  1. Get the current time.
  2. Uses your shading function to shade the window with s=200 and cc equal to the number of hours before or after noon. Times before noon could be anywhere from -12 to 0. For example, at 10:30am, the value of cc would be -1.5. Times after noon can be from zero to +12.
  3. Draw a simple analog clock that displays the current hour and minute. The clock must have:
  4. Pause for ten seconds (which you can do by calling delay(10000), since ten seconds is 10000 milliseconds.
Your program may use Michael's get_time function with no changes, or you may change it to suit your needs if you prefer.


Work should be submit part IIA to Dora by 9am on Wednesday, Feb 5. And you should bring a printout of the program to your recitation to turn in to the TA. Part IIB is due one week later.

Students who do not give a printout to the TA during recitation will have a 2 point penalty (20%) on that part of the program. Students who submit work after 9am on Wednesday will have an additional penalty: -1 point for any late Wednesday submission; -1 additional point for each day through Monday. No submissions after Monday.