## CS 2401 Test #1

Date: Wednesday, September 26, 2010.

Name: ___________________________________________________________________

1-2. A list of students in a class can be represented either as an array or as a vector. Let us assume that one of the students decided to drop the class; in this case, we need to delete this student's name from our list.

• For each of the two representations, write pieces of code for deleting a student with a given number from a list.
• Trace both pieces of code on the example of a list which initially consisted students A, B, F, and C, and we remove a student # 2 (i.e., student F) from this list. As a result, you should have a list consisting of three students A, B, and C.

```

```
3-5. When trying to program Fibonacci numbers, a student made two mistakes: used multiplication 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.
• 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 of every student.
• Write a method that, given a 2-D array g, returns a 1-D array a[s] of average grades.
• For example, if student 0 has grades 100 and 96, and student 1 had grades 80 and 84, then the array of averages should contain a[0] = (100 + 96) / 2 = 98 and a[1] = (80 + 84) / 2 = 82. Trace your method on this example.

```

```
7-8. A student is so happy with his straight A grades that she decides to print them in a nice way. Write a method that uses recursion to print the following pattern
```A

A A

A A A
```
up to a row with n A's.
• 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.