CPSC 120: Lab Assignment 2

Due at 2:00 pm on Thursday, 02/13/2014

Home | Calendar | Announcements | Assignments | Example Code | Notes


This assignment has three goals:



Your solutions to the following problems should be placed in a folder named "lab2". When you have finished the assignment, copy this folder and its contents to your turn-in directory.

  1. Consider the following drawing:
    void setup() {

    Write a version of this program that draws exactly the same thing, using a loop for the points.

  2. How many times does a loop run? What effect will each iteration of the loop have? In this exercise you'll gain practice with direct analysis of code, in order to better answer such questions. We learned how to trace a program's execution in Monday's class (February 3rd): write down each variable and its current value. Then mark the changes to variables as each statement executes. For example, with the loop
  3. int x = 50;
    int i = 0;
    while (i < 5) {
       x = x + 50;
       i = i + 1;

    our trace would look like this:

    x: 50 100 150 200 250  300
    i: 0 1 2 3 4 5

    This process allows us to visualize program execution as it happens, seeing, for example that point will be drawn from left to right, and that the loop runs exactly 5 times.

    Trace the following program, and draw what you think will be displayed when it is run. Do not run the program until you've completed this! If you find that what you expect is not what you observe, don't throw away the failed attempt! Rather start over, with the corrections you need to make, and turn in both traces, numbering them in the order of attempt.

    int d = 32;
    int x = 50;
    int y = 50;
    int dx = 40;
    int dy = 40;
    int j = 0;
    while (j < (d / 4) - 1) {
       if (j > 3) {
          dy = -40;
       x = x + dx;
       y = y + dy;
       j = j + 1;
  4. Consider the stipple_line4 examples from class. Modify the program to draw two additional lines, one from (33, 330) to (150,50) and the other from (23, 34) to (303, 255).
  5. Modify the makeLine method so that it draws a line with 20 dots, instead of 5.
  6. Modify the both the definition of makeLine and the calls to it in setup() method so that the number of dots used to draw a line can be given as an argument. For example, if we want a 34-dot line, we could use the call

Turn In

Make a folder in your turn in directory named "lab2". Copy all five of your solutions in to this folder. Consult the Linux tutorial from Lab #0 if you need a reminder of the details.


John H. E. Lasseter