Thread: 2d Arrays

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    1

    Lightbulb 2d Arrays

    Hello I'm confused how to build a 9x9 board using 2d arrays. I attached what my board should look like. I looked at the information online for help but i couldn't really understand. I want to build the board and understand how to print and edit the arrays. If you could help me understand that would be great. this is what i have so far.
    2d Arrays-1-png

    Code:
    #include <stdio.h>
    # define length 9
    # define width 9
    
    
    int main (void){
        int row;
        int col;
        int i=0;
        int count=0;
        int mines;
        int randomseed;
    
    printf("please input your seed: ");
        scanf("%d", &randomseed);
    
    
    printf("please input the number of mines: ");
        scanf("%d", &mines);
    
    //create the gameboard size
        int gameboard[length][width];
    
    //read in the gameboard file
    //for each row
        for(row=0; row<length; row++)
        {
    //for each col
            for(col=0; col<width; col++)
            {
                scanf("%d", &i);
                gameboard[row][col]=i;
            }
        }
    
    //check in the readin file
    //for each row
        for(row=0; row<length; row++)
        {
    //for each col
            for(col=0; col<width; col++)
            {
    //prints an X in place of every 1
                if(gameboard[row][col]==1)
                {
                    printf("X ");
                }
    //prints out the other numbers for how many mines are found by each space
                else
                {
    //finds 0's and checks how many X's are around them
                    if(gameboard[row][col]==0)
                    {
                        count = 0;
                        if(gameboard[row][col+1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row][col-1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row+1][col]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row-1][col]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row+1][col+1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row+1][col-1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row-1][col+1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row-1][col-1]==1)
                        {
                            count=count+1;
                        }
    
                        printf("%d ", count);
                    }
                }
            }
    
            printf("\n");
        }
    
        printf("\n");
        return 0;
    }
    Last edited by Johny Gerard; 06-15-2015 at 09:53 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Just reposting code after formatting since it is hard to read

    Code:
    #include <stdio.h>
    # define length 9
    # define width 9
    
    
    int main (void)
    {
        int row;
        int col;
        int i=0;
        int count=0;
        int mines;
        int randomseed;
    
        printf("please input your seed: ");
        scanf("%d", &randomseed);
    
    
        printf("please input the number of mines: ");
        scanf("%d", &mines);
    
    //create the gameboard size
        int gameboard[length][width];
    
    //read in the gameboard file
    //for each row
        for(row=0; row<length; row++)
        {
    //for each col
            for(col=0; col<width; col++)
            {
                scanf("%d", &i);
                gameboard[row][col]=i;
            }
        }
    
    //check in the readin file
    //for each row
        for(row=0; row<length; row++)
        {
    //for each col
            for(col=0; col<width; col++)
            {
    //prints an X in place of every 1
                if(gameboard[row][col]==1)
                {
                    printf("X ");
                }
    //prints out the other numbers for how many mines are found by each space
                else
                {
    //finds 0's and checks how many X's are around them
                    if(gameboard[row][col]==0)
                    {
                        count = 0;
                        if(gameboard[row][col+1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row][col-1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row+1][col]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row-1][col]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row+1][col+1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row+1][col-1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row-1][col+1]==1)
                        {
                            count=count+1;
                        }
                        if(gameboard[row-1][col-1]==1)
                        {
                            count=count+1;
                        }
    
                        printf("%d ", count);
                    }
                }
            }
    
            printf("\n");
        }
    
        printf("\n");
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need to be careful about how you check the borders. For example, when "col == 0" don't use "[col - 1]" and when "col == width-1" don't use "[col + 1]".
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have not given us enough information to help you. For instance:

    1. What exactly is the input supposed to consist of?
    2. What are the rules for transforming this input into the final result?

    Printing two-dimensional arrays is easy, you just need to use nested loops, of which your code already makes use of in several places. To determine whether to print a number or a symbol, just check the value at each location with an "if()" and print the desired output, based on the value at that location. You'd also want to print a newline after each row - you should be able to reason out where exactly this should be placed.

    Editing arrays is also easy, you just need to assign a value to a particular location. If you want to cycle through the entire array, again you just use nested loops, and assign values to each location as needed.

    I suspect you're supposed to receive input into the array, then cycle through the array and update the values according to a certain set of rules (though this is just a guess since you didn't give us a complete description of the expected program behavior). However, you're not updating the array in your code, you're just printing the result to the screen. You can just change the print statement (printf("%d ", count);) to an assignment (gameboard[row][col] = count;). And, as GReaper mentioned, make sure you don't go outside the bounds of your array.

    Code:
    # define length 9
    # define width 9
    Though this is not wrong, by convention, named constants are typically in all caps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  2. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread