# Nested For Loop algorithm

• 01-08-2011, 11:03 PM
but43r
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.
• 01-08-2011, 11:17 PM
JavaHater
you can do your own sort routine using one of many sort algorithm, or you can use Arrays/(collections) which have the sort() method.
• 01-09-2011, 02:35 PM
JosAH
Quote:

Originally Posted by but43r
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
• 01-09-2011, 02:47 PM
Fubarable
Also, to the OP, it's always best to show your most recent attempt at a solution when asking these types of questions.
• 01-11-2011, 06:44 AM
themulator
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 :)

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

• 01-11-2011, 09:52 AM
Tolls
Quote:

Originally Posted by themulator
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.