CS 2401 Test #1

Date: Thursday, February 26, 2009.

Name: ___________________________________________________________________

1-2. Write a recursive method for computing the sum of squares Sn = 12 + 22 + 32 + ... + n2. Explain first how you go from the case n-1 to the case n, then transform this algorithm to code, and trace your code on the example of n = 3.

3-4. The genetic code of all leaving creatures is stored in DNA, a long molecule coded by letters A, T, C, and G. When two molecules get together, A pairs with T, and C pairs with G. In the analysis of the molecules, it is very important to find palindrome-type sequences, in which the first letter pairs with the last one, the second letter pairs with the second-from-last one, etc. For example, in the sequence ACAGT, the first letter A pairs with the last letter T, and the second letter C pairs with the second-from-last letter G. Write a method for checking whether a given sequence has this palindrome-type property.

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

6. Write a method that takes a 2-dimensional array of temperatures at different weather stations in different days, and the ordinal number of the station, and computes the average temperature measured by this station. For example, if temp = {{60, 70}, {65, 75}}, then the average temperature measured by station 1 is (65 + 75) / 2 =70. Trace your method on the example of this 2-D array.

7. Write a method that, given a vector of integers, returns the average value of these integers. For example, for the vector [65 75], it should return (65 + 75) / 2. Hint: 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.

9. Use your name and the names of your friends to form an enumeration type (fictitious friends are OK). Write a code for returning the name of your friend No. 1.

10. Write a method that, given an array of real numbers, returns the largest of these numbers. Feel free to use a for-loop or a recursive method. For extra credit, do both.