### COMP203 Problems, Chapter 3

These pages are BSU's "questions at the end of the chapter" for Computer Science Logo Style Volume 1. Homework assignments will consist of subsets of these problems. Some of these problems may appear on the midterm.
1. Without using Logo, determine what the following instructions do and why. If the instruction gives an error message, just write ERROR. When you have finished, check your work using Logo.
• print first "GoBears!
• print first [Go Bears!]
• print butfirst "GoBears!
• print butfirst [How about those Bears?]
• print last "GoBears!
• print last [Go Bears!]
• print butlast "GoBears!
• print butlast [How about those Bears?]
• print butlast [GoBears!]
• print item 2 [Go, Bears, Go!]
• first butfirst "odd
• print word "have "Conversation
• print (word "a "& "b)

2. What do the instructions:
```print count "octopus
print count [King of Hearts]
```
do and why? You may explore this question using your textbook, the Logo manual, the help command, and/or Logo itself. Answer in complete sentences and explain your answer fully using words like "list", "word" and "item".

3. What does the instruction
`print (word (item 2 "octopus) (first "octopus) (first butfirst "odd) (first "even))`
do and why? Explain your answer by giving the input and output of each procedure in the instruction. Draw a plumbing diagram to illustrate your answer.

4. What does the instruction
`print item (count "octopus) "lizabeth`
do and why? Explain your answer by giving the input and output of each procedure in the instruction. Draw a plumbing diagram to illustrate your answer.

5. Write a procedure square that accepts a number (say, :size) as input and draws a square with that side length.
6. Write a procedure picture that uses your square procedure to draw squares of different size that together make up an original picture, different from these examples: (example 1, example 2).
7. Write a Logo procedure named convert that accepts as input a two digit word describing a number in base 2 (00, 01, 10, or 11) and outputs the value of that number.

8. Which set of instructions below will print the word Hello on the screen without giving an error message? Choose an answer first, then check your answer using Logo.

1. ```erall
to Hi
output "Hello
end
print "Hi
```

2. ```erall
to Hi
output "Hello
end
output Hi
```

3. ```erall
to Hi
print "Hello
end
print "Hi
```

4. ```erall
to Hi
output "Hello
end
print Hi
```

5. ```erall
to Hi
print Hello
end
print "Hi
```

6. ```erall
to Hi
print "Hello
end
output Hi
```

9. For each set of instructions above that does not print the word Hello, briefly explain what the instructions do. For the first set you might write "The instructions define the procedure Hi and then print the word Hi."

10. Write a Logo procedure negate that takes :number as input and outputs negative one times the number, as shown below.
```?  negate -2
2
```
Test the procedure on numbers other than -2.
11. Write a Logo procedure printPerimeter that takes two inputs :length and :width and prints the perimeter of a rectangle with length :length and width :width. Make sure that if you type
`printPerimeter 4 3`

you'll get

`14`

. Test the procedure with other numbers as input as well.

12. Write a Logo procedure outputPerimeter that takes two inputs :length and :width and outputs the perimeter of a rectangle with length :length and width :width. Make sure that if you type
`print outputPerimeter 4 3`

you'll get

`14`

. Test the procedure with other numbers as input as well.

13. Write a Logo procedure happyGreeting that takes an input :name and prints Hello followed by the value :name followed by an exclamation point (with no space before the exclamation point). Make sure that if you type
`happyGreeting "Laura`

you'll get

`Hello, Laura!`

. Test the procedure with other names as input as well.

14. In your own words, explain what a global variable is.

15. The procedure bottom on page 50 of the text uses a variable outer that is not declared as local to the procedure, nor is it an input to the procedure. Does the variable outer in procedure bottom violate the prohibition on page 56 against using global variables? Why or why not?

16. A classmate wrote the following procedure, which draws a beautiful cube. Write a helper procedure, and rewrite drawCube as drawCube2, which eliminates dupication of instructions in drawCube by calling your helper procedure.
```to drawCube
repeat 4 [forward 50 right 90]
forward 50
right 45 forward 25
right 45
forward 50
right 135
forward 25
left 45 forward 50
left 135
forward 25
left 45 forward 50
end
```

17. A classmate wrote the following procedure, which draws nested triangles. Write a helper procedure that takes one input, and rewrite triangle as triangle2, which eliminates dupication of instructions in triangle by calling your helper procedure. Use repeat as necessary.
```to triangle
rt 30
forward 100
rt 120
forward 100
rt 120
forward 100
rt 120
forward 50
rt 120
forward 50
rt 120
forward 50
end
```
Challenge problems

1. Write the Logo procedure conj described on page 46.

2. Write the Logo procedure sc1 described on page 54.

3. Your procedure sc1 was designed to "take advantage of dynamic scope". Does this violate the prohibition on page 56 against using global variables? Why or why not?

4. It is hard to tell by looking at sc1 what it is supposed to do. It might also be hard to use sc1 as a subprocedure in a different program. Instead of relying upon dynamic scope to determine the values of :verb and :endings, one could instead require that those values be input to the procedure, as below.
```to superconj2 :verb :endings
sc2 "je :verb 1 :endings
sc2 "tu :verb 2 :endings
sc2 "il :verb 3 :endings
sc2 "nous :verb 4 :endings
sc2 "vous :verb 5 :endings
sc2 "elles :verb 6 :endings
end

to sc2 :who :verb :what :endings
print sentence :who word butlast butlast :verb item :what :endings
end
```
Type in and test the procedures superconj1 and superconj2 which use sc1 and sc2 respectively. Which do you prefer, the sc1 procedure which relies on dynamic scope or the sc2 procedure, which has twice as many inputs but for which all variables are local? Give reasons for your preference.

5. What question would use to test students on ideas in this chapter? Give a sample solution. If you would like your name associated with it if it is used as a homework or exam question, please say so.