Help me please!
below is a part of the code which causes the error.

int B[996];
for (j = 0, k = 0; j < (N / 6); j++){
    if (A[j] == 0){
    continue;
    }
    B[k++] = A[j];
} //팀원이 여섯명인 팀만의 순위대로 저장한 배열을 새로 만듭니다.

int SUM[167] = { 0 };

for (j = 0; j < (N / 6); j++){
    int count = 0;
    for (k = 0; k < (N / 6) && count < 4; k++){
        if (B[j] == B[k]){
            SUM[B[j]] += k + 1;
            count++;
        }
    }
}

Here, (N/6) 's max is 166.

It is not clear how you are initializing A[] but on line 6, if the value of A[j] is more than 166, then on line 15, the numeric index for SUM would be out of bounds.

The value of A[j] is a number not exceeding 166.
Because I initialized A[] as A[1000] but the numbers being stored in the A array is equal or less than 166,
and same numbers can be stored only up to 6 times.

if the number is stored less than six times in array A,
I switched those numbers with 0,
and then made another array B, to store values that are not 0 in array A.
That is the first for loop.

Second for loop is to get sums of first four orders (actual index + 1) of the same numbers in array B.
For example, if the array B is {1, 2, 3, 1, 2, 3, 1, 3, 2, 1, 1, 1, 2, 3, 2, 3, 2, 3},
the value of SUM[1] has to be 1 + 4 + 7 + 10 = 22.

This was my original intention.
I can't figure out what did I do wrong.

Line 6:

B[k++] = A[j];

This is the only place where B[] array is set. Let's say that B[140] is equal to 500,00 and B[165] is equal to 1,000,000 after the B[] array is created on line 1 (could be anything since you don't initialize it). What is the value of k at the end of the loop (line 8)? Let's say k is 130 because "continue" is executed 36 times (I just picked a random number). Is B[140] or B[165] touched? If not, they are still 500,000 and 1,000,000. What happens when you get to line 15?

This article has been dead for over six months. Start a new discussion instead.