### COMP203 Midterm Exam

Name:
Please submit your answers in a .txt document on the Blackboard site. There is an "assignment" link for this purpose in the Midterm section of the site. You may also submit a copy of this paper with your work handwritten, as a backup.

You are allowed a textbook, calculator and notes on this exam. You are also allowed to use a computer to run Logo, view Blackboard (including past homework assignments), your W: drive, the course web site (http://webhost.bridgew/edu/hburgiel/COMP203), the textbook (http://www.cs.berkeley.edu/~bh/v1-toc2.html) and the user manual (http://www.cs.berkeley.edu/~bh/usermanual).

You are not permitted to use other computer software such as a chat client, email, or Facebook.

If you get stuck on a problem, leave it blank and return to it later. You will receive partial credit for code with minor errors, but please check your work if you have time.

1. Is the procedure flower below a command or an operation? Justify your answer.
to flower
repeat 5 [repeat 36 [forward 10 right 10]~
right 72]
end

This is a command. It has no output, and it displays a picture of a flower on the screen.

2. Write a procedure that accepts a number as input and then draws a pentagon whose side length equals that number.

to pentagon :size
repeat 5 [forward :size right 72]
end

3. Write a predicate longp that takes a list as input and outputs "false if the list contains more than three items and "true otherwise. (Hint: use count.)

to longp :mylist
ifelse greaterp (count :mylist) 3 [output "false] [output "true]
end

4. Write a procedure counter that accepts a number as input and uses while to count down from that number to 0.
? counter 5
5
4
3
2
1
0
? counter 3
3
2
1
0
?

to counter :num
while not (lessp :num 0) [print :num   make "num :num-1]
end

5. Professor Burgiel wants a program to write more bad poetry, but the one below doesn't work. Find and correct as many errors as you can.
to story
local "verb
local "noun
local "otherverb
getwords
ifelse equalp (last :noun) "s [make "otherverb "are] [make "otherverb "is]
print (sentence :noun :otherverb word :adjective ", ~
[but still can] word :verb "!)
end

to getwords
print [Type in a noun.]
print [Type in a verb.]
end

If the procedure runs correctly it should behave as shown below.
? story
Type in a noun, singular or plural.
cat
Type in a verb.
hunt
sly
cat is sly, but still can hunt!
? story
Type in a noun, singular or plural.
trees
Type in a verb.
grow