Don’t understand how to solve this.
I need to accept a seat number as a parameter (seat). And then convert that seat number into a row and column index for the two-dimensional array.
to check myself I’ve been assuming there are 10 rows (0-9) and 2 seatsPerRow (0-1) and picking a seat from 1-20 out of that to check the math. BUT There can be any amount of rows and columns. This is just what I’m using to check myself.
static void isAvailable(int seat, boolean seat[][]) {
int row = 0;
int column = 0;
if (seat > 0 && seat <= (getRows() * getSeatsPerRow())){
row = (seat ) % getRows();
column = (seat - 1) % getSeatsPerRow;
seat[row][column] = false;
}
}
Assuming the seats were arranged this way:
seatsPerRow
0 1 2 3 4 5 6
0 (1) (2) (3) (4) (5) (6) (7)
r 1 (8) (9) (10)(11)(12)(13)(14)
o 2 (15)(16)(17)(18)(19)(20)(21)
w 3 ...
s 4 ...
.
.
.
Say I wanted to find seat 11. It would be seat[1][3]. I don’t understand how to convert the seat number to the location on the 2-d array.
Well, assuming seats are numbered starting at 1, you can get the column index simply by using:
You can then find the row with a simple expression:
Then, you’d get the actual element by doing:
Both these expressions should work for all dimensions of the
seatarray (except 0 x 0).The first line will use modulo to find the remainder of
seatNum / seat[0].length, or basically the seat number divided by the number of seats per row. This will restructcolumnto the correct range.Next, the second line will divide the number of seats by the length of a row, which will find the row itself. We then cast it to an int with
(int), which truncates it, dropping any decimal that may have resulted from the operation. In this case, we’re actually performing purely integer math, so the cast is unnecessary.