### COMP203 Problems, Chapter 4

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. Write an operation position that takes t as an input. If t is less than 5.38, your operation should OUTPUT 10t - 1.86t^2. If t is greater than or equal to 5.38, you should output 0. Test it to confirm that it runs as follows:
```? print position 2
12.56
? print position 6
0```

You may wish to run additional test cases.

2. Write a procedure that prints Type in a number, lets the user type a number, and prints a sentence stating what the absolute value of the number is. Notice that while the user only types in the number once, your procedure will need to use that number twice. Test your procedure to confirm that it runs as follows:
```? Type in a number.
-7
The absolute value of -7 is 7.
```

For full credit the punctuation should appear exactly as above. You may wish to run additional test cases.

3. (Practice with local and make.) As described in the last two sections of Chapter 4, write a quiz program that asks three or more questions. When the quiz is completed, print the number of questions answered correctly. You will probably want to use the local and make commands to create and modify a variable that stores the score.

4. Write an operation absolute that takes :num as an input and OUTPUTS the absolute value of the number. Your answer should be different from the solution in the textbook (do not use ifelse as an operation).

Test your procedure to confirm that it runs as follows:

```? print absolute -2
2
? print absolute 6
6```

You may wish to run additional test cases.

5. Write a predicate oddp that takes :num as an input and OUTPUTS true if the input is odd and false if the input is not odd. (Hint: use the operation remainder; see Chapter 1 problem 5.)

Test your procedure to confirm that it runs as follows:

```? print oddp 3
true
? print oddp 6
false```

You may wish to run additional test cases.

6. a) Read the section on Defining Your Own Predicates on page 64 of the text, then create a predicate smallp which takes a single number as input and outputs true if the number is less than 1 and false if the number is greater than or equal to 1. Test your procedure with the instruction print smallp 0.

b) What happens when you print smallp zero? Why? What happens when you print smallp "zero? Why? (Note: this a question about error messages, not about predicates. It also serves as an introduction to and motivation for part (c).)

c) Use the primitive procedures numberp and ifelse to create a new procedure smallnumberp that takes one input and outputs true if the input is a number less than 1, false if the output is a number greater than or equal to 1, and which prints a warning and outputs false if the input is not a number. (If you get stuck on this problem, take a break and work on the next one.)

7. Give a careful explanation for what the following procedure does and why:
```to ticketPrice :age
ifelse :age < 13 [output 0][ifelse :age >= 55 [output 3][output 7]]
end
```
Use complete sentences. Draw an informal flow chart to provide visual support for your explanation. If you wish, you may submit the flow chart on paper in class. Support your explanation with sample runs of the procedure for representative choices of inputs.

Vocabulary point: The procedure uses "nested conditionals" because conditional execution occurs within conditional execution.

8. Write a procedure to simulate the rolling of a pair of six-sided dice. The procedure will involve generating two random numbers between one and six. If the computer generates the same number twice, display You got doubles! When you run the procedure, it should behave, for example, as follows:
```? rollDice
6
4
```
```? rollDice
3
3
You got doubles!
```
```? rollDice
5
2
```
If you wish, you may use wait to build suspense for each die roll.
Challenge problems
1. According to this manual, the post office will not ship packages whose length plus girth is more than 130 inches. Write a procedure that accepts the length, width and height of a package, computes the girth of the package, and prints a message stating whether it exceeds this size limit.
2. Revise your answer to the previous problem so that the message printed is correct even if the user mistakenly enters a "length" that's shorter than the height or width. (For example, a 10 by 40 by 40 box has a girth of 100 inches, but a poor choice of "length" of the box could lead us to calcuate a girth of 160 inches.)
3. Write a quiz program which gives the user a second chance if he or she answers the question incorrectly the first time. (Note that it's difficult to use the example in the book because stop ends the entire program, not just the procedure it occurs in.)

4. Write a procedure solveQuadratic that calls a helper procedure discriminant. (Make sure to include your helper procedure in your .txt file.) Write the code so that the following commands generate exactly the result indicated:
```? solveQuadratic 1 -1 -2
2
-1
1
1
0.5 + 0.5*i*sqrt 7
0.5 - 0.5*i*sqrt 7
?```

As you can see, our new task for this procedure is correctly displaying complex roots. For the test case above with complex roots, you should solve x^2 - x + 2 = 0 by hand to reason through what the roots should be. Notice that humans understand 0.5 + 0.5*i*sqrt 7 as a number, but we will have Logo construct it as a sentence. The first 0.5 comes from -b/(2a). The second 0.5 comes from 1/(2a). The square root of -1 is denoted i. The discriminant is negative, so instead of putting the discrimant after the word "sqrt (not the instruction sqrt), we switch the sign on the discriminant (to display 7 instead of -7).

You should run additional test cases to make sure your procedure is not tailored to the specific cases above and to get extra evidence that it works as it should for all cases with complex, real and different, and repeated roots. (You are not required to show these test cases.)

5. Logan airport parking pass program: Write a program to simulate the interaction with the pay-for-parking machine at Logan:
```Would you like a receipt for this transaction?  (Type "Y" or "N".)
Insert your parking ticket.  (Type "P" to simulate inserting your parking ticket.)
Thank you.
You owe \$12 for 4 hours of parking.
Insert your credit card. (Type "C" to simulate inserting your credit card.)
Thank you.  Remove your credit card.
```
Have it print the last line only if you pushed "yes" at the beginning. (In practice it gives you a receipt and tells you to take it regardless.)

Add an option to pay in cash, use a randomly generated number between 1 and 12 for the time parked. For bonus points, add a "help" screen; can you make the help screen come up whenever a user types "H" at a prompt then return to the appropriate point in the program?

It is important that this program be very reliable and easy to use. Spelling and grammar should be correct.

6. 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.