1

I want the method to count all the maximum/minimum values in the 2D array poängInsamling. Each domare (=judge) gives a value to all deltagare (=members). I want to remove the maximum/minimum value of each deltagare. The code I have now only works if there are 2 or less members.

This is what I got:

        for (int x = 0; x < deltagare; x++) {
            for (int y = 0; y < domare; y++) {
                if (poängInsamling[y][x] == -1) {
                    poängInsamling[y][x] = 0;
                    break;
                }       
            }
        }
    return poängInsamling;
}

Thanks in advance, I've been trying to fix this for hours.

Edit: int[][]PoängInsamling = int[domare][deltagare];

If all deltagare has the same value, all their points end up 0.

4
  • What is the connection between poängInsamling and deltagare -- which points in the 2D array represent a single deltagare? Commented Oct 25, 2015 at 14:33
  • for (int i = 0; i < deltagare; i++) {; See this line? You should remove that semi-colon. Commented Oct 25, 2015 at 14:35
  • @MickMnemonic View my edit. Thanks. Commented Oct 25, 2015 at 14:35
  • @wadda_wadda Do not know how that got there... However, that does not solve my problem unfortunately. Thanks Commented Oct 25, 2015 at 14:38

1 Answer 1

1

You are searching the entire 2D array in order to remove the lowest and highest value across all members, but you want to remove the lowest and highest value only for the current member. You can eliminate the second loop if you keep track of the index having the maximum/minimum value.

For example, for the maximum (the minimum will be similar) :

    int max = -1;
    int maxIndex = -1;
    for(int i = 0; i < deltagare; i++) {
        max = -1; // clear the max value when starting with a new member
        maxIndex = -1;
        for(int t = 0; t < domare; t++) {
            if (poängInsamling[t][i] > max) {
                max = poängInsamling[t][i];
                maxIndex = t;
            }       
        }
        // clear the max value for the current member
        poängInsamling[maxIndex][i] = -1;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! But should minimumIndex also be -1?
@Habbo It doesn't really matter what you initialize it to (since you will overwrite its value anyway). -1 will work fine.
Check my edit. If all members get the value = 3 by 3 judges, their points should be 3 (max = 3 and min = 3), but they end up getting the value 6.
@Habbo I believe you should change if (poängInsamling[t][i] < min) to if (poängInsamling[t][i] < min && poängInsamling[t][i] >= 0), as you had in your original code. Otherwise you will always find the minimum to be -1 (the value that was the maximum and was changed to -1).
THANKS! Cheers to you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.