Program Received Signal sigsegv Segmentation fault

Asked

Viewed 2,690 times

0

I made the code in C of the statement:

Develop a program that simulates a ticketing system for a airline. The company has no flights, where in each of them there are a available seats. The first m/2 seats of each flight are reserved for non-smokers and the rest for smokers.

Use a matrix of n rows per m columns to represent the seats of flights of this company. Initialize the matrix elements with zero to indicate that all seats on each of the n flights are available. As the seats are occupied assign 1 to the corresponding element of the matrix.

Shall be generated at random:

  1. The number of flights.
  2. The number of tickets requested per flight.
  3. If the passenger is from the area of smokers or non-smokers. The program should print at the end of the simulation:
  4. The average number of passengers per flight.
  5. The average number of passengers per area on each flight
  6. The average number of passengers not boarded due to lack of seat (please note there may be room on the plane, but there is no room in the requested area)

I’ve used debug but I can’t identify the reason for the error: Program Received Signal sigsegv Segmentation fault I’ve changed and put several ways to start random number generator, using NULL for example, but it’s no use. srand(time(NULL)), below the code:

main() {


  int n,m,p, i, j, matriz[n][m],f,assento_ocup,aux,fumantes,nao_fumantes; 
  float media_passageiros_voo, media_passageiros_area,media_pas_falta_assento,
  media_passageiros_nao_fumantes_area, media_passageiros_fumantes_area;
  time_t t;


  // iniciar gerador de numeros aleatórios
  srand((unsigned)time(&t));

  //numero de voos-linhas
  n=rand()%1+500;

  //numero de lugares-colunas
  m= rand()%1+100;

  //numero de passagens solicitadas
  p= rand()%1+100;


  for(i=0; i<n; i++)  { //linha-voos
    for(j=0; j<m; j++){//coluna-passagens
        matriz[n][m]=0;//todos assentos disponiveis
    }
  }

  if(f%2==0){//se par nao e fumante
    for(i=0; i<n; i++)  { 
        for(j=0; j<m/2; j++){
              matriz[i][j]=1;
              nao_fumantes++;
            }
    }
  }
  else{ //e fumante
    for(i=0; i<n; i++)  { 
        aux=((m/2)+1);
        for(j=aux; j<m; j++){
            matriz[i][j]=1;
            fumantes++;
        }
    }   
  }

  for(i=0; i<n; i++)  { 
    for(j=0; j<m; j++){
        if(matriz[i][j]==1){
            assento_ocup++;
        }
    }
  }

  media_passageiros_voo=assento_ocup/n;

  media_pas_falta_assento= m-p;

  media_passageiros_fumantes_area= fumantes/(m/2);
  media_passageiros_nao_fumantes_area= nao_fumantes/(m/2);

  printf("\n MEDIA DE PASSAGEIROS POR VOO:%.2f ",media_passageiros_voo);
  printf("\n MEDIA DE PASSAGEIROS POR AREA EM CADA VOO: %.2f", media_passageiros_area);  
  printf("\n PASSAGEIROS NAO EMBARCADOS POR FALTA DE ASSENTO",media_pas_falta_assento);

  getche();

}
  • 1

    The gdb (Debugger) can give you at least a sense of where Segfault is going. Try the command backtrace right after the error.

1 answer

0


First, variables are not used in size statements arrays, even more so before you even define them, as you did in matriz[n][m]. Put whole numbers as sizes or set your own constants with #define and do something like

matriz[LINHAS_MAX][COLUNAS_MAX]

And clearly the following line is wrong (probably by typo):

matriz[n][m]=0;//todos assentos disponiveis

Should be matriz[i][j] in place.

  • You’re right, I used #define and it worked. , and yes, I had this typo. Thanks

  • You can use variables to declare the size of vectors and matrices. The matrix will have an automatic allocation and its space will be released as soon as it leaves its variable scope. More on the subject: https://answall.com/a/281116/64969

Browser other questions tagged

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