Problems organizing a C-struct

Asked

Viewed 341 times

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.

  • 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.

  • I did not understand this "if" problem, could exemplify me better. Thank you

  • 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 the for there is no guarantee that i+1 is a valid element

  • Thanks, I’ll check on the if.

  • 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

  • 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.

Show 2 more comments

1 answer

0


Dude basically your mistake is in for, your for is as follows, for( i=0; i<j; i++ ), this way when you make your vector in the index i + 1, when your for, reach the last loop it will buffer overflow, and burst the memory position belonging to its vector.

To fix this error just in the condition of your for you put, i < j - 1, spinning as follows for( i=0; i<j-1; i++ ).

Without further ado here is the complete answer:

#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(tam * sizeof(cla));
    cla aux;
    register int i , j;

    for ( i = 0, j = 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 - 1; ++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("%i\t\t%i\t\t%s\t\t%3i\n", i+1, conc[i].m, conc[i].n, conc[i].pont);

    free(conc);
}

int main()
{
    register int i,j;
    int tam;
    con* c;

    printf("Digite o numero de candidatos: ");
    scanf(" %i", &tam);

    c = (con*)malloc(tam * sizeof(con));

    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(" %i", &c[i].matri);

        printf("Digite a nota 1 do candidato %s: ", c[i].nome);
        scanf(" %i", &c[i].prov1);

        printf("Digite a nota 2 do candidato %s: ", c[i].nome);
        scanf(" %i", &c[i].prov2);

        printf("Digite a nota 3 do candidato %s: ", c[i].nome);
        scanf(" %i", &c[i].prov3);
    }

    ordena(c,tam);

    free(c);

    return 0;
}

I hope I’ve helped ;).

  • thanks man, I had solved the problem by doing another function, but it helped a lot, thank you

Browser other questions tagged

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