-
3 dimentional arrays
how can i do this
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int test[3][3][9] = { 0 };
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
for ( int z = 0; z < 9; z++ )
test[i][j][z] = z + 1;
int counter = 1;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
test[i][j] = counter;
counter++;
}
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
for ( int z = 0; z < 9; z++ )
printf("i = %d j = %d z = %d element = %d\n", i, j, z, test[i][j][z]);
putchar('\n');
}
return 0;
}
i need to make a suduko grid im trying to put the solved squares in co-ordinates i and j and the possible values in z
-
Please stop "initializing" your arrays with when there is no need to.
Sometimes it is useful, often it is not. With your strings it is often covering up mistakes (not properly zero-terminating them in your functions).
Anyway, obviously you can't do this:
Code:
test[i][j] = counter;
test[i][j] is an array of 9 ints.
test[i][j][k] is an int.
I can't tell what you are trying to do so I can't fix it.
-
Code:
foo.c: In function ‘main’:
foo.c:17:24: error: assignment to expression with array type
17 | test[i][j] = counter;
| ^
Compilation exited abnormally with code 1 at Thu Jun 15 09:44:19
test[i][j] is an array of 9 integers, NOT an int!
Compile with FULL warnings turned ON. You are posting code that you have not tested!
-
imagine a grid 3 x 3 (i x j) each square is a number 1 to 9 but each number can only be used once the squares that haven't been filled in have all the available possible values (z)
so the first row could be somthing like 0,0 = 1 0,1 = (2456789) 0,2 = 3
-
I understand the basic idea. I just have no idea what the loop starting at line 14 is supposed to be doing. You've already filled the 3x3 grid of 9-ints with the numbers 1 to 9 in the previous triple loop.
BTW, it's spelled sudoku.
And "dimension" is has an 's', not a 't'.
-
i was proving to myself that assigning a value to test[i][j] didnt change the value test[i][j][z] but instead got the error "error: assignment to expression with array type"
-
It is not possible to assign a value to test[i][j]. It is meaningless. You seem very confused as to how arrays work.
test is an array of 3 arrays of 3 arrays of 9 ints.
test[i] is an array of 3 arrays of 9 ints.
test[i][j] is an array of 9 ints.
test[i][j][k] is an int.
There is a total of 3 * 3 * 9 = 81 ints in test.
Also, sudoku boards are 9 x 9, divided into 3x3 3x3 grids.
Sudoku - Wikipedia
-
so to do what i want to do i will have to have two arrays one 3x3 for the solution and one 3x3x9 for the possible numbers
-
or i guess 1 3 x 3 x 9 and use the k value for the possibles as well as the solved squares
-
Sudoku boards are 9x9.
Sudoku - Wikipedia
Example board:
Code:
5 - - - 1 6 2 - -
- - - 9 8 - 5 - 4
- 3 - - - - - - 9
- - - 6 - - 1 - -
9 - 6 - - - - - 2
- 2 3 - 4 9 - - -
- 4 - - - - - 8 1
3 1 5 2 9 - - 6 7
8 - 9 4 7 1 - 2 5
-
the whole grid will be represented by 9 x 3 x 3 x 9 that way each "square" is addressable so k can be updated easily rather than having to try and work out which value of i and j needs to be updated
-
I'll leave you to it then.
-
-
Not at all. There's just nothing for me to do here. You seem to have it in hand. Give it a go.
I just whipped up a simplistic solver in < 10 minutes that solves that grid I posted.
I did not need lists of numbers. I just tried them all in a 9x9 grid.
This simplistic program would be pretty useless on a difficult grid, though, so maybe your number lists will help.
IIRC, expert solvers are quite complicated.
Code:
5 9 4 3 1 6 2 7 8
6 7 1 9 8 2 5 3 4
2 3 8 7 5 4 6 1 9
4 8 7 6 2 5 1 9 3
9 5 6 1 3 7 8 4 2
1 2 3 8 4 9 7 5 6
7 4 2 5 6 3 9 8 1
3 1 5 2 9 8 4 6 7
8 6 9 4 7 1 3 2 5
-
Some symbolic constants, and slightly better loop variable names would go a long way to understanding.
Code:
#include <stdio.h>
// The dimensions of the smaller square
#define REGION_W 3
#define REGION_H 3
// The dimensions of the larger square, number of regions
#define SQUARE_W 3
#define SQUARE_H 3
int main(void) {
int game[SQUARE_H][SQUARE_W][REGION_H][REGION_W];
for ( int sh = 0 ; sh < SQUARE_H ; sh++ ) {
for ( int sw = 0 ; sw < SQUARE_W ; sw++ ) {
for ( int rh = 0 ; rh < REGION_H ; rh++ ) {
for ( int rw = 0 ; rw < REGION_W ; rw++ ) {
game[sh][sw][rh][rw] = rh * 3 + rw + 1;
}
}
}
}
// now print the whole board.
// note how the loops are rearranged!
printf("-------------------------\n");
for ( int sh = 0 ; sh < SQUARE_H ; sh++ ) {
for ( int rh = 0 ; rh < REGION_H ; rh++ ) {
printf("| ");
for ( int sw = 0 ; sw < SQUARE_W ; sw++ ) {
for ( int rw = 0 ; rw < REGION_W ; rw++ ) {
printf("%d ", game[sh][sw][rh][rw]);
}
printf("| ");
}
printf("\n");
}
printf("-------------------------\n");
}
return 0;
}
$ ./a.out
-------------------------
| 1 2 3 | 1 2 3 | 1 2 3 |
| 4 5 6 | 4 5 6 | 4 5 6 |
| 7 8 9 | 7 8 9 | 7 8 9 |
-------------------------
| 1 2 3 | 1 2 3 | 1 2 3 |
| 4 5 6 | 4 5 6 | 4 5 6 |
| 7 8 9 | 7 8 9 | 7 8 9 |
-------------------------
| 1 2 3 | 1 2 3 | 1 2 3 |
| 4 5 6 | 4 5 6 | 4 5 6 |
| 7 8 9 | 7 8 9 | 7 8 9 |
-------------------------
The loops might be in different orders, but the subscripts are always in the same order.