Quicksort C - Contest. c:16:50: error: request for Member' name in Something not a Structure or Union

Asked

Viewed 54 times

0

I’m trying to make this URI thing:

Tetris

His high school class decided to organize a tetris championship. After discussion about the rules, it was defined that each student would play a total of 12 matches. Of the 12 scores obtained by a student, the larger and smaller are discarded, and the rest are added up, resulting in in the final score of the student.

As you have programming knowledge, you ended up being designated by the class to write a program to print the rating Championship final, from the scores of each player. Entry

The input consists of several test sets. The first line of a test suite contains an integer J (0 J 1000), which indicates the number of players who participated in the championship. Next, for each player there are two lines in the input: the first has the name of the player (formed only by letters, being only the initial in capital letters, with a maximum of 15 letters), and the second has the 12 scores the player has scored, separated by space. Scores are integers between 0 and 1000. The end of the input is indicated by a test set with J = 0. Output

For each test set, your program should write a line containing the identifier of the test set in the "Test n" format, where n is numbered sequentially from 1. Next, your program should write the final rating in the championship, using one row for each participant. Each row must contain three information, separated by a blank space: the classification of the player, his final score, and his name. The ranking of a player is equal to 1 plus the number of players who have scored greater than yours. In the event of a tie, players must be sorted in alphabetical order. After all the sorting, you must a blank line is left. The format of the output example below must be strictly followed.

https://www.urionlinejudge.com.br/judge/pt/problems/view/2250

I’m at the part where I sort alphabetically the ones with the same amount of dots. I tried to make a condition in quicksort for when the points are equal, sort by name and am having the following error:

Contest. c: In Function 'compare': Contest. c:16:30: error: request for Member' name' in Something not a Structure or Union Return strcmp((Player*)a).name, ((Player*)b).name); Contest. c:16:50: error: request for Member 'in Something not a Structure or Union Return strcmp(((Player*)a). name, (((Player*)b). name);

Can someone explain to me what it is? "I learned" this today and I’m kind of doing it blindly. My code is this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    char nome[16];
    int pontuacao;
}Jogador;

int compare(const void * a, const void * b){
    if((*(Jogador *)a).pontuacao > (*(Jogador *)b).pontuacao){
        return -1;
    }else if((*(Jogador *)a).pontuacao < (*(Jogador *)b).pontuacao){
        return 1;
    }else if((*(Jogador *)a).pontuacao == (*(Jogador *)b).pontuacao){
        return strcmp(((Jogador*)a).nome, ((Jogador*)b).nome);
    }

    return 0;
}

int achaMaior(int *pontos){
    int maior = pontos[0];
    int i, j;

    for(i = 1; i < 12; i++){
        if(pontos[i] > maior){
            maior = pontos[i];
        }
    }

    return maior;
}

int achaMenor(int *pontos){
    int menor = pontos[0];
    int i, j;

    for(i = 1; i < 12; i++){
        if(pontos[i] < menor){
            menor = pontos[i];
        }
    }

    return menor;
}

Jogador jogador[100];

int main(){
    int j, i, k , pontos[12], maior, menor, teste = 0;;

    scanf("%d", &j);

    while(j != 0){
        teste++;
        for(i = 0; i < j; i++){
            scanf("%s", jogador[i].nome);
            for(k = 0; k < 12; k++){
                scanf("%d", &pontos[k]);
                jogador[i].pontuacao += pontos[k];
            }

            maior = achaMaior(pontos);
            menor = achaMenor(pontos);

            jogador[i].pontuacao -= (maior + menor);
        }

        qsort(jogador, j, sizeof(jogador[0]), compare);

        printf("Teste %d\n", teste);
        for(i = 0; i < j; i++){
            printf("%d %d %s\n", i + 1, jogador[i].pontuacao, jogador[i].nome);
        }

        printf("\n");

        for(i = 0; i < j; i++){
            jogador[i].pontuacao = 0;
        }

        scanf("%d", &j);
    }


    return 0;
}

1 answer

0

I was able to solve this. I was missing a pointer on the next line:

return strcmp(((Jogador*)a).nome, ((Jogador*)b).nome);

Corrected line:

return strcmp((*(Jogador*)a).nome, (*(Jogador*)b).nome);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.