Homework Ten

From Scheme to C

Comp210

Due: 98.Nov.23 (Mon)

(But still accepted normally in class Nov.25)

You don't need to write templates if you don't want, but (as always) every function must have a contract, and (if it's not obvious from the name) a short description of what it does. A large component of the grade is based not so much on correctness, as on having a clearly organized, well thought out, straightforward solution. If you notice yourself repeating similar chunks of code, abstract over it!

Be sure to submit your test inputs and the resulting outputs for each problem.

  1. (4pts) For this problem use a global vector of numbers data, and placeholder N for its size (which you can presume is positive). That is, in Scheme, your solution would start off with
    (define data (vector 14 7 -3 17 1))
    (define N (vector-length data))
    
    1. Using Scheme, write a function with natural recursion (and local) which returns the maximum of data[0]...data[N-1].
    2. Convert this to a tail-recursive function, using an accumulator.
    3. Re-write the tail-recursive version of max in Scheme, using fori= and set!.
    4. Translate this imperative version into Jam2000 assembly language, including comments. (You should know how to further translate this into machine code (raw numbers), though the JAM inspector does this assembling for you automatically.)

  2. (3pts) Write a program which asks for a natural number n as input, and prints out (or in Scheme, returns) the sum of the first n cubes: n³ + (n-1)³ + ... + 2³ + 1³. Write this function in Scheme, and in C, based on the appropriate template. (You do not need to write an accumulator or imperative for-loop version.)

    (First write a function which calculates the cubes; getting the I/O working is less important. Assume that the user types in a valid number if prompted politely.)

  3. (3pts) Transliterate the solution to the jail-cell problem from Scheme into C. Change as little as possible -- keep corresponding functions, a globally-declared vector, a placeholder for the number of cells, etc. Use a C "for"-loop instead of the Scheme fori= (making any adjustments as necessary). However, instead of returning a list, the C program will just print the open cells' numbers.