Friday, July 31, 2009

C++ finding limits of a sorted array?

How would you go about finding the highest and lowest scores in an array that is already sorted? I know how to find the highest and lowest scores when it is not sorted, but if it is sorted there must be a simpler way to find the limits besides searching the entire array. Here is the problem:





"findlimits() which will find the highest SAT score and the lowest SAT score and return both to the main program using reference parameters. Note that this is extremely easy once the score array has been sorted. The findlimits() function receives four parameters, the array scores, the number of values in the array n, and reference parameters max and min. After returning to main, main should print the high and low scores together with messages."





Why is it easy once the array is sorted, that means it must be a simple way. And how would I do that???

C++ finding limits of a sorted array?
I might be overlooking something, but if you know the array is of length n, and it is sorted in ascending order, then





int min = array[0];


int max = array[n-1];
Reply:There are 2 ways to go about it. One way is to use the min %26amp; max variables to store the highest %26amp; lowest values during the sorting routine.





(This is the best way.)


Another way is to store the 1st byte and the last byte from the array from outside or within the sorting routine. You need to know the length of the array though.


No comments:

Post a Comment