1
A good example of a data set is booking a flight tickets. Build a C program for booking plane tickets. The plane has 50 rows with 6 seats each. The program must have:
Two vectors whose number of positions is the total number of seats of the aeroplane.
In one vector will be registered the name of the passenger of each seat and in the other, the number of the seat.
A matrix to represent each seat. If the seat is occupied, the value 1 (one) will be stored in the matrix position. If the seat is free, the value 0 (zero).
Initialize all array positions to 0 (zero ).
#include <stdio.h>
#include<string.h>
main () {
int assentos[300] ; //Vetor com número de assentos
char nomes[300][15] ; //Matriz para os nomes de cada passageiro
int contadorAssentos = 0 ; //Variável de acesso ao índice do vetor assentos
int contadorNomes = 0 ; //Variável de acesso a matriz nomes
int ocupados[50][6] ; //Matriz para cada assento
char escolha ; //Variável de escolha para prosseguir com o programa ou não
int i,j ; //Variável de controle da matriz de assentos
// Preenchendo e imprimindo a matriz de assentos ocupados ou não com zero para verificação
for ( i = 0 ; i <= 49; i++) {
for (j = 0 ; j <= 5 ; j++) {
ocupados[i][j] = 0 ;
printf ("%d", ocupados[i][j]) ;
}
printf ("\n");
}
//Fim loop de preenchimento
//Inicia programa
do {
printf ("\nDigite o nome do passageiro : ") ;
scanf("%s", &nomes[contadorNomes]) ;
printf("\nDigite o numero do assento requerido : ");
scanf("%d", &assentos[contadorAssentos]) ;
printf("\nNome do passageiro : %s", nomes[contadorNomes]) ;
printf("\nAssento escolhido : %d",assentos[contadorAssentos]) ;
contadorAssentos ++ ;
contadorNomes ++ ;
printf("\n\nDeseja continuar ? <S/N>") ;
scanf(" %c", &escolha) ;
}
while ((escolha == 'S')|| (escolha == 's')) ;
}
What would be more recommended to go through the matrix and assign the value 1 exactly to the position given in the seat variable? How to get exactly in each position?
I think I’d use a for
to go through, but I still can’t implement a way to relate the seat number to the position in the matrix.
Enough math, unless I didn’t understand.
– Maniero
Good evening, thanks for replying! Well, I figured I could go through the array and play at the specific position with for.. But my question is how to make a logic out of that to turn the position of the informed seat into position in the matrix.. For example... but my doubt is precisely because the user will only enter one parameter, which is the number stored in the vector, and to access the matrix would have to use two. [i][j].. can really be mathematical, to try
– Edsts
Then you calculate the position instead of going through something, it’s division and subtraction, I think a mutiplication too.
– Maniero
I understand...thank you very much friend. I will try here =)
– Edsts