CS 2401 Test #1

Date: Friday, October 2, 2009.

Name: ___________________________________________________________________

1-2. Write a recursive method for computing the smallest element of a given array of real numbers. First, explain your algorithm, then transform this algorithm to code, and trace your code on the example of numbers 2.5, 7.2, 2.1, and 3.0.
















































3-4. Write two methods for drawing the following sequence of symbols
***
**
*
all the way to a row with n symbols. Your first method should use the for-loop, your second method should use recursion.




















































5. Show the sequence of moves that solves the Hanoi tower problem for n = 3.


























































6. Write a method that takes a 2-dimensional array of temperatures in different cities in different days, and the ordinal number of the day, and computes the highest temperature measured on that day. Assume that each row corresponds to a different city. For example, if temp = {{60, 70}, {65, 75}}, then the highest temperature measured on day 1 is the largest of 70 and 75, i.e., 75. Trace your method on the example of this 2-D array.























































7. Write a method that, given a vector of integers, returns the product of these integers. For example, for the vector [2 3], your method should return 2 * 3 = 6. Use vector-oriented for-loop to compute the product.


























































8. Describe advantages and disadvantages of recursion vs. a more traditional method of solving a problem, from the viewpoint of easiness to write, easiness to understand, and running time.