Pointer returning memory value

0
Well, good night, I ask you to guide me about my problem.

Next, I'm developing a college assignment where I store and display the sorting results of various algorithms, such as comparisons, exchanges, and time. I have everything ready and written, that is, the scope of the work is Ok, we need to implement the rest of the algorithms.

I got almost everything with the help of colleagues from another forum, but I have an error that I can not solve, 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 sort results:

typedef struct {     int numcomp;     int numtrocas = 0; } STATISTICS;

2- As an example, the bubble sort algorithm receives the values by reference:

void bubbleSort (int vet [], int num, STATISTICS * statistics) {

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- Here is the list declaration and the call of the sort function with the display of stored values:

int * ptr;                     ptr = geraVetor (vector1);                     // printVector (ptr, vector1);

                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 consists of: when compiling my program everything is ok, however the value returned by the numcomp variable is well beyond the number of comparisons, if it looks more like a memory address. something like: 348219. But the numtrocas variable returns the value normally after increments ++;

I look forward to your help, thank you.

    
asked by anonymous 13.03.2017 / 03:23

2 answers

0

I think your problem comes from the fact that you did not initialize the numcomp variable of the struct, you should initialize it as 0, as you did with numtrocas.

Also, I noticed 2 strange things in your code, which I'll explain below:

Failed to compile

When trying to compile your code with gcc, I got the following compilation error:

189566.c:1:44: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
typedef struct{ int numcomp; int numtrocas = 0; }ESTATISTICAS;

This means that it was not accepting the initialization of this numtrocas variable, when I took the '= 0', it worked.

In my opinion, this makes sense because you should not initialize the variable inside the typedef struct, because in typedef you are only defining a new type, and when creating variables of this new type you should initialize them. / p>

Comparisons in BubbleSort

Your BublleSort algorithm looks wrong, the following warnings that I received when compiling make the problem explicit:

189566.c:17:11: warning: comparison between pointer and integer
    if(vet > vet[i+1]){
           ^
189566.c:18:9: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
     aux = vet;
         ^
189566.c:19:9: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     vet = vet[i+1];
         ^

What is happening here is that you are comparing a pointer (vet) with an integer (vet [i + 1]). The right one would be vet [i] > vet [i + 1].

After the changes that I thought necessary, the following code worked for me:

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

typedef struct{
    int numcomp;
    int numtrocas;
}ESTATISTICAS;

void bubbleSort(int vet[], int num, ESTATISTICAS* estatisticas){
    int i, continua, aux, fim = num;
    do{
        continua = 0;

        for(i = 0; i < fim -1; i++ ){

            estatisticas->numcomp++; 
            if(vet[i] > vet[i+1]){
                aux = vet[i];
                vet[i] = vet[i+1];
                vet[i+1] = aux;
                estatisticas->numtrocas++;
                continua = 1;
            }
        }
        fim--;
    }while(continua != 0);

}

int main() {
    //int *ptr; ptr = geraVetor(vetor1); //imprimeVetor(ptr, vetor1);

    int ptr[5] = {4,2,6,3,7};

    ESTATISTICAS* estatisticas = malloc(sizeof(ESTATISTICAS));
    estatisticas->numcomp = 0;
    estatisticas->numtrocas = 0;

    bubbleSort(ptr,5, estatisticas);
    printf("\n\nCOMPARACOES: %d", estatisticas->numcomp);
    printf("\n\nTROCAS: %d", estatisticas->numtrocas);
}
    
13.03.2017 / 04:26
0

Good morning! I was able to solve the problem by just initializing the variables. I do not know why the typdef does not accept the startup, okay, I did. Thank you so much for helping, God bless you.

    
13.03.2017 / 13:15