Counting a given day period with more vehicles

Asked

Viewed 71 times

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 in 4? So you never assign value to aux[0].

  • Is the code too long? You could put the whole code so it would be possible to test directly with the compiler.

  • 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.

1 answer

1

According to the title of the post, you must need an array to count by period. Each element of the array corresponds to a period.

For example, if the period is 1 hour

int contaperiodo[24];
//foreach (passagem) {
//    contaperiodo[passagem.hora] += 1;
//}
/* agora pesquisa no array contaperiodo[] qual o periodo com mais passagens

Browser other questions tagged

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