## CS 2401 Test #1

Date: Thursday, September 27, 2010.

Name: ___________________________________________________________________

1-2. A list of temperatures recorded at different locations in El Paso can be represented either as an array or as a vector. These temperatures are recorded in the usual US units: degrees Fahrenheit. To supplement their own measurements, our Cd. Juarez colleagues would like to use these temperatures, but they would like to have them in Centigrade (Celsius). The conversion formulas is easy:
tC = (5/9) * (tF - 32).

• Both for array and vector representations, write pieces of code for transforming all the temperatures in the list from Fahrenheit to Celsius.
• Trace both pieces of code on the example of a list which initially consisted of temperatures 50, 68, and 77. As a result, you should get temperatures 10, 20, and 25.
• In general, what are advantages and disadvantages of using vectors instead of arrays?

```

```
3-5. When trying to program Fibonacci numbers, a student made two mistakes: used subtraction instead of addition, and used a wrong initial value n(1) = 2 instead of n(1) = 1. As a result, he got a sequence of numbers n(0), n(1), ..., for which n(0) = 1, n(1) = 2, and n(i) = n(i-1) - n(i-2) for all other i > 0. Since the numbers are unusual, it is not easy for a TA to check the correctness of the student's code.
• To help your TA, write a recursive method for computing this new sequence n(i).
• Trace your method on the example of computing n(4).
• Write an iterative method for computing n(i) and trace it too.
By comparing these two methods, what can you say about the advantages and disadvantages of recursive versus non-recursive techniques?

```

```
6. In a 2-D array, let g[s][t] represent the grade of student s on test t. Based on this input, we need to find the average grade on each test.
• Write a method that, given a 2-D array g, returns a 1-D array a[t] of average grades.

• For example, if student 0 has grades 100 and 96, and student 1 had grades 80 and 82, then the array of averages should contain a[0] = (100 + 80) / 2 = 90 and a[1] = (96 + 82) / 2 = 89. Trace your method on this example.

```

```
7-8. A student decided to earn some money by designing a UTEP-related pattern for T-shirts. Write a method that uses recursion to print the following pattern
```UTEP
UTEP UTEP
UTEP UTEP UTEP
```
up to a row with UTEP repeated n times.
• First, describe an algorithm.
• Then transform your algorithm into a code.
• Trace this code on the example of n = 2.

```

```
9. What are advantages and disadvantages of using enumerated types as opposed to a list of strings? Give examples.