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:


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

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

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


Note: You should approach a problem like this by first "playing computer", that is following the program mechanically just as the computer would. If you follow all the steps the same way the computer would, you would get the same result. Of course, it is also OK to think! For example, after following the individual steps the first time through the loop, you should realize that the first three lines in the loop draw an equilateral triangle and leave the turtle back at its original postion and heading. Since the value of size increases each time through the loop, the triangles get bigger. Since the loop ends when size = 5, five triangles are drawn.

[by David Eck]