COMP203 Problems, Chapter 7


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. Define the following recursive Logo procedure:
    to countdown :number
     print :number
     countdown :number-1
     if equalp :number 0 [stop]
    end
    
    This procedure is supposed to look like this when run:
    ? countdown 5
    5
    4
    3
    2
    1
    0
    ?
    
    Try it -- it doesn't work! Use the Logo menu to stop the procedure.

    Why doesn't the procedure work?

  2. Edit the countdown procedure so that it works as intended.

  3. Write a recursive procedure sumup that takes a positive integer n as input and outputs the sum 1 + 2 + 3 + ... + n. You may find it helpful to review the initials.recursive procedure from Lecture 13.

  4. Write a procedure countup that does the opposite of procedure countdown, as shown below. (The example at the top of page 140 in chapter 7 may or may not be helpful.)
    ? countup 5
    0
    1
    2
    3
    4
    5
    ?
    

  5. Write a procedure slant which takes a word as input and produces the output described on page 161.

  6. Fibonacci numbers are defined on page 96. Write a recursive procedure recursefib that accepts a positive integer n as input and outputs Fn. You might use a stop condition that looks like:
     if :num = 0 [output 1]
     if :num = 1 [output 1]
    
    For more information on special stop rules, see p. 165. Recursive operations are ones which output a value; these are described in more detail on pages 202-203.

  7. Compare your recursefib to the procedure fib defined on page 97. Be sure to test both procedures on inputs like 23, 25 and -1. Which procedure works better? List a few significant differences between the procedures (I'll be looking for 3 key differences when grading.)

  8. Study the example procedure inout on page 144 and use what you learn to write a recursive procedure recursepower that works as well as the power procedure defined on page 96. Write an instruction or a procedure that prints the multiples of 12 from 12 thru 144. Use iteration.

  9. In Assignment 3 we eliminated duplication of code in a procedure by writing a helper procedure that takes one input, using repeat to further streamline the code:
    to TwinTriangles
    right 30 
    triangle 100 
    triangle 50
    end
    
    to triangle :sidelength
    repeat 3 [forward :sidelength right 120]
    end
    
    A couple of classmates pointed out that you can get a cool design with
    repeat 12 [nestedTriangles]
    . In this exercise, rewrite nestedTriangles as nestedTriangles2, a procedure that takes an input. You should make it so that
    nestedTriangles2 100
    does the same thing as TwinTriangles. Next write a procedure that calls TwinTriangles2 twelve times from within a for or while loop, making each set of nested triangles a little smaller than the one before. The pattern will look somewhat like a snail shell.

  10. A rock is thrown upward from the surface of Mars with an initial velocity of 10 m/s. Its height t seconds later is s(t) = 10 t - 1.86t^2 m. How high is the rock at times 0, 1, 2, 3, 4, 5, and 6 seconds? Write a procedure that shows the height of the rock at times 0, 1, 2, 3, 4, 5, and 6 seconds. Use a for loop, and use the operation position that you wrote in Assignment 4 as a helper procedure.

  11. Write a recursive procedure that prints Type in a number. If the user types in something other a number, the user must again be asked Type in a number. until the user eventually types in a number. At that point, the program can do anything of your choosing. (One possibility is to have it simply print Thank you.) Hint: use numberp.
Challenge problems
  1. Write a recursive procedure triangleview that draws a collection of nested triangles similar to those shown below.

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