CS 2401 Test #1

Date: Wednesday, February 24, 2010.

Name: ___________________________________________________________________

1-2. Write a recursive method for computing the sum of 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, and -3.
















































3-4. Write two methods for drawing the following sequence of symbols
+ + +
+ +
+
starting with a row with n pluses separated by blank spaces. 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 gas prices at different El Paso gas stations in different days, and the ordinal number of the day, and computes the lowest gas price on this day. For example, if prices = {{2.50, 2.60}, {2.40, 2.55}}, meaning that on Day 0, the prices were 2.50 and 2.60, and on Day 1, the prices were 2.40 and 2.55, and then the lowest price on Day 1 was the smallest of 2.40 and 2.55, i.e., 2.40. Trace your method on the example of this 2-D array.























































7. Write a method that, given a vector of integers, returns their product. For example, for the vector [6 7], your method should return 6 * 7 = 42. 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.