Homework Six


Due Friday, 13 July 2001 at 8:00AM


Objectives

Warning: Failure to use a do loop where instructed will result in NO credit.



Problem 1
10 points

Write a function called my-expt which mimics the functionality of the built in function expt. You may make the simplifying assumption that you will only have positive integer exponents. You must use a do loop in your solution. For sample data you can compare your function's output to the output from the built in function.

Problem 2
10 points

Write a function called fib-term which will take in an integer and will return that term in the Fibonacci sequence. Once again, you must use a do loop in your solution. If you don't remember what the Fibonacci sequence is then check back to the lecture slides. For example:

> (fib-term 1)
1
> (fib-term 2)
1
> (fib-term 3)
2
> (fib-term 4)
3

Problem 3
15 points

Write a function called fib-list which will take in a number (let's call the number 'n'). Return a list of length 'n' which contains the first 'n' terms in the Fibonacci sequence. You must use a do loop in your solution. For example:

> (fib-list 0)
()
> (fib-list 1)
(1)
> (fib-list 5)
(1 1 2 3 5)

Problem 4
15 points

Now write a function called fib-vector which will work like fib-list did, but instead returns a vector of the Fibonacci numbers. You may not use a do loop in your solution. You must use recursion. Example usage:

> (fib-vector 0)
#0()
> (fib-vector 3)
#3(1 1 2)

Problem 5
25 points

Write a function called bubble-sort which will take in a vector of numbers, sort that vector in place using the bubble-sort algorithm from homework five, and return a reference to the vector. Sample usage:

> (define my-vect #(8 2 3 1 9 4))
> (bubble-sort my-vect)
#6(1 2 3 4 8 9)
> my-vect
#6(1 2 3 4 8 9)
HINT:You may want to abstract out a swap and bubble-pass function.

Problem 6
10 points

Give one advantage and one disadvantage of pass-by-value. Give one advantage and one disadvantage of pass-by-reference. For full credit you must explain your answers thoroughly.

Problem 7
15 points

Write a function called product-rect which will take in two parameters, both numbers, and return a two dimensional vector (ie, a vector of vectors). The first number passed in will be the number of "columns" and the second number will be the number of "rows." Each inner vector should represent a "row" in the rectangle. Every row's element should be the product of the element's row and column inside the 2d vector. Remember that vector indices begin at zero.

> (product-rect 1 1)
#1(#1(0))
> (product-rect 2 3)
#3(#2(0)
   #2(0 1)
   #2(0 2))
> (product-rect 5 3)
#3(#5(0)
   #5(0 1 2 3 4)
   #5(0 2 4 6 8))