CS 2401 Test #1

Date: Thursday, October 1, 2009.

Name: ___________________________________________________________________

1-2. Write a recursive method for computing the largest element from a given array of integers. First, explain your algorithm, then transform this algorithm to code, and trace your code on the example of numbers 1, 7, -3, and 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 city, and computes the lowest temperature measured in this city. Assume that each row corresponds to a different city. For example, if temp = {{60, 70}, {65, 75}}, then the lowest temperature measured in city 1 is the smallest of 65 and 75, i.e., 65. Trace your method on the example of this 2-D array.























































7. Write a method that, given a vector of doubles (real numbers), returns the sum of these doubles. For example, for the vector [6.5 7.5], your method should return 6.5 + 7.5 = 14.0. Use vector-oriented for-loop to compute the sum.


























































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.