Thread: 3 dimentional arrays

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    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
    Last edited by cooper1200; 06-15-2023 at 07:27 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Please stop "initializing" your arrays with
    Code:
    = {0}
    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.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    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!

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    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

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    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'.
    Last edited by john.c; 06-15-2023 at 08:20 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    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"

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    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
    Last edited by john.c; 06-15-2023 at 08:44 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    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

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    or i guess 1 3 x 3 x 9 and use the k value for the possibles as well as the solved squares

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    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
    A little inaccuracy saves tons of explanation. - H.H. Munro

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    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

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I'll leave you to it then.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    is that wrong then

  14. #14
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    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
    A little inaccuracy saves tons of explanation. - H.H. Munro

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with multi-dimentional arrays in C
    By thebenman in forum C Programming
    Replies: 3
    Last Post: 11-01-2014, 09:13 AM
  2. 2 dimentional array
    By gameover6005 in forum C Programming
    Replies: 2
    Last Post: 04-16-2013, 09:07 PM
  3. Accessing column data in multi-dimentional arrays
    By Darkmentor in forum C Programming
    Replies: 5
    Last Post: 11-30-2012, 11:51 AM
  4. 3 Dimentional string Arrays
    By paulroseby in forum C Programming
    Replies: 3
    Last Post: 11-27-2002, 06:53 AM
  5. moving n-dimentional arrays in c++
    By kknla in forum C++ Programming
    Replies: 0
    Last Post: 02-06-2002, 05:15 PM

Tags for this Thread