### COMP203 Midterm Exam

Name:
Please write your answers in the space provided. You are allowed a textbook, calculator and notes but you are not allowed to use a computer, cell phone, or other device capable of communication.

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. Write a procedure that accepts a number as input and then draws a hexagon whose side length equals that number.

Hint: We did this with squares and triangles at the beginning of the semester.

Solution:

```to hexagon :input
repeat 6 [forward :input right 60
end
```
2. Write a procedure that asks a person for their favorite musician and then uses the musican's last name in a sentence, as shown below. Punctuation counts!
```Who is your favorite musician?
K. D. Lang
Everybody loves Lang!
? musicianchat
Prince
Everybody loves Prince!
? musicianchat
John Lennon
Everybody loves Lennon!
```

Hint: There were some procedures like this in Chapter 1.

Solution:

```to musicianchat
print [Who is your favorite musician?]
print sentence [Everybody loves] word last readlist "!
end
```
3. Professor Burgiel wanted to draw a picture of a smiley face (below left).

When she ran her program, what she got was the picture above on the right. Find as many errors in the following two procedures as you can:
```to square :size
repeat 4 [forward 40 right 90]
end

to face
right 180 repeat 180 [forward 1 right 1] ; mouth
penup forward 50 pendown square 40       ; right eye
penup right 90 forward 80 square 40      ; left eye
penup left 90 forward 10 left 90 forward 20 pendown
square 20                                ; nose
end
```

Hint: Go through the procedure step by step, drawing what the turtle draws at each step.

Solution:

```to face
right 180 repeat 180 [forward 1 right 1] ; mouth
penup forward 50 pendown square 40       ; right eye
penup right 90 forward 80 left 90 pendown square 40      ; left eye
penup left 90 forward 10 left 90 forward 20 pendown
square 20                                ; nose
end

to square :size
repeat 4 [forward :size right 90]
end
```
4. Write a Logo operation that takes one input and outputs "false if the input is a list and "true otherwise.

Hint: See Chapter 4.

Solution:

```to notalistp :input
output not listp :input
end
```
5. Write a recursive procedure to keep asking the same question until the user gets the right answer.
```? keepquizzing
Who is the greatest musician of all time?
K. D. Lang
Who is the greatest musician of all time?
Michael Jackson
Who is the greatest musician of all time?
Billy Joel
Who is the greatest musician of all time?
John Lennon
Good job!
?
```
Hint: This is similar to the maze program we worked on in class.

Solution:

```to keepquizzing
print [Who is the greatest musician of all time?]
if equalp [John Lennon] readlist [print [Good job!] stop]
keepquizzing
end
```

Bonus: (5 points) Use a for loop to print the sequence:
```2
4
8
16
32
64
```

Hint: The students probably do not know how to raise a number to a power in Logo; that's why this is an extra credit problem. They will receive partial credit if they make a reasonable guess at how to exponentiate and get everything else right.

Solution:

`for [i 1 6] [print power 2 :i]`