Doubt about matrix values

1
  

A good example of a data set is the booking of a flight ticket. Build a C program for booking airline tickets. The plane has 50 rows with 6 seats each. The program should have:

     
  • Two vectors whose number of positions is the total number of seats in the airplane.

  •   
  • In a vector will be registered the name of the passenger of each seat and the other, the number of the seat.

  •   
  • A matrix to represent each seat. If the seat is occupied, the value of 1 (one) will be stored in the position of the matrix. If the seat is free, 0 (zero) will be stored in the array.

  •   

Initialize all array positions with a value of 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 most recommended to traverse the array and assign the value 1 to exactly the position entered in the variable seat? How to get exactly in each position?

I think I would use for to go through, but I can not yet implement a way to relate the seat number to the position in the array.

    
asked by anonymous 24.02.2017 / 22:55

1 answer

1

The code is a bit unorganized, it does some unnecessary things like tying the matrix, it has unnecessary variables, but it is necessary to close when the number of seats is exhausted, to inform that the seat is already occupied and obviously occupy the seat, besides of not showing a map (okay, exercise does not ask, but it's the way to see if everything is right).

To know the row is easy, just divide by 6. You have to subtract 1 because the array starts at 0. Rounding the integer will ensure that the number is round. if you divide 9 by 6 it will give 1 (it would be 1.5, but a int has no decimal places).

I did as I was, but I think it would be better not to ask if you want to continue, if the seat was 0, it could end.

The calculation of the seat is a bit more complicated, but not much. Do the same account and then multiply by 6 again to get the seat position as if it had no rows. Take the seat number and subtract from the position found in the previous account, then you find the seat displacement.

#include <stdio.h>

int main () {
    int assentos[300];
    char nomes[300][15];
    int ocupados[50][6] = { 0 };
    int contadorAssentos = 0;
    char escolha;
    do { 
        printf("\nDigite o nome do passageiro: ");
        scanf("%s", nomes[contadorAssentos]);
        printf("\nDigite o numero do assento requerido: ");
        scanf("%d", &assentos[contadorAssentos]);
        printf("\nNome do passageiro: %s", nomes[contadorAssentos]);
        printf("\nAssento escolhido: %d", assentos[contadorAssentos]);
        int fileira = (assentos[contadorAssentos] - 1) / 6;
        int assento = assentos[contadorAssentos] - 1 - fileira * 6;
        if (ocupados[fileira][assento]) {
            printf("\nO assento já está ocupado, escolha outro!");
        }
        ocupados[fileira][assento] = 1;
        if (contadorAssentos++ == 300) {
            break;
        }
        printf("\nDeseja continuar? <S/N>");
        scanf(" %c", &escolha);
    } while (escolha == 'S' || escolha == 's');
    printf("\n");
    for (int i = 0; i < 50; i++) {
        for (int j = 0; j < 6; j++) {
            printf("F%02dA%d=%s | ", i + 1, j + 1, ocupados[i][j] ? "ocupado" : "livre  ");
        }
        printf("\n");
    }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
25.02.2017 / 00:12