0
I’m having trouble organizing the data of a struct, the data are even organizing, but I always end up receiving memory junk in the first position, the others are organized normally without errors. The structure should be in descending order according to the given int Pont of the classified struct.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct concurso
{
char nome[30];
int matri;
int prov1;
int prov2;
int prov3;
}con;
typedef struct clasificados
{
char n[30];
int m;
int pont;
}cla;
void ordena(con* co,int tam)
{
cla *conc=(cla*)malloc(sizeof(cla)*tam);
cla aux;
int i,j=0;
for(i=0;i<tam;i++)
{
if((co[i].prov1>40)&&(co[i].prov2>40)&&(co[i].prov3>40)&&(co[i].prov1+co[i].prov2+co[i].prov3>=150))
{
conc[j].pont=co[i].prov1+co[i].prov2+co[i].prov3;
strcpy(conc[j].n,co[i].nome);
conc[j].m=co[i].matri;
j++;
}
}
//O PROBLEMA ESTA DAQUI PARA BAIXO, AQUI QUERO ORDENAR OS DADOS DA STRUCT CLASSIFCADOS, ATE QUE ORDENA, MAIS O PRIMEIRO ELEMENTO SAI COMO LIXO DE MEMORIA
for( i=0; i<j; i++ )
{
if( conc[i].pont < conc[i+1].pont )
{
aux.pont = conc[i+1].pont;
conc[i+1].pont = conc[i].pont;
conc[i].pont = aux.pont;
strcpy(aux.n,conc[i+1].n);
strcpy(conc[i+1].n,conc[i].n);
strcpy(conc[i].n, aux.n);
aux.m = conc[i+1].m;
conc[i+1].m = conc[i].m;
conc[i].m = aux.m;
i=-1;
}
}
printf("\n\t\t------Classificados-------\n");
printf("\nPOSICAO\t\tMATRICULA\tCANDIDATO\tNOTA\n");
for( i=0; i<j; i++ )
{
printf("%d\t\t%d\t\t%s\t\t%3d\n",i+1,conc[i].m,conc[i].n,conc[i].pont);
}
free(conc);
}
int main()
{
int i,j;
int tam;
printf("Digite o numero de candidatos: ");
scanf("%d",&tam);
con *c=(con*)malloc(sizeof(con)*tam);
for(i=0;i<tam;i++)
{
printf("Digite o nome do candidato %d: ",i+1);
scanf(" %[^\n]s",c[i].nome);
printf("Digite a matricula do candidato %s: ",c[i].nome);
scanf("%d",&c[i].matri);
printf("Digite a nota 1 do candidato %s: ",c[i].nome);
scanf("%d",&c[i].prov1);
printf("Digite a nota 2 do candidato %s: ",c[i].nome);
scanf("%d",&c[i].prov2);
printf("Digite a nota 3 do candidato %s: ",c[i].nome);
scanf("%d",&c[i].prov3);
}
ordena(c,tam);
free(c);
return 0;
}
I quickly tested your code, and it seemed to work correctly. Check here at Ideone if it wasn’t the result you expected. So I invite you to detail the problem better. Which entries did you use to make it not work ? Remember that when generating the classifieds if only one qualify has a problem in the
if
which immediately tests two array elements.– Isac
So, in the online compilers the code presents no problems, but in places like code Blocks, dev; this error happens that I mentioned of printing memory junk in the first position.
– Graciano Souza
I did not understand this "if" problem, could exemplify me better. Thank you
– Graciano Souza
I ran in codeblocks and ran the same as in Ideone. What exact values are you using as input? And the problem comes in this
if
=>if( conc[i].pont < conc[i+1].pont )
that according to thefor
there is no guarantee thati+1
is a valid element– Isac
Thanks, I’ll check on the if.
– Graciano Souza
About the values, I’m using random values, I’ve done several tests, I always have the same mistake, but now with your statement I’ll check my compilers, and test on another pc, then I come back here, thanks
– Graciano Souza
Enjoy and test with the values that the link I passed in Ideone uses. And put here the values that you’re testing and that you say fail, so that I can also test and see if there really is a problem.
– Isac