-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.
– anonimo
In fact in the exercise does not require maintaining relations, only ordering the data, even if the relations are lost
– Junior Kubiak
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.
– anonimo
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).
– anonimo
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
– vmp