# Thread: Nested For Loop algorithm

1. Member
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.

2. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
8
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. 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

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

5. Member
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; 01-11-2011 at 05:47 AM.

6. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
25
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.

#### Posting Permissions

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