Home > Net >  Why is first value of an C array and last value reversed without intention?
Why is first value of an C array and last value reversed without intention?

Time:01-11

Here is my code

#include<stdio.h>
#define MAX 10000

int main(){
    int a[MAX], b[MAX], n;  
    scanf("%d", &n);
    for(int i=0;i<n;i  ){
        int c, d, e, f;
        scanf("%d%d%d%d", &c, &d, &e, &f);
        if(f==e){
            a[c] =1;
            a[d] =1;
        }
        else{
            if(f>e){
                a[c] =3;
            }
            else{
                a[d] =3;
            }
        }
    }   
    for(int i=0;i<4;i  ){
        printf("%d ", a[i]);
    }
    return 0;
    }

And here is it's input:

4 
1 2 0 0
3 4 1 0
1 3 2 1
2 4 1 1

There are 4 games, each game has 2 teams. The c and d are input variables could be changed in each input statement of the for loop, they help the program to indentify what team is it to add 1 point if c and d of that match are tie (that for loop), if c or d wins, gets 3 points and 0 point for loser. To know if c or d wins, e and f are the games scores and also can be changed in each input statement of the for loop. For example:

  • There are 4 games (given input):
  • The first game ended up with 0-0 for team 1 and team 2(c and d now are 1 and 2, e and f are 0 and 0), by logically a[1] and a[2] values are 1 and 1 because they tie.
  • Respective with the other games. In total, a[1]=4, a[2]=2, a[3]=3, a[4]=1. Then the expected array output must be: 4 2 3 1 but by somehow, my prgoram reversed the first and last value which is 1 2 3 4.

CodePudding user response:

  1. Check the return value from scanf() otherwise you may be operating on uninitialized values.
  2. Initialize the array a.
  3. There appears to be a logic error. If e < f then we want to award points to a[d].
  4. The first index of arrays is 0 but your teams are between 1 and 4 so the output loop is wrong. c and d are really labels so maybe write a function to extract an array of labels and iterative over those instead.
  5. (not fixed) Consider using a "team, score" format instead of a flat list of 4 numbers.
  6. The array b is not used.
  7. (not fixed) The non-descriptive variables a, c, d, e and f makes your code hard to read. For the last 4 what about home, guest, home_score, guest_score?
  8. (not fixed) You should check that c and d are >= 0 and < MAX, otherwise you will have a buffer overflow. Consider using unsigned values instead of (signed) int.
#include <stdio.h>
#define MAX 10000

int main() {
    int n;
    if(scanf("%d", &n) != 1) {
        return 1;
    }
    int a[MAX] = { 0 };
    for(int i=0; i<n; i  ){
        int c, d, e, f;
        if(scanf("%d%d%d%d", &c, &d, &e, &f) != 4) {
            return 1;
        }
        if(e < f) {
            a[d]  = 3;
        } else if(f == e) {
            a[c]  ;
            a[d]  ;
        } else {
            a[c]  = 3;
        }
    }
    for(int i=1; i<=4 ;i  )
        printf("%d ", a[i]);
    printf("\n");
}

and example session:

4 
1 2 0 0
3 4 1 0
1 3 2 1 
2 4 1 1
4 2 3 1 
  •  Tags:  
  • c
  • Related