Week 11: Exercises

1. Filtering an Array

Write a method 'filter' that takes an array a of integers and a delegate f that maps an integer to a bool. The method should return an array containing only the integers i in a for which f(i) is true.

2. Filtering an Array II

Generalize the previous method so that it will work with any kind of array.

3. Filtering a Linked List

Repeat the previous exercise, using a linked list of any type T and a delegate that maps type T to a bool.

4. Filtering a Linked List II

Repeat the previous exercise, modifying the linked list in place rather than returning a new linked list.

5. Approximate Zero

Write a method that finds an approximate zero of a continuous function using a binary search. The method should take a delegate f that maps a double to a double, plus two start values x1 and x2 such that f(x1) < 0 and f(x2) > 0. Return a value y such that |f(y)| < 1 / 1,000,000.

6. Zip

Write a recursive method 'zip' that takes two linked lists (possibly of different types) and returns a linked list of pairs of values (using a generic struct Pair). Each pair should contain one value from each input list. The number of pairs should equal the minimum of the lengths of the input lists.

7. Random Lines

Write a Windows Forms application that draws 100 random lines between random points, each with a random color.