Thread: Multidimensional array in C

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    5

    Multidimensional array in C

    Hey, Im having trouble understanding how this function prints out this board. mainly how the 2d array is equal in 1 or 2 or 3 or 0.
    Code:
    void drawboard(int board[6][7]){
        int i;
        int j;
        printf("   1   2   3   4   5   6   7  \n");
        for(i = 5; i >= 0; i = (i - 1))
        {
            for(j = 0; j < 7; j = (j + 1))
            {
                printf(" | ");
                if(board[i][j] == 1)
                {
                    printf("X");
                }
                else if(board[i][j] == 2)
                {
                    printf("O");
                }
                else if(board[i][j] == 3)
                {
                    printf("4");
                }
                else if(board[i][j] == 0);
                {
                    printf(" ");
                }
            }
            printf(" | \n");
        }
        printf(" ----------------------------- \n");
    }
    This function prints out a board for connect 4, and im trying to update it in a turn based way by calling it after I ask a user for what column he wants to drop the piece. So he can choose columns 1 throught 7.
    So Im going to update my array to
    board[]["whatever column he chose"]. I dont know what to do for the row.
    And then the function will take the board and hopefully drop the piece

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Look at the incremental sections of each for loop. Math involved be it 'i' or 'j' would represent the total value of the rows and columns. Thus, the 'if' statement would return true if the total value of the 'i' + 'j' was equal to either 1, 2, 3 etc..

    Of course, this is just a guess at what you mean as there is no other code present. I am taking on belief the sum values of each calculation would determine where the piece would drop.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with multidimensional array
    By tmdm in forum C Programming
    Replies: 2
    Last Post: 04-07-2011, 04:11 PM
  2. multidimensional array help
    By dg862 in forum C Programming
    Replies: 22
    Last Post: 02-19-2009, 01:33 AM
  3. help with multidimensional array
    By keira in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2008, 11:28 AM
  4. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  5. multidimensional array
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2002, 09:17 PM

Tags for this Thread