1. ## just for fun...

Here's a little problem I came across. If you have the time and are interested...

Populate a 2-D array exactly as shown below:
Code:
```1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8```
Provide the simples solution possible. Only stipulation is that the values must be in an array (no classes).

Here's my best stab at it:
Code:
```for( int i = 0; i < 9; i++ ){

for( int j = 0; j < (9-i); j++ )
mArray[i][j] = j+i+1;

for( int k = 0; k < i; k++ )
mArray[i][(9-i)+k] = k+1;
}```

2. Static initialization would be the simplest.

Did you "come across" this problem, or was it assigned?

gg

3. One loop. (But probably less efficient than two loops, really)
Code:
```int x = 0;
for( int i = 0; i < 9; i++ ){

for( int j = 0; j < 9; j++)
mArray[i][j] = x % 9 + 1;
x++;
}```
--
Mats

4. Originally Posted by Codeplug
Static initialization would be the simplest.

Did you "come across" this problem, or was it assigned?

gg
I'm exploring some options using java swt to provide the gui for an app whose nuts and bolts are in c++. as a small example, i began playing around with creating a very simplistic sudoku game. to test things out, i needed a simple a 9X9 array where every row was different from the other rows, every column diff from the other cols, and the 9 3X3 boxes all different from each othe as well. So... i went off on a tangent and played around with this. Ultimatley I'm wondering if there is a substantially simpler way to do this like cutting it down to 2 "for" loops and s single assignment? Or is it simply mathematically impossible?

5. Originally Posted by matsp
One loop. (But probably less efficient than two loops, really)
Code:
```int x = 0;
for( int i = 0; i < 9; i++ ){

for( int j = 0; j < 9; j++)
mArray[i][j] = x % 9 + 1;
x++;
}```
--
Mats
That gives me the following output:
Code:
```1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9```

6. Personally, I agree with Codeplug and prefer initialisation:
Code:
```int numbers[9][9] = {
{1, 2, 3, 4, 5, 6, 7, 8, 9},
{2, 3, 4, 5, 6, 7, 8, 9, 1},
{3, 4, 5, 6, 7, 8, 9, 1, 2},
{4, 5, 6, 7, 8, 9, 1, 2, 3},
{5, 6, 7, 8, 9, 1, 2, 3, 4},
{6, 7, 8, 9, 1, 2, 3, 4, 5},
{7, 8, 9, 1, 2, 3, 4, 5, 6},
{8, 9, 1, 2, 3, 4, 5, 6, 7},
{9, 1, 2, 3, 4, 5, 6, 7, 8}};```
Originally Posted by dudeomanodude
That gives me the following output:
Fix the bug, e.g.,
Code:
```for( int i = 0; i < 9; i++ ) {
int x = i;
for( int j = 0; j < 9; j++) {
mArray[i][j] = x++ % 9 + 1;
}
}```

7. matsp's example was what i was looking for, thanks. i understand how static init is more efficient, but it's not something fun to play around with

8. btw, it's disgusting how good you guys are.

9. Perhaps a little simpler:
Code:
```for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
array[i][j] = (i+j) % 9 + 1;
}
}```
(EDIT: Previous "purely gross" example excised due to incorrectness.)

10. ^_^

All of you are forgetting the "obvious" version. (That is probably the slowest.)

Soma

Code:
```for(unsigned int z(0); 81 > z; ++z)
{
grid[z / 9][z % 9] = ((z % 9) + (z / 9)) % 9 + 1;
}```