# Array trouble

• 11-16-2009, 09:08 PM
hobo
Array trouble
Hi I am trying to do a 2D array to create a ticket reservation sysytem for an airline. I am having trouble understanding 2 of the statements under the for loop .
The statements that say i=rows
j=columns. If I take them out, the whole array is printed but if I leave them in then it prints the first seat. I want to keep them there but I don't fully understand what they do in the code?
Please can someone explain it to me?
thanks.

Code:

```for (int i=0; i<rows; i++) {                                                               for(int j=0; j<columns; j++) {                                                               if(array1[i][j] ==0) {                                                               array1[i][j]=1;                                                               System.out.println("Reserving seat - non-smoking - row: " +(i+1) + " column: "+(j+1));                                                               total_seats--;                                                               i=rows;                                                               j=columns;                                                                     reservednon++;                                                               break;                                                               }                                                             }                                                         }```
• 11-16-2009, 09:21 PM
Fubarable
the outer loop will continue to loop until i is no longer less than rows and the inner will keep looping until j is no longer less than columns. In the loop, when you find an empty seat (the array holds the number 0), you are setting the seat occupied by setting it to 1, and then setting your i and j to their loop maximal values, telling the loop to end. It's kind of a kludge, in my opinion.

By the way, you may wish to post code with a little less indentation as this would make it easier to read.

Much luck.
• 11-16-2009, 09:23 PM
JosAH
@OP: am I correct thinking that you didn't write that code snippet yourself?

kind regards,

Jos
• 11-16-2009, 09:26 PM
hobo
Quote:

Originally Posted by Fubarable
the outer loop will continue to loop until i is no longer less than rows and the inner will keep looping until j is no longer less than columns. In the loop, when you find an empty seat (the array holds the number 0), you are setting the seat occupied by setting it to 1, and then setting your i and j to their loop maximal values, telling the loop to end. It's kind of a kludge, in my opinion.

By the way, you may wish to post code with a little less indentation as this would make it easier to read.

Much luck.

Thanks for the reply. So why do I need to set i and j to their loop maximal values?
• 11-16-2009, 09:34 PM
hobo
Quote:

Originally Posted by JosAH
@OP: am I correct thinking that you didn't write that code snippet yourself?

kind regards,

Jos

I did write it myself. I was just told to put the i=rows and j=columns at the end of the loop. I just don't understand what they actually do.
• 11-17-2009, 10:24 AM
Tolls
Quote:

Originally Posted by hobo
I did write it myself. I was just told to put the i=rows and j=columns at the end of the loop. I just don't understand what they actually do.

Formatting the code a bit better might help:
Code:

```        for (int i = 0; i < rows; i++) {             for (int j = 0; j < columns; j++) {                 if (array1[i][j] == 0) {                     array1[i][j] = 1;                     System.out.println("Reserving seat - non-smoking - row: " + (i + 1) + " column: " + (j + 1));                     total_seats--;                     i = rows;                     j = columns;                     reservednon++;                     break;                 }             }         }```
Presumably you know what the code itself is supposed to do. That is, what it's for. You haven't actually told us, so I'm having to guess a bit here (we're not mind readers).

I'm assuming this bit of code is checking through a seating array looking for a spare seat to reserve. Once it's found a "seat" (that is a 0 in the array...by the way, use meaningful variable names..."array" is not meaningful) it sets that entry in the array to 1, so it won't reserve it again. And then it has to break out of the search for a spare seat. Now, this code is not using a standard way of exiting a loop (I wouldn't have used a for loop for this myself in any case, but hey ho), at least not in my opinion.

So..how do you think it is ensuring it will exit the loops once it has found a seat?
• 11-17-2009, 10:32 AM
r035198x
Quote:

Originally Posted by hobo
I did write it myself. I was just told to put the i=rows and j=columns at the end of the loop. I just don't understand what they actually do.

You were told what to write?
So you simply typed in the characters?
Better read an arrays tutorial so that you will be able to write
the code yourself without anyone telling you what to type in.
• 11-17-2009, 10:59 AM
hobo
Quote:

Originally Posted by Tolls
Formatting the code a bit better might help:
Code:

```        for (int i = 0; i < rows; i++) {             for (int j = 0; j < columns; j++) {                 if (array1[i][j] == 0) {                     array1[i][j] = 1;                     System.out.println("Reserving seat - non-smoking - row: " + (i + 1) + " column: " + (j + 1));                     total_seats--;                     i = rows;                     j = columns;                     reservednon++;                     break;                 }             }         }```
Presumably you know what the code itself is supposed to do. That is, what it's for. You haven't actually told us, so I'm having to guess a bit here (we're not mind readers).

I'm assuming this bit of code is checking through a seating array looking for a spare seat to reserve. Once it's found a "seat" (that is a 0 in the array...by the way, use meaningful variable names..."array" is not meaningful) it sets that entry in the array to 1, so it won't reserve it again. And then it has to break out of the search for a spare seat. Now, this code is not using a standard way of exiting a loop (I wouldn't have used a for loop for this myself in any case, but hey ho), at least not in my opinion.

So..how do you think it is ensuring it will exit the loops once it has found a seat?

Thanks for making it clearer. So the i=rows and j=columns allows it to just print one seat because the loop ends when the max value has been reached.
• 11-17-2009, 01:04 PM
Tolls
Yep.
It's not just printing the seat, though, it's also "reserving" it (ie setting the flag in the array you have). Next time round this chunk of code it wouldn't be able to reserve that seat.