## CPSC 100, Spring 1997: Answers to Quiz #5

This is the fifth quiz from the course Computer Science 100: Principles of Computer Science, taught by David Eck. The answers that are included here are sample answers only. There might be many other answers to a given problem that would also receive full credit. See the home page for the text, The Most Complex Machine, for more information about the course.

Question 1: Explain the terms state and process, as they relate to computers and programming.

Answer: As the computer executes a program, it goes through a series of states. A state consists of all information that is relevant to running a program, at a particular instant during the execution of that program. For example, the state includes the values of all the variables used in the program. It includes an indication of which statement in the program is being executed. And in xTurtle, it includes the position and heading of the turtle. A process consists of the sequence of states that the computer goes through as it executes the program. The program is a kind of script for the process, but a process is dynamic -- it is something that takes place over a period of time.

Question 2: What is a variable ? Give as full an answer as possible.

Answer: A variable is a named memory location that can store a value. Since a variable in a program is referred to by name, the programmer doesn't have to worry about the address of the memory location. When the variable is used in the program, it can refer either to the memory location itself or to the value stored in that memory location.

Question 3: Draw the picture that would be produced by the following xTurtle program:

```          DECLARE count
count := 0
LOOP
forward(5) turn(90)
forward(1) back(2)
forward(1) turn(-90)
back(5) turn(45)
count := count + 1
EXIT IF count = 5
END LOOP
```

Answer:Each repetitition of the loop draws a "T" shaped figure. Five such figures fan out from the turtle's starting position, separated by angles of 45 degrees. (At the end, the turtle is back where it started from, at a heading of 225 degrees.)

Question 4: Write an xTurtle program that will draw the following picture. The picture consists of ten identical squares. Use a loop.

Answer:There are many possible answers. Here is a typical program, with comments to explain what it does:

```        DECLARE count   { A variable to count the number
of squares that have been drawn. }

count := 0      { Start counting with zero squares. }

LOOP  { A loop to draw one square. }

forward(1) turn(90)    { Draw the square. }
forward(1) turn(90)
forward(1) turn(90)
forward(1) turn(90)

{ Turtle is now back at the point where it started
drawing the square.  It is at the lower LEFT
corner of the square. }

PenUp        { Move to lower left corner of NEXT Square. }
forward(2)
PenDown

count := count + 1  { Count the square that has just been drawn. }

EXIT if count = 10  { Exit if all ten squares have been drawn. }

END LOOP
```

David Eck, 16 May 1997