CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: 2d Arrays

  1. #1
    Join Date
    Nov 2008
    Posts
    2

    2d Arrays

    i was wondering how do print a 2d array that would look like a square?

    char theBoard[24][24]; //global variable

    //code below is storing random char values in the in the array
    //the output for this code would print all the values of the strings in single line
    void random(char& random)
    {

    srand(1);
    srand( time(0));
    int i=rand()%7;
    switch(i)
    {
    case 1:
    random = '#';
    break;
    case 2:
    random = 'o';
    break;
    case 3:
    random = '!';
    break;
    case 4:
    random = '+';
    break;
    case 5:
    random = '~';
    break ;
    case 6:
    random = '.';
    break;
    }
    }

    void draw()
    {
    char Random = ' ';
    for(int i = 0;i<24;i++)
    {
    for(int j=0;j<24;j++)
    {

    random(Random);
    theBoard[i][j]=Random;
    cout<<theBoard[i][j];
    }
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: 2d Arrays

    Code:
    void draw()
    {
    char Random = ' ';
    for(int i = 0;i<24;i++)
    {
    for(int j=0;j<24;j++)
    {
    
    random(Random);
    theBoard[i][j]=Random;	
    cout<<theBoard[i][j];
    }
    cout << "\n ";
    }
    }

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: 2d Arrays

    • Use CODE tags !!!
    • Why doesn't random return the char?
    • What about case 0?
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured