8 x 8 array checkerboard

This is a discussion on 8 x 8 array checkerboard within the C Programming forums, part of the General Programming Boards category; I am trying writing a program that declares an 8 x 8 char array and uses a loop to store ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    8 x 8 array checkerboard

    I am trying writing a program that declares an 8 x 8 char array and uses a loop to store the following data into the array.
    (I am trying to create a checkerboard)

    B R B R B R B R
    R B R B R B R B
    B R B R B R B R
    R B R B R B R B
    B R B R B R B R
    R B R B R B R B
    B R B R B R B R
    R B R B R B R B

    This is what I have so far. Keep getting errors.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int checker_board[8][8];
        int x, y;
    
        for(x = 0; x < 8; x++)
            for(y = 0; y < 8; y++)
                if(checker_board[x][y] == ((x+y)%2))
                    checker_board[x][y] = 'B';
                if(checker_board[x][y] != ((x+y)%2))
                    checker_board[x][y] = 'R';
                printf(checker_board[x][y]);
    
        getch();
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Chatham Chance View Post
    This is what I have so far. Keep getting errors.
    What sort of errors?

    At a glance, printf is going to yell at you for using the wrong argument for its first argument, and you probably want to throw in a pair of braces around your for loop segments.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You could replace all the if/else munging with:

    Code:
    register unsigned short count = 0;
    
    /* ... */
    
    for (x = 0; x < 8; x++) {
        for (y = 0; y < 8; y++) {
            if ((count++ % 2) == 0)
                checker_board[x][y] = 'B';
            else
                checker_board[x][y] = 'R';
        }
    }
    Edit: Nesting for loops is usually more expensive than just using one. Because you're doing such a simple patterned fill, it might be easier to set the array with only one loop to 64 (that's how 2d arrays are laid out in memory). Just an idea, you don't have to use it.
    Last edited by memcpy; 03-04-2012 at 02:18 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Chatham Chance View Post
    This is what I have so far. Keep getting errors.
    Always a good idea to describe what those errors are.

    1) You are testing against the (uninitialized) values in the matrix with your modulus, as opposed to the current position in the matrix.
    2) You need to use an enclosing block if you want to include the printf() in the inner loop.
    3) The printf will not do what you want unless you format it properly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Here's a good way to set your array:
    Code:
    for (x = 0; x < 8; x++)
        for (y = 0; y < 8; y++)
            checker_board[x][y] = (x + y) % 2 ? 'R' : 'B';
    @memcpy: If you're going to use an extra variable here, better to simply do this
    Code:
    int count = 0;
    than this
    Code:
    register unsigned short count = 0;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by memcpy View Post
    Code:
    register unsigned short count = 0;
    The use of "register" is atavistic. Modern compilers will completely ignore it, except for applying by-the-standard limitations (you can't take the address). They decide what really goes in a register and what doesn't -- you don't get any say in the matter.

    Edit: Nesting for loops is usually more expensive than just using one. Because you're doing such a simple patterned fill, it might be easier to set the array with only one loop to 64 (that's how 2d arrays are laid out in memory). Just an idea, you don't have to use it.
    Likewise, modern compilers will optimize this way automatically anyway when they see the nested loops. So while the intention is good, this is almost certainly not worth the hassle (and the obfuscating effect on the code).
    Last edited by MK27; 03-04-2012 at 03:32 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Modern compilers will completely ignore it
    Not necessarily. Well, they definitely don't ignore it if you're more specific:
    Code:
    register unsigned short count asm ("ax");
    , but this is a horrible idea to shove a variable into a random register if the rest of your function doesn't use registers. How I learned it, the keyword "register" is simply a suggestion to put the variable in a register, and the compiler will choose whether this is necessary or not. To ensure that it actually ends up in a specific register, using the above code most likely will serve that purpose.

    > (and the obfuscating effect on the code).
    Good point.. haha.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    How would I need to fix my print statement to print 'R' and 'B'

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The printf function needs a "format" string as the first argument.
    Code:
    printf("%c", checker_board[x][y]);
    And presumably checker_board should be char instead of int.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    To make the 8x8 shape, you could try this:

    Code:
    for ( ... ) {
        for ( ... ) {
            printf("%c ", checker_board[x][y]); /* notice the space */
        }
        printf("\n"); /* and the newline character after 8 */
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll want each square of the board to be shown as the center of a 3 x 3 char square:
    Code:
    --------------------------------------------------------
    |blank row
    | B | R |                                 
    |blankrow
    --------------------------------------------------------
    Possibly, without the blank rows in there - depending on the size of the board you want to display.

    You should google and go to the Chess Wiki. Checkers and chess programs share a lot in common, and the chess wiki has a lot of info.
    Last edited by Adak; 03-04-2012 at 10:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grapics question checkerboard
    By d2rover in forum C# Programming
    Replies: 1
    Last Post: 04-07-2011, 02:53 PM
  2. Random Checkerboard Help
    By ninety3gd in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2009, 11:11 PM
  3. Checkerboard
    By Ducky in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 05:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Checkerboard pattern
    By magnum38 in forum Tech Board
    Replies: 4
    Last Post: 08-17-2006, 02:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21