Results 1 to 9 of 9
Thread: Array trouble
- 11-16-2009, 08:08 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
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.
Java 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; } } }
-
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, 08:23 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
@OP: am I correct thinking that you didn't write that code snippet yourself?
kind regards,
Jos
- 11-16-2009, 08:26 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
- 11-16-2009, 08:34 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
- 11-17-2009, 09:24 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Formatting the code a bit better might help:
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).Java 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; } } }
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, 09:32 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 11-17-2009, 09:59 AM #8
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
- 11-17-2009, 12:04 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Having trouble insert/sorting array values w/ binary searching.
By bh-chobo in forum New To JavaReplies: 2Last Post: 10-07-2009, 06:24 PM -
Here comes trouble... :-)
By sargehendricks in forum IntroductionsReplies: 1Last Post: 04-23-2009, 03:18 PM -
[SOLVED] Array trouble....
By AngrYkIdzrUlE in forum New To JavaReplies: 9Last Post: 04-18-2009, 10:18 PM -
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-21-2008, 11:52 PM -
Having trouble with array
By ice22 in forum New To JavaReplies: 3Last Post: 11-13-2007, 03:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks