Programming in BlooP

Due by class time Tuesday, February 28

Instructions: Please write out your program definitions on paper and bring them with you to class on Tuesday. We will go over the solutions together in class. If you wish, you may work together with another student on this assignment. Use the example BlooP programs on pages 410-413 of GEB as a guide. Also, if you wish, you may try out your solutions using the BlooP interpreter that I demonstrated in class today, which is available on our class web page under Notes.

  1. Write a BlooP program called RIGHT-TRIANGLE? that takes three numbers A, B, and C as input and checks whether or not A2 + B2 = C2. If they satisfy this formula, then the program should output YES, otherwise it should output NO. For example, calling (RIGHT-TRIANGLE? 3 4 5) should output YES, but calling (RIGHT-TRIANGLE? 1 2 3) should output NO. Here is the basic skeleton to get you started:

    DEFINE PROCEDURE ''RIGHT-TRIANGLE?'' [A, B, C]:
    BLOCK 0: BEGIN
       ...
    BLOCK 0: END.
    

    Hint: Because the name of this program ends with ?, the variable OUTPUT automatically gets initialized to NO when the program starts.

  2. Write a BlooP program called ADDUP-ODDS that takes a number N as input and adds up the first N odd numbers. For example, calling ADDUP-ODDS with 4 should return the sum 1 + 3 + 5 + 7, which equals 16. Here is the basic skeleton to get you started:

    DEFINE PROCEDURE ''ADDUP-ODDS'' [N]:
    BLOCK 0: BEGIN
       ...
    BLOCK 0: END.
    

    Hint: your program will need to use a LOOP statement that executes exactly N times. You can use the variable OUTPUT to keep track of the running total, and the variable CELL(0) to keep track of the current odd number being added. Thus your program should initialize CELL(0) to 1, the first odd number, and then increase it by 2 each time around the loop. Because the name of this program does not end with ?, the OUTPUT variable automatically gets initialized to 0 when the program starts.