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);
}
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?
– Aislan Silva Costa
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
– Victor Henrique