# CPSC 120: Lab Assignment 2

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

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

### Overview

This assignment has three goals:

• You will gain further practice will problem solving, using the four basic control forms of sequential execution, assignment, conditional statements, and loops.
• You will learn how to analyze the behavior of a block of code without running it, in order to better understand the observed behavior of a program.
• You will gain an initial understanding of the use of method calls and the definition of methods.

• There is no new reading for this assignment. Refer to notes from this week's class and to the tutorial notes from last week.

### Exercises:

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() {
size(500,400);
background(187,100,75);

strokeWeight(10);

point(450,50);
point(437,68);
point(424,86);
point(411,104);
point(398,122);
point(385,140);
point(372,158);
point(359,176);
point(346,194);
point(333,212);
}
```

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) {
point(x,100);
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;
stroke(0,0,255);

int j = 0;

while (j < (d / 4) - 1) {
point(x,y);

if (j > 3) {
stroke(255,0,0);
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
makeLine(50,400,250,300,34);

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

### Standards

• Your code must be syntactically correct. Any solution that contains a syntax error anywhere (and hence crashes when you hit the "play" button") will receive no credit.

If you run into any trouble here, please ask me or one of the TAs for help.

• Naturally, your code must be behaviorally correct, though partial credit is always given for partial solutions.

John H. E. Lasseter