ZyxyZ
ZyxyZ

Reputation: 33

C: Using Array, keeps giving me wrong output

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!

Upvotes: 0

Views: 65

Answers (1)

Bumzur
Bumzur

Reputation: 111

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.

Upvotes: 1

Related Questions