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:
- The number of flights.
- The number of tickets requested per flight.
- If the passenger is from the area of smokers or non-smokers. The program should print at the end of the simulation:
- The average number of passengers per flight.
- The average number of passengers per area on each flight
- 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();
}
The
gdb
(Debugger) can give you at least a sense of where Segfault is going. Try the commandbacktrace
right after the error.– Emoon