## CPSC 100, Fall 1995, Quiz #5

This is Quiz #5 from CPSC 100: Principles of Computer Science, Fall 1995. The answers given would receive full credit, but in many cases, there are variations or even completely different answers that would also receive full credit. The "Notes" are meant to clarify the answer or to provide some interesting extra information.

Question 1: Carefully explain what the statement "declare x,y" means in a program, and explain what the computer does when it executes this statement.

Answer: This statement just declares that x and y are variables that will be available for use later in the program. When the computer executes this statement, it sets aside two memory locations to hold the values of x and y, and it associates the names "x" and "y" with those memory locations. Then, you can use the names x and y in statements such as "x := y+17" and "forward(x)". Without the declare command, these statements would produce an "undeclared variable" error.

Question 2: We can make an analogy, "A program is to a process as a script is to a movie." Define the term process, and explain the analogy in more detail.

Answer: A process is essentially a running program. More formally, it is defined as the sequence of states the computer goes through as it executes the program. The program is a kind of script for the process. That is, a program is a static object, like a movie script, and the process is like the movie in the sense that it is what happens as the script plays itself out over time.

Question 3: Write a sequence of xTurtle commands that will draw an X-shaped figure like this:

Answer: Assume that the turtle starts at the lower left corner, facing to the right. Then the following commands will draw an X:

turn(45)
forward(10)
back(5)
turn(90)
forward(5)
back(10)

Note: It was not specified how big the X should be, where the turtle should start, or where it should end up, so there are many possible answers. Here are three more, which all draw an X the same size as the one produced by the above program. The first program begins at the lower left corner of the X; the other two start at the center of the X.

{assume is at (0,0)}      turn(45)           declare ct
Moveto(10,10)             forward(5)         ct := 0
PenUp                     back(5)            turn(45)
MoveTo(0,10)              turn(90)           LOOP
PenDown                   forward(5)            forward(5)
MoveTo(10,0)              back(5)               back(5)
turn(90)              turn(90)
forward(5)            ct := ct + 1
back(5)               EXIT if ct = 4
turn(45)           END LOOP
forward(5)
back(5)
turn(90)

Question 4: Draw the figure that would be produced by the following xTurtle program:

DECLARE size
size := 1
LOOP
forward(size) turn(120)
forward(size) turn(120)
forward(size) turn(120)
EXIT IF size = 5
size := size + 1
END LOOP