- Open file and read line by line assuming that the each number is on a separate line.
- Convert the textual representation to a number. The number format to choose depends on you file content (long, int, float or double).
- After successful conversion add the number to a list of the corresponding type: e.g.
List<double>
- Continue reading lines from your file until there are no more left
- Sort the
List<double> numbers
via numbers.Sort()
. Since numbers already implement the IComparable
interface the list will be sorted in ascending order. - 100th least element will be at numbers[99] and the 100th highest element will be at numbers[numbers.Count - 99].
- Done!
Since the task was to find the least and highest number you'd also need to make sure that you only add a number to the list if it
Contains
it not. Depending on the content of your file this may very well leave you with less than a hundred numbers. In that case there is no solution to the given question. To also avoid the paradoxon that the n
th highest may be smaller than the n
th least you'd even have to check that there are at least
2n elements in the list. In your case the list should thus be at least 200 elements long. In case the paradox doesn't really matter you should still output a warning with your result.
Best Regards,
—MRB