Results 1 to 9 of 9

Thread: Array trouble

  1. #1
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default 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;
    							       }
    							    }
    							}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

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

    kind regards,

    Jos

  4. #4
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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?

  5. #5
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    @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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by hobo View Post
    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:
    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;
                    }
                }
            }
    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?

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by hobo View Post
    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.

  8. #8
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Formatting the code a bit better might help:
    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;
                    }
                }
            }
    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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    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.

Similar Threads

  1. Replies: 2
    Last Post: 10-07-2009, 06:24 PM
  2. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 03:18 PM
  3. [SOLVED] Array trouble....
    By AngrYkIdzrUlE in forum New To Java
    Replies: 9
    Last Post: 04-18-2009, 10:18 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM
  5. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •