CPSC 100, Fall 1997: Answers to Quiz #6

This is the sixth 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 term semantics.

Answer: The word "semantics" refers to meaning. Formally, the semantics of a language is the set of rules that determine the meaning of phrases or statements in that language. Sometimes, the word is used in the context of the semantics of a particular statement. In that case, it just refers to the meaning that statement. For example, in the xTurtle programming language, the semantics of the statement "forward(5)" is that when this statement is executed, the turtle will move forward 5 units along the direction in which it is currently facing.

Question 2: Define the term precondition as it applies to programs. Why might a programmer be interested in preconditions?

Answer: A precondition is a condition that has to hold true at a certain point in a program, if the execution of the program is to continue correctly from that point. For example, a precondition of the statement "x := sqrt(y)" is that y >= 0, since if y is less than zero, the program will crash. For programmers, preconditions are a tool for thinking about programs. When a programmer notices a precondition in a program, the programmer can check to make sure that the precondition is in fact true. If not, the programmer can add some statements to the program that will make the precondition true. A programmer who stays on the lookout for preconditions has a better chance of writing correct programs.

Question 3: A "process" is the sequence of states that a computer goes through as it executes a program. What, exactly, is a state?

Answer: A state consists of all the information in the computer that is relevant to the execution of the program at some particular instant of time. The state includes, for example, the current values of all the variables in the program and the value of the program counter (which tells the computer which instruction in the program is next to be executed). The idea is that the program could be halted at that instant, and the state could be saved. Later, if the state is restored, the program can continue on just as if no interruption has occurred.

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

```               DECLARE Len
Len := 5
LOOP
forward(Len)
Len := Len - 1
EXIT IF Len = 0
turn(120)
END LOOP
```

Answer: As the loop executes, the variable Len takes on the values 5, 4, 3, 2, and 1. Then, when Len becomes equal to zero, the loop ends. So the program draws five connected line segments of length 5, 4, 3, 2, and 1. The angle between one segment and the next is 120 degrees. The figure looks like a "triangular spiral." (Note that after three 120-degree turns, the turtle will return to its original heading; thus, the fourth line drawn, like the first, is horizontal.) Here is the picture:

Question 5: Write an xTurtle program that will draw the following picture. (Note that each of the four lines in the picture is the same length.)

Answer: There are many different possible answers. The one thing they have in common is that they all have to use the PenUp and PenDown commands in order to leave the "gaps" between the two pieces of the figure. Here are three answers, using different values for the length of the four lines:

```      { Starting from           { Starting from            { Starting from
lower left }              upper left }               right end of
gap at the top }
forward(5)                move(0,-3)
PenUp                     move(3,0)                  forward(10)
forward(1)                PenUp                      turn(-90)
PenDown                   move(1,0)                  forward(10)
turn(90)                  PenDown                    turn(-90)
forward(5)                move(0,3)                  PenUp
turn(90)                  move(-3,0)                 forward(2)
forward(5)                                           PenDown
PenUp                                                forward(10)
forward(1)                                           turn(-90)
PenDown                                              forward(10)
turn(90)
forward(5)

David Eck,
30 October 1997

```