Results 1 to 6 of 6
Thread: Nested For Loop algorithm
 01082011, 11:03 PM #1Member
 Join Date
 Jan 2011
 Posts
 1
 Rep Power
 0
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.
 01082011, 11:17 PM #2Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 7
you can do your own sort routine using one of many sort algorithm, or you can use Arrays/(collections) which have the sort() method.
 01092011, 02:35 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.

Also, to the OP, it's always best to show your most recent attempt at a solution when asking these types of questions.
 01112011, 06:44 AM #5Member
 Join Date
 Oct 2010
 Posts
 15
 Rep Power
 0
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; 01112011 at 06:47 AM.
 01112011, 09:52 AM #6Moderator
 Join Date
 Apr 2009
 Posts
 13,378
 Rep Power
 24
Similar Threads

Does Hashmap or Nested Loop use more memory?
By TheGame in forum New To JavaReplies: 2Last Post: 10212010, 09:22 AM 
Nested Loop help  concerning image repetition
By Submerged in forum Java AppletsReplies: 0Last Post: 03052010, 04:57 AM 
can some one help me with nested loop?
By keycoffee in forum New To JavaReplies: 10Last Post: 01252010, 03:49 AM 
nested for loop question
By javabob in forum New To JavaReplies: 3Last Post: 05202008, 11:00 PM 
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07082007, 01:11 PM
Bookmarks