Difficulty accessing elements in a C vector

Asked

Viewed 54 times

0

In the following code I try to obtain the largest element of a vector. However, when trying to access the elements of the vector I can access only the index 0 and consequently cannot access the other elements of the vector.

Below this the source code (main. c):

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


int *maior;
int *comparador;
int i = 0;
int quant;
int count = 0;

int CriarVetor(int tamVetor){

    int a = 1;

    int *vetorElementos = malloc(sizeof(int *) * a);

    srand(time(NULL));

    vetorElementos = realloc(vetorElementos, sizeof(int *) * tamVetor);

    for (int i = 0; i < tamVetor; i++)
    {
        vetorElementos[i] = rand() % 100;
    }


        return *vetorElementos;

}



int AcharElemento(int *vetorElementos){

    maior = vetorElementos[0];
    printf("recebi o vetor, seu primeiro elemento e %d\n", maior);
    comparador = vetorElementos[i];
    printf("vetorElementos[i] e %d\n", comparador);

    /* 

       if (i == quant) { return maior; } else {

       if (comparador > maior) { printf("entrou no if de comparacao e
       %d\n",comparador);

       maior = comparador;


       } i++; AcharElemento(&vetorElementos);

       }

     */

   return maior;
}



int main(){

    printf("Entre a quantidade de Elementos desejada\n");
    scanf("%d", &quant);

    int *elementos = CriarVetor(quant);


    printf("Os elementos do vetor sao:{");
    for (int i = 0; i < quant; i++)
    {
        printf("%d,", &elementos[i]);

    }
    printf("}\n");

    /* Encontrar o maior numero dentro do vetor */
    int result = AcharElemento(&elementos);
    printf("Maior Elemento e: %d\n", result);

} 

1 answer

1

First remove the pointer from the greater and comparator:

int maior;
int comparador;

Then you must correct the function return parameter CriarVetor so that it returns int * instead of int and the return variable shall be vectorial and not *vectorial

int* CriarVetor(int tamVetor){

    int a = 1;

    int *vetorElementos = malloc(sizeof(int *) * a);

    srand(time(NULL));

    vetorElementos = realloc(vetorElementos, sizeof(int *) * tamVetor);

    for (int i = 0; i < tamVetor; i++)
    {
        vetorElementos[i] = rand() % 100;
    }


        return vetorElementos;

}

And finally in the main you must:

  1. remove the symbol & of elements;
  2. Dislocate elements (is not a mistake, but a good practice of programming)

Function must be equal below:

int main(){    
    printf("Entre a quantidade de Elementos desejada\n");
    scanf("%d", &quant);

    int *elementos = CriarVetor(quant);

    printf("Os elementos do vetor sao:{");
    for (int i = 0; i < quant; i++)
    {
        printf("%d,", elementos[i]);

    }
    printf("}\n");

    /* Encontrar o maior numero dentro do vetor */
    int result = AcharElemento(elementos);
    printf("Maior Elemento e: %d\n", result);

    free(elementos);
}

I used two online compilers

  • 1

    Hi Victor, thanks for the contact! Yes it worked here, and I got the recursive function too. However in this part of the code: int* result = Acharelemento(elementos); printf("Maior Elemento e: %d n", *result); I was giving Seguimentacao error, so I removed the asterisk and it worked, I could tell me why?

  • 1

    The error occurs because its Acharelemento function is returning a POINTER and its variable is of type INT. I will edit the answer, I ended up finding errors. so done and ask to evaluate and if appropriate, mark as solution. This is good practice on the site

Browser other questions tagged

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