Exercises in this lecture   previous -- Keyboard shortcut: 'p'        Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 5.16
Finding and sorting elements in an array


Make an array of Point objects. You can, for instance, use this version of class Point. You can also use a version that you wrote as solution in one of the previous exercises.

Use the method System.Array.Find to locate the first point in the array that satisfies the following:

The sum of the x and y coordinates are (very close to) zero

The solution involves the programming of an appropriate delegate in C#.

Next use the method System.Array.FindAll to locate all such points.

Finally, sort the list of points by use of the static method System.Array.Sort<Point>(Point[], Comparison<Point>). Point is a type parameter of Sort. You do not need to care much about it in this exercise. We will come back to type parameters in a later lecture. Comparison<Point> is a delegate that compares two points, say p1 and p2. Use a comparison for which

   p1 <= p2 if and only if p1.X + p1.Y <= p2.X + p2.Y

Look up the documentation of Sort and Comparison to find the necessary information.