Results 1 to 6 of 6
  1. #1
    but43r is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Nested For Loop algorithm

    Hi guys.

    My head is gone tonight and I hoped someone would help me to solve this Nested for loop algorithm.

    Basically what I want is nested for loop that would sort an array of numbers like this:

    12345678
    22345678
    33345678
    44445678
    55555678
    66666678
    77777778
    88888888

    Any help would be appreciated.

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    you can do your own sort routine using one of many sort algorithm, or you can use Arrays/(collections) which have the sort() method.

  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

    Quote Originally Posted by but43r View Post
    Hi guys.

    My head is gone tonight and I hoped someone would help me to solve this Nested for loop algorithm.

    Basically what I want is nested for loop that would sort an array of numbers like this:

    12345678
    22345678
    33345678
    44445678
    55555678
    66666678
    77777778
    88888888

    Any help would be appreciated.
    What is there to be sorted? If the top left corner has coordinate (0, 0) (x moving to the right, y moving downwards in their positive directions) the value in each grid cell is Math.max(x+1, y+1). Simply fill in each cell with those values and you're done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Also, to the OP, it's always best to show your most recent attempt at a solution when asking these types of questions.

  5. #5
    themulator is offline Member
    Join Date
    Oct 2010
    Posts
    15
    Rep Power
    0

    Default

    The solution is below, but before you look at it, think about it first.
    How many times are you printing the first number of each line?
    Are there any patterns?

    I had this problem about a few months ago too, but it really shouldn't be that much of a challenge. Try to code it before looking at the solution below :)
























    Java Code:
    for(int x = 1; x <= 8; x++){
         for(int y = 1; y <= 8; y++){
             if (y<=x) array[x][y] = x;
             else array[x][y] = y;
         }
    }
    Last edited by themulator; 01-11-2011 at 05:47 AM.

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

    Default

    Quote Originally Posted by themulator View Post
    The solution is below, but before you look at it, think about it first.
    How many times are you printing the first number of each line?
    Are there any patterns?

    I had this problem about a few months ago too, but it really shouldn't be that much of a challenge. Try to code it before looking at the solution below :)
    You do realise that an awful lot of students will simply take that code (whether it works or not I have no idea) and use it without doing the brain work up front.

    Or, to put it another way, don't spoonfeed. It's considered bad form here.

Similar Threads

  1. Does Hashmap or Nested Loop use more memory?
    By TheGame in forum New To Java
    Replies: 2
    Last Post: 10-21-2010, 09:22 AM
  2. Nested Loop help -- concerning image repetition
    By Submerged in forum Java Applets
    Replies: 0
    Last Post: 03-05-2010, 03:57 AM
  3. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 02:49 AM
  4. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Posting Permissions

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