data not ordered after execution C

Asked

Viewed 45 times

-1

I need to put the columns a in order, however, only the names are getting sorted correctly. The functions that sort the notes and sex are not working. Can anyone tell me where I’m going wrong? The exercises do not require relationships to remain the same, just sort the data

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

#define LIN 31
#define COL 50

    

void ordena(char nomes[LIN][COL]);
void ordenaIdade(int idade[LIN]);
void ordenaNota(float nota[LIN]);
void ordenaSexo(char sexo[LIN]);

int main(){
    
    int i;
    char nomes[LIN][COL] = {
        "Pedro Andrade Schmitz                   ",
        "João Manoel Spingler Francisco (Juvenil)",
        "Gustavo Andrade Schmitz                 ",
        "Maria Clara Argolo Sampaio              ",
        "Arthur Marinho Miranda                  ",
        "Jose Gomes da Silva                     ",
        "Pedro Castro Cabral (Juvenil)           ",
        "Davi Guilherme                          ",
        "Bruno Petri                             ",
        "Isabela Miranda Lima                    ",
        "Sebastian Annanias De Araujo Solari     ",
        "Robson Andre Da Paz Radmann             ",
        "Tomas Levy                              ",
        "Facundo Nicolas Mezquita Duarte         ",
        "Gustavo Gouvea Oliva                    ",
        "Raphaela Mello Oka                      ",
        "Anderson Calado Schmitt                 ",
        "Pedro Eckschmidt Buso                   ",
        "Felix Dumas                             ",
        "Danilo Gilberto Volkmann Gonçalves      ",
        "Pedro Henrique Colauto Gregorio         ",
        "Marcelo Augusto Evangelista Ribeiro     ",
        "Piedro Xavier Tuchtenhagen              ",
        "Beatriz Vilela Da Silva                 ",
        "Erik Kai Attie                          ",
        "Arthur Poliselli Farsky                 ",
        "Felipe Toledo Piza Abramento            ",
        "Luca Suplicy Leger                      ",
        "Matheus Mello Oka                       ",
        "Wesley Patrick Guedes De Oliveira       ",
        "Yan Ewald Zechner                       "
        
    };
    char sexo[LIN] = {'M','M','M','F','M','M','M','M','M','F','M','M','M','M','M','F','M','M','M','M','M','M','M','F','M','M','M','M','M','M','M'};
    int idade[LIN] = {15,14,15,17,16,17,18,14,15,14,15,18,14,15,16,16,15,14,14,16,17,15,18,14,14,14,15,16,17,18,14};
    float nota1[LIN] = {7.0,2.0,5.0,7.0,7.8,5.0,7.0,9.8,7.4,8.5,7.0,9.0,8.4,8.0,7.0,6.0,8.0,8.0,8.0,7.0,6.0,7.0,7.0,5.0,4.5,6.0,8.0,7.0,9.0,10.0,8.0};
    float nota2[LIN] = {9.0,0.0,7.0,6.0,7.0,9.0,8.0,7.0,10.0,8.0,8.0,8.4,7.0,9.0,5.0,5.4,9.0,6.5,9.0,6.0,4.0,6.5,8.0,9.0,7.0,7.7,8.0,7.0,9.0,10.0,6.0};
    
    setlocale(LC_ALL,"Portuguese");
    
    for(i=0;i<LIN;i++){
        printf("%s\t%c\t%d\t%.1f\t%.1f\n",nomes[i],sexo[i],idade[i],nota1[i],nota2[i]);
    }
    
    ordena(nomes);
    ordenaSexo(sexo);
    ordenaIdade(idade);
    ordenaNota(nota1);
    ordenaNota(nota2);
    
    printf("\n|==========NOMES ORDENADOS==========|\n\n");
    for(i=0;i<LIN;i++){
        printf("%s\t%c\t%d\t%.1f\t%.1f\n",nomes[i],sexo[i],idade[i],nota1[i],nota2[i]);
    }
    
    
    
    printf("\n\n");
    getch();
    return 0;
}

void ordena(char nomes[LIN][COL]){
    int i,j,k;
    char aux[COL];
    
    for(i=0;i<LIN;i++){
        for(j=i+1;j<LIN;j++){
            k=strcmp(nomes[i],nomes[j]);
            if(k>0){
                strcpy(aux,nomes[i]);
                strcpy(nomes[i],nomes[j]);
                strcpy(nomes[j],aux);
            }
        }
    }
}


void ordenaIdade(int idade[LIN]){
    int i,j,aux;
    
    for(i=1;i<LIN;i++){
        aux = idade[i];
        j = i-1;
        idade[0] = aux;
        while (aux < idade[j]){
            idade[j+1] = idade[j];
            j--;
        }
        idade[j+1] = aux;
    }
}

void ordenaNota(float nota[LIN]){
    int i,j,aux;
    
    for(i=1;i<LIN;i++){
        aux = nota[i];
        j = i-1;
        nota[0] = aux;
        while (aux < nota[j]){
            nota[j+1] = nota[j];
            j--;
        }
        nota[j+1] = aux;
    }
}
    
void ordenaSexo(char sexo[LIN]){
        int i,j,k;
        char aux;
    
            for(j=i+1;j<LIN;j++){
                if(strcmp(sexo[i],sexo[i+1])>0){
                    aux = sexo[i];
                    sexo[i] = sexo[i+1];  
                    sexo[i+1] = aux; 
                }
            }
        
}
  • So I was able to understand each index of each of the arrays identifies a person and its attributes (names[i] is the name of the ith person, sex[i] his sex, age] and Nota1[i] and nota2[i] his notes) This way you cannot sort each array separately because it will lose the association of each person with its attributes. When defining that you have to change the position of the names has to change the position of the attributes associated with them.

  • In fact in the exercise does not require maintaining relations, only ordering the data, even if the relations are lost

  • In my opinion you misunderstood the formulation of the problem because your statement makes no sense and, I believe, no teacher would commit such idiocy.

  • Although in C a string is a character array followed by the terminator character ' 0' its sex array is not a string but a simple character array and therefore the comparison of each character should not be made with the use of the strcmp function (unlike names).

  • the last 3 functions of sorting only have 1 for each... compare these 3 with the first function you will find the error... and when comparing the sex compare with > and not with strcmp

1 answer

1

  • In the first Function, the notes
void ordenaNota(float nota[LIN]){
   int i,j,aux; //O aux aqui tinha que ser float, assim como seu vetor nota, do contrário nenhuma das operações com ele funcionará
   
   for(i=1;i<LIN;i++){
       aux = nota[i];
       j = i-1;
       nota[0] = aux; //Não compreendi essa linha, como ela sempre vai ser executada, a primeira posição do vetor não será sempre trocada pelo valor a ser analisado? significando que em LIN-1 vezes vc terá pelo menos 2 repetições do mesmo valor
       while (aux < nota[j]){
           nota[j+1] = nota[j]; // Essa linha somada com a outra fará vc simplesmente copiar o dado para as posições em que aux for menor, posso estar errado, mas acredito que essa função vai entrar em loop infinito
           j--;
       }
       nota[j+1] = aux;
   }
}
  • A better strategy for the note sorting function is that which you have already applied in the normal Sort, a simple Bubblesort. If you want to run something more similar with what you have thought, I suggest studying the code of Insertsort, which has a similar approach.

  • In the function of Sort sex, I believe that the char needs to have a set size, in your case, [1] would be enough.

I hope I’ve helped.

Browser other questions tagged

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