Doubt about matrix values

Asked

Viewed 692 times

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.

  • 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

  • Then you calculate the position instead of going through something, it’s division and subtraction, I think a mutiplication too.

  • I understand...thank you very much friend. I will try here =)

1 answer

1


The code is a little disorganized, does some unnecessary things like make loop to reset the matrix, has unnecessary variable, but it remains to close when it depletes the number of seats, report that the seat is already occupied and obviously occupy the seat, besides not showing a map (ok, the exercise does not ask, but it is 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 dividing 9 by 6 will give 1 (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, I could finish.

The seat calculation is a little more complicated, but not much. Do the same count and then multiply by 6 again to get the position of the seat 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 offset.

#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");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Bigown.... thank you very much! I’ve noticed that your code has valuable hints.. I will try to understand line by line to get the logic used...I believe it is the most important right...I will try to draw the conclusions commenting on the code and put here to check, if possible

  • @Edsts Yes, if you do this you will be in front of a lot of people. What we see most is that the programmer’s apprentice thinks he already knows everything and does not need to improve by looking at how the more experienced do. Not that my way is the only good, it’s interesting to see various styles to shape your. Didactic comments ok, but does not fall in the "comment everything": http://answall.com/a/15566/101. I just saw an error in my code, I forgot a line, repair later.

  • I understand perfectly. I’m a student of systems analysis, second period only. In a way I see that there are students in my class who know less than me, but I always say that what I know is VERY small and basic.. Proof of that was the code I just posted, full of trouble. I’m in the initial learning phase, I know it’s an endless road, because we can always learn and improve more. I have kept in touch with friends more experienced developers and have gotten good tips too...I believe I need first of all to improve programming logic primarily

  • And about the comments, I also appreciate the tip, because I see a lot of people actually replicating that the more you comment, the better.. as I don’t have much experience, I kind of had a tendency to follow this, but with a little bit of distrust too...because it must be strange to pass a legacy code all scrambled right right

  • You did the right thing which is to give good names to variables, this eliminates much the need for comment. The names could be better, but these demonstrate well what is happening. In C indeed some comments are good because it is not the most readable language in the world.

  • Right, in this I always try to give easy names to facilitate understanding Now, about the code : busy int[50][6] = { 0 }; -> Here I understood that to initialize a matrix with a single equal value, simply between keys in this way and int counters = 0; -> Here I noticed that you only used one index access variable and took the other...so I can conclude that I can treat access to the indices of several arrays with a single control variable, right ? (I’ll ask more after these)

  • It got badly organized.. I’d better answer my question with what I’m going to ask ?

  • @Edsts If they are other problems it would be better to ask another question. This initialization only works for 0. The value that goes in the index has no link with anything, it can be a literal or any variable, it can be up to an expression that calculates its value, what matters is that it has the right number there at the right time.

  • Thank you very much, bigown...I am trying to develop the exercise with my way, taking advantage of the tips of logic that left me.. very grateful !

Show 4 more comments

Browser other questions tagged

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