Pointer returning memory value

Asked

Viewed 81 times

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.

2 answers

0

I think your problem comes from the fact that you didn’t initialize the numcomp variable of the struct, you should initialize it as 0, as you did with nums.

Also, I noticed 2 strange things in your code, which I will explain next:

Failed to compile

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

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

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

In my opinion, this makes sense, because you should not initialize the variable within typedef struct, because in typedef you are just defining a new type, and when creating variables of this new type is that you should initialize them.

Comparisons in Bubblesort

Your Bubllesort algorithm seems wrong, the following warnings I received when compiling spell out the problem:

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]). Right would be vet[i] > vet[i+1].


After the changes 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);
}

0

Good morning ! I was able to solve the problem just by initializing the variables. I don’t know why typdef didn’t accept the initialization, okay, I did. Thank you so much for helping, keep your God hugs.

Browser other questions tagged

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