0

I have been struggling with this all day, the output I get is all correct execept the middle result. If anyone can help me it would be great. The output I get is for id[A] instead of id[C], it seems like in the loop the ID for A is carried to C.

    int firstA=0, firstS=0, secondS=0, firstC=0, secondC=0;

    for (row=0; row<totalSize; row++) {

        if (category[row]=='A' && ranking[firstA] < ranking[row]) {
            firstA = row;

        }

        if (category[row]=='C' && ranking[firstC] < ranking[row]) {
            secondC = firstC;
            firstC = row;
        }
        else if (category[row]=='C' && ranking[secondC] < ranking[row]) {
            secondC = row;
        }

        if (category[row]=='S' && ranking[firstS] < ranking[row]) {
            secondS = firstS;
            firstS = row;
        }
        else if (category[row]=='S' && ranking[secondS] < ranking[row]) {
            secondS = row;
        }


    }

    printf("A : %d %.2lf \n", id[firstA], ranking[firstA]);
    printf("C : %d %.2lf \n", id[firstC], ranking[firstC]);
    printf("C : %d %.2lf \n", id[secondC], ranking[secondC]);
    printf("S : %d %.2lf \n", id[firstS], ranking[firstS]);
    printf("S : %d %.2lf \n", id[secondS], ranking[secondS]);

    return 0;
}

INPUT FILE

10
14 A 447 252 68 34 978
2 C 230 299 597 180 9
27 A 318 220 97 28 1317
32 C 563 450 547 112 28
8 C 669 260 200 36 171
11 S 179 45 1342 732 174
19 S 74 249 861 1165  6 
21 A 757 240 97 119 2032
15 S 275 177 588 577 52
6 C 886 401 327 109 48

EXPECTED OUTPUT

A: 21 1171.00
C: 6 696.70
C: 32 578.00
S: 11 1094.20
S: 19 1046.50

The problem is here, I get this

A : 21 1171.00
C : 6 696.70
C : 14 601.10
S : 11 1094.20
S : 19 1046.50

The Middle C is carrying the ID of one of the A's. I can't seem to figure out whats wrong in my loop. Any help would be appreciated!

2
  • Your second ranking for C never gets assigned. That is because the first ranking for C is the last one, hence the first part of the relevant if-else statement for C is always true, and your code never gets to the else part (where secondC is assigned). You're seeing the value for A, because that is the very first value in your list (item 0) and secondC is still set to its initial default of 0. Commented Mar 25, 2017 at 4:07
  • so a different way from doing it as in the answer down below, would be to just use all if statements instead of else? Commented Mar 25, 2017 at 4:18

1 Answer 1

1

The secondS is initialised to row 0 which is category 'A'. The rank of that row is less than the expected secondS row, so secondS stays 0. You need to do something so the initial values will always get assigned to the correct category, like this

   int firstA=-1, firstS=-1, secondS=-1, firstC=-1, secondC=-1;

   for (row=0; row<totalSize; row++) {

       if (category[row]=='A' && (firstA == -1 || ranking[firstA] < ranking[row])) {
           firstA = row;
       }

       if (category[row]=='C' && (firstC == -1 || ranking[firstC] < ranking[row])) {
           secondC = firstC;
           firstC = row;
       }
       else if (category[row]=='C' && (secondC == -1 || ranking[secondC] < ranking[row])) {
           secondC = row;
       }

       if (category[row]=='S' && (firstS == -1 || ranking[firstS] < ranking[row])) {
           secondS = firstS;
           firstS = row;
       }
       else if (category[row]=='S' && (secondS == -1 || ranking[secondS] < ranking[row])) {
           secondS = row;
       }


   }

Since all of the initial indices are -1, as soon as you see a row of the corresponding category they are assigned. Initialising an index as -1 can be dangerous, but this is safe because C should stop evaluating the conditional statements when it sees e.g. firstA == -1, and will never evaluate ranking[-1] due to short circuit evaluation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for the help. It worked! I really appreciate it. I wonder why this question got a -1 rating though?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.