Home > Blockchain >  Can anyone tell me the problem with my code?
Can anyone tell me the problem with my code?

Time:03-29

Everythings right except the output of the last part, I'm supposed to get this output https://i.stack.imgur.com/PRJMT.png but instead it got this https://i.stack.imgur.com/iZflq.png

Also heres my code:

#include <stdio.h>
int main() {
    int n;
    printf("Enter the number of goal-scorers: ");    
    scanf("%d", &n);

    int ar[n];
    for(int i=0; i<n; i  ){
        printf("Score of player #%d: ", i 1);
        scanf("%d", &ar[i]);
    }

    for(int i=0; i<n; i  ){
        for(int j=i 1; j<n; j  ){
            if(ar[i] <ar[j]){
                int temp = ar[j]; 
                ar[j] = ar[i];
                ar[i]; temp;
            }
        }
    }

    printf("\nHighest to lowest:\n");
    for(int i=0; i<n; i  )
        printf("Player #%d: %d\n",i 1, ar[i]);

    return;
}

CodePudding user response:

Found it!

ar[i]; temp;

But you want ar[i] = temp;

You enabled compiler warnings. What I don't get is why the warning for "statement has no effect" is not showing up.

CodePudding user response:

The possible solution may be like :

#include <stdio.h>

struct player {
    int no;
    int score;
};

int main() {
    int n;
    printf("Enter the number of goal-scorers: ");
    fflush(stdout);
    scanf("%d", &n);
    if (n <= 0)
        return 0 ;
    
    struct player players[n];
    for (int i = 0 ; i < n ; i  ) {
        players[i].no = 1   i;
        printf("Score of player #%d: ", players[i].no);
        fflush(stdout);
        scanf("%d", &players[i].score);
    }

    for (int i = 0 ; i < n ; i  ) {
        for (int j = i   1 ; j < n ; j  ) {
            if (players[i].score < players[j].score) {
                struct player temp = players[j];
                players[j] = players[i];
                players[i] = temp;
            }
        }
    }
    
    printf("\nHighest to lowest:\n");
    for (int i = 0 ; i < n ; i  )
        printf("Player #%d: %d\n", players[i].no, players[i].score);

    return 0;
}
  •  Tags:  
  • c
  • Related