0
I’m doing a project where it’s based on creating a structure, a vector of a structure, filling that vector with data from a file and then performing some functions.
One of the functions is to count the period of a given day with more vehicles. What I have wrong and why?
typedef struct _hora
{
int hora;
int minuto;
}Hora;
typedef struct _data
{
int dia;
int mes;
int ano;
}Data;
typedef struct _passagem
{
char matricula[9];
Data data;
Hora hora;
char tipo_passagem[3];
int portao;
}Passagem;
int hora_maisveiculos(Passagem *v,int x)//4.c
{
int dia,i,tamanho=4;
Passagem *aux;
aux = (Passagem*)malloc(tamanho * sizeof(Passagem));
if (aux == NULL) {printf("Erro:Atribuicao de Memoria Dinamica");free(aux);exit(-2);}
printf("Qual o dia que deseja pesquisar?\n");
scanf("%d",&dia);
for (i =0;i<x;i++)
{
if (v[i].data.dia == dia)
{
tamanho++;
aux = (Passagem*)realloc(aux,tamanho*sizeof(Passagem));
aux[tamanho-1]=v[i];
}
}
int menor = aux[0].hora.hora;
int maior = aux[tamanho-1].hora.hora;
//for (i = 0; i<tamanho;i++)
//{
/*if (strcmp(aux[i].tipo_passagem,"in") == 0)
{
if (aux[i].hora < menor)
{
menor = aux[i].hora;
}
if (aux[i].hora > maior)
{
maior = aux[i].hora;
}
}*/
//}
printf("Maior %d Menor %d \n",maior,menor);
return 0;
}
Why
tamanho
begins in4
? So you never assign value toaux[0]
.– Guilherme Bernal
Is the code too long? You could put the whole code so it would be possible to test directly with the compiler.
– Bernardo Botelho
Didn’t you say which error you get... Failed execution (error message), wrong values return (which ones would be correct)? You can [Edit] your question freely to add information.
– brasofilo