0
Folks good night, I turn to you to guide me on my problem.
Next, I am developing a work for college where I store and display the sorting results of several algorithms, being number of comparisons, exchanges and time. I have everything ready and written, IE, the scope of the work this Ok, It remains to implement the rest of the algorithms.
I got almost everything with the help of colleagues from another forum, but I have a mistake that I cannot solve it, if anyone can give me an idea of what is happening I am grateful.
1- Here I have my global list of records that will store the sorting results:
typedef struct{ int numcomp; int number = 0; }STATISTICS;
2- As an example the Bubble Sort algorithm receiving the values by reference:
void bubbleSort(int vet[], int num, STATICS* statistic){
int i, continua, aux, fim = num;
do{
continua = 0;
for(i = 0; i < fim -1; i++ ){
estatisticas->numcomp++;
if(vet > vet[i+1]){
aux = vet;
vet = vet[i+1];
vet[i+1] = aux;
estatisticas->numtrocas++;
continua = 1;
}
}
fim--;
}while(continua != 0);
}
3- and here the list declaration and the call of the sort function with the stored values display:
int *ptr; ptr = geraVetor(vector 1); //imprimeVetor(ptr, vector 1);
ESTATISTICAS* estatisticas = (ESTATISTICAS*) malloc(sizeof(ESTATISTICAS));
clock_t start, end;
start = clock();
bubbleSort(ptr,vetor1, estatisticas);
end = clock();
printf("\n\nTROCAS: %d", estatisticas->numcomp);
printf("\n\nTROCAS: %d", estatisticas->numtrocas);
4- The error is: when I compile my program everything is ok, but the value returned by the numcomp variable is far beyond the number of comparisons, looking more like a memory address. something like: 348219. Already the variable in a change the value is returned normally after the increments ++;
I’m waiting for help, thank you.
Which compiler are you using? I tried to compile your code using gcc and error tydedef struct by doing 'int numtrocar = 0'. This build error makes sense, because you shouldn’t initialize the value of a variable in a typedef struct, you’re just defining a new type. When creating a variable of this new type you should initialize its values.
– luislhl