max value count of an array

I have an assignment to print out an 8 by 8 matrix and show the max and min values. I also have to state how many times the max and min both occur, this is the part I'm having trouble with. Here's my code so far:
Last edited on
Your code sorts the array but you didn't mention that as part of the assignment. If you don't need to sort the array then don't do it since it takes a lot of time.

Here is some pseudo-code for finding the max value and counting the number of occurances. You can use similar code for the min value:

1
2
3
4
5
6
7
8
9
10
11
12
13
int max = array[0][0];    // assume first element is max
int maxCount = 1;         // it occurs once so far

for (int i=0; i<8; ++i) {
    for (int j=0; j<8; ++j) {
        if (array[i][j] > max) {
            max = array[i][j];    // found higher value. remember it
            maxCount = 1;        // and reset the count
        } else if (array[i][j] == max) {
            ++maxCount;         // saw max value again. increment count
        }
    }
}

Sorry, I need to sort the array and include the average value as well, I just didn't mention it since I have both those aspects working. Thanks for the code though, I can't try it right now but I will let you know later.
Hey, I just wanted to thank you again. The code works great, and you've officially helped me more than my professor has all semester. Thanks!
LOL. Thanks!
Topic archived. No new replies allowed.