CS 2401 Test #1

Date: Thursday, February 25, 2010.

Name: ___________________________________________________________________

1-2. Write a recursive method for computing the product 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
up to a row with n X's 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 average gas price on this day. For example, if prices = {{2.50, 2.60}, {2.40, 2.54}}, meaning that on Day 0, the prices were 2.50 and 2.60, and on Day 1, the prices were 2.40 and 2.54, and then the average price on Day 1 was (2.40 + 2.54) / 2 = 2.47. Trace your method on the example of this 2-D array.

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