Thread: C++ Checkerboard

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    8

    C++ Checkerboard

    Hi everyone I need some help

    I am to: For 2 task,you need to apply two nesting if loops iteration for making a grid or matrix of 8 *8 size.So first declare an array of 8*8 ,and use two nested if loops to print out any two values in matrix.something like this you can start with -
    int main(void)
    {
    int Matrix[8][8];
    int a, b, count;
    count = 0;
    for (a = 0; a < 8; a ++) {
    for (b = 0; b < 8; b++) {

    It should look like:
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B

    I just can't seem to get the code right :/ Help please!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Perhaps you should post the rest of your code as well.

    Also, remember to use the [code][/code] tags when posting code.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    I don't have anymore code Salem. It was all I was given and I can't seem to find any where to help. There was another thread

    8 x 8 array checkerboard

    This was the closest I have found but I am not sure how to make it work :/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Start writing your own code, making your own real mistakes - it's the only way to learn.

    Just using google and reading other peoples code won't make you a programmer, any more than going to a restaurant will make you a chef.

    Why don't you try some intermediate steps like

    AAAA
    AAAA
    AAAA
    AAAA

    or
    AAAA
    BBBB
    AAAA
    BBBB
    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.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Basically you just need to figure out the rest of the logic.
    Step inside the nested for loop, and work out what code
    should be next.

    You would eventually want to do the following:

    *Print out the output
    *Ensure every second output is a B - so devise an expression to
    evaluate this will be true for every other output.
    *Remember to place a cout << '\n' at the end so it forms the matrix.

    Hope this helps.

    Wishes Ada xx
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    This is what I have so far.

    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] = 'A';
                if(checker_board[x][y] != ((x+y)%2))
                    checker_board[x][y] = 'B';
     
        getch();
        return 0;
    }
    I was told to put this inside the loop but it's not working for me:

    if ((ctr ++ %2) == 0)
    checker_board[x][y] ='B'
    else
    checker_board[x][y] ='R'

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Like I said, start simple and stop trying to copy what other people have done.

    Forget the checker_board array for the moment, and just try and print something of the right shape using two nested loops.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    I can do this triangle nested loop code.
    Code:
    #include<iostream>using namespace std;
    int main()
    {
       for(int i=0;i<=5;i++){
    
    
            for(int j=0;j<=i;j++)
                {
                    cout<<j;
                }
           cout<<endl;
        }
     return 0;
    }
    So I am wondering if I can turn this into the checkerboard somehow.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Meaghan Winduss
    It should look like:
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    A B A B A B A B
    Are you sure that that striped pattern is what it is supposed to look like, rather than the chequered (checkered) pattern:
    A B A B A B A B
    B A B A B A B A
    A B A B A B A B
    B A B A B A B A
    A B A B A B A B
    B A B A B A B A
    A B A B A B A B
    B A B A B A B A
    ?

    Quote Originally Posted by Meaghan Winduss
    So I am wondering if I can turn this into the checkerboard somehow.
    It looks like you can't, for now

    Take the advice that Salem has offered you: simplify the problem and tackle that. For example, write a program that will use a loop to print:
    A B A B A B A B
    write another program that will use nested loops to print:
    A A A A A A A A
    A A A A A A A A
    A A A A A A A A
    A A A A A A A A
    A A A A A A A A
    A A A A A A A A
    A A A A A A A A
    A A A A A A A A
    From there you will get experience to figure out how you can write code to print according to your assignment's requirements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Yes you are right with your pattern sorry about that laserlight! and yes I will do exactly that (: it's just hard finding tutorials haha

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    I did it! Thank you for your help everybody!

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread