COMP203 Problems, Chapter 5


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. Use the Logo command for to write a Logo instruction that lists the multiples of 5 between 0 and 85.

  2. Write a procedure named listn which accepts a positive integer, say n as input and outputs a list of the counting numbers from 1 to n. Hint: use make to create a variable which stores an empty list, then use for, make and sentence to add elements to that list.
    ? show listn 4
    [1 2 3 4]
    ?
    

  3. On page 82 of Chapter 5 there is an example that uses map to produce a list of initials given a name. Being able to perform the same operation on every member of a list can be extremely useful -- for instance, in the linear algebra midterm project you must apply a transformation to each point in a list of points.

    a) Write a procedure finals that uses map to output the last letter of each word in a list. The input to this procedure should be a list of words. The output should be a list of letters.

    b) The example of initials was carefully chosen so that we just needed to apply an existing Logo procedure (a primitive) to each item in a list. More commonly (e.g. for the linear algebra project) we'll need to write our own procedures for use with map.

    to double :number
     output product 2 :number
    end
    
    Use map and the procedure double defined above to write a procedure named double.list that takes one input (a list of numbers) and outputs a second list whose members two timse the members of the first list. For example:
    ? show double.list [1 2 3 4]
    [2 4 6 8]
    ?
    
    to double :number
     output product 2 :number
    end
    

  4. The procedure roll below simulates rolling a six sided die. Use while to write a procedure rollfor6 that uses roll to simulate rolling a die until the result is a 6.
    to roll
     output 1 + (random 6)
    end
    
    Running your procedure should look something like:
    ? rollfor6
    2
    5
    6
    ? rollfor6
    1
    1
    5
    3
    4
    1
    6
    ?
    

  5. Consider the procedure
    to triangle :sidelength
    repeat 3 [forward :sidelength right 120]
    end
    
    Rewrite triangle as triangle2 so that a for loop is used instead of repeat. Which structure do you prefer for drawing a triangle and why?

  6. Use for to write a procedure named nest that draws a set of nested polygons (e.g. squares or triangles). Use penup and pendown to make usre your polygons appear disconnected.

  7. The Logo procedures for and map are similar but not the same. Below are two simple procedures that take a name as input and output the initials in that name. Which do you think is better? Why?
    to initials.for :name 
     ; code copied from page 78 of Computer Science Logo Style
     local "result
     make "result []
     for [i 1 [count :name]] ~
         [make "result sentence :result first (item :i :name)]
     output :result
    end
    
    to initials.map :name
     output map "first :name
    end
    
  8. Below is a procedure that uses for to accept a list as input and output a new list in which the value of every item in the old list is doubled. Write a program that uses map to do this, or explain what goes wrong when you try to use map.
    to double.list :numlist
     local "newlist
     make "newlist []
     for [i 1 [count :numlist]]~
      [make "newlist (sentence :newlist (2 * (item :i :numlist)))]
     output :newlist
    end
    

  9. Consider the procedure
    to triangle :sidelength
    repeat 3 [forward :sidelength right 120]
    end
    
    Rewrite triangle as triangle2 so that a for loop is used instead of repeat. Which structure do you prefer for drawing a triangle and why?

  10. In Assignment 3 we eliminated duplication of code in a nested-triangles procedure by writing a helper procedure that takes one input, using repeat to further streamline the code:
    to nestedTriangles
    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 nestedTriangles. Next write a procedure that calls nestedTriangles2 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.

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

  12. Write a 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.) Notice this procedure will need numberp and while.

  13. Give a careful explanation for what the following instruction does and why:
    for [i 1 12][for [j 1 :i] [type :j] print []]
    Explicitly state what the execution block is when i = 1, when i = 2, when i = 3, etc.

    Vocabulary point: This instruction is called a "nested for loop" because it contains a for loop within a for loop.

  14. Write a procedure oddsum that accepts a number n as input and OUTPUTS the sum of the first n odd numbers. Test it to confirm that it runs appropriately, for example:
    ? print oddsum 3
    9
    
Challenge problems
  1. Write an operation initials.while that does the same thing as initials.for and initials.map, but uses while in stead of for or map.

  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.