Thread: cannot print out my 2d array correctly! please help

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    cannot print out my 2d array correctly! please help

    im trying to write a program to basically play a concentration game where you pick 2 coordinates on the 2d array and it displays. if they match it stays if not the 2d array display resets back to Xs. anyway i want my board to look like this when you start:

    --1 2 3 4 5 6
    1 X X X X X X
    2 X X X X X X
    3 X X X X X X
    4 X X X X X X
    5 X X X X X X
    6 X X X X X X
    ( the -- is just a space for the thread its not in my code or whatever)
    to do so i created 2 functions. one to create the board and one to display the board:
    Code:
    // Create display function to make 2D array
    char createdisplay()
    {
    	char X = 'X';
    	char bb[7][7] = {{0, 1, 2, 3, 4, 5, 6}, {1, X, X, X, X, X, X}, {2, X, X, X, X, X, X}, {3, X, X, X, X, X, X}, {4, X, X, X, X, X, X}, {5, X, X, X, X, X, X}, {6, X, X, X, X, X, X}};
    	return bb[7][7];
    }
    
    // Display a blank board using char bb[7][7]
    
    void DisArray(char bb[7][7])
    {
    	for (char x=0; x < 7; x++) {
    			for (char y=0; y < 7; y++)
    			cout << bb[x][y] << endl;
        }
    	return;
    }
    now this compiles with a warning about the local variable bb. when i run it i get like 30 lines with just "[" in it and then it ends. whats wrong with my code? whats the correct code to display what i was trying to do? thanks in advance
    Last edited by dalearyous; 04-09-2006 at 04:08 PM.

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    >> char createdisplay()

    >> return bb[7][7];

    You create the array inside the function which in turn returns one charater only, the rest of the array goes to space when you return like that.
    Last edited by LinuxCoder; 04-09-2006 at 04:16 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    so what should the return look like

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    0 is not equal to character '0'
    Code:
    	char bb[7][7] = {{0, 1, 2, 3, 4, 5, 6}, {1, X, X, X, X, X, X}, {2, X, X, X, X, X, X}, {3, X, X, X, X, X, X}, {4, X, X, X, X, X, X}, {5, X, X, X, X, X, X}, {6, X, X, X, X, X, X}};
    all the digits in this line of code equate to various unprintable control characters from the start of the ascii table.

    you also have issues with scope as linux code pointed out. Go back and re-read the chapter on arrays and pointers in your favourite C++ book.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    ok yeah i forgot about that. i messed around and if you change everything to int instead of char i get this:
    0
    1
    2
    3
    4
    5
    6
    1
    88 (X)
    and so on...once i fix my char/int problem how do i make it so these lines show up in multiple columns...not just one?

    so that:
    0 1
    1 88
    2 etc...
    3
    4
    5
    6
    Last edited by dalearyous; 04-09-2006 at 04:54 PM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You'll need to rearrange the entire array then, or build a new "display" array from the current one.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. 2D array project help
    By Cnewbie in forum C Programming
    Replies: 2
    Last Post: 12-10-2001, 09:20 PM