6 Replies - 768 Views - Last Post: 19 April 2013 - 08:15 PM Rate Topic: -----

#1 Stevebass21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

two dimensional array data input

Posted 19 April 2013 - 07:21 PM

Hi all, I have a program I am writing which requires input of data into a two dimensional array. Specifically, I have to display a chess board, and then based on user input allow for a move of a piece into an empty space, and then display the board again. I keep trying different things but I think I am not understanding the concept. What am I doing wrong here? How do I fix this?

Here is what I have right now, it is very rough code:




#include <iostream>
#include <iomanip>
#include <cctype>

//function definitions
void cmove(char (&)[8][8], int, int, char);
void displayboard(char (&) [8][8]);
void showboard(char (&) [8][8]);

int main()
{
    char pieces[8][8] = {{'r', 'n', 'b', 'k','q', 'b', 'n','r'},
                        {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'},
                        {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
                        {'r', 'n', 'b', 'k','q', 'b', 'n','r'}} ;

   char choice = ' ';


      displayboard(pieces);


      cmove(pieces, 8, 8, ' ');


      std::cout << "Your move\n" << std::flush;

      displayboard(pieces);


    return(0);
}

void cmove (char (&eightby)[8][8], int a, int b, char c)
{

   for (a= 0; a<2; a++)
   {
       for (b=0; b<2; b++)
       {std::cout << "enter row: " << std::flush;
        std::cin >> a;
        std::cout << "\n enter column: " << std::flush;
        std::cin >> b;
       }
   }


    eightby[a][b]=c;
    std::cout << "\nchanged\n" << std::endl;

}



void displayboard(char (&byeight)[8][8])
{
    for (int i=0; i<8; i++)
    {
        for (int j=0; j<8; j++)
    {
        std::cout << std::setw(2) << byeight[i][j] << std::flush;
    }
    std::cout << std::endl;
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: two dimensional array data input

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: two dimensional array data input

Posted 19 April 2013 - 07:42 PM

Arrays are naturally passed via pointer, there is not need to specify the ampersand in the function declaration.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: two dimensional array data input

Posted 19 April 2013 - 07:52 PM

Hi, you don't need to use reference variables for the arrays, unless you have been told to.

//function definitions
void cmove(char board[8][8], int, int, char);
void displayboard(char board[8][8]);
//void showboard(char board[8][8]);




The white set could be in upper case.

When you move a piece, you move from a row and column to another row and column (ranks and files in chess). You will need to test that the destination square is empty.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: two dimensional array data input

Posted 19 April 2013 - 07:56 PM

You can also leave out the number of rows in the declaration, 2D arrays are simply one dimensional arrays that are offset-ed by the column width.

//function definitions
void cmove(char board[][8], int, int, char);
void displayboard(char board[][8]);
//void showboard(char board[][8]);


This post has been edited by jjl: 19 April 2013 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 Stevebass21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

Re: two dimensional array data input

Posted 19 April 2013 - 08:06 PM

thanks everyone for the advice so far

As far as actually moving the piece this is what I have:
void cmove (char (&eightby)[8][8], int a, int b, char c)
{

   for (a= 0; a<1; a++)
   {
       for (b=0; b<1; b++)
       {std::cout << "enter row: " << std::flush;
        std::cin >> a;
        std::cout << "\nenter column: " << std::flush;
        std::cin >> b;
        a-=1;
        b-=1;
       }
       eightby[a][b]=c;
   }


Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: two dimensional array data input

Posted 19 April 2013 - 08:12 PM

Quote

thanks everyone for the advice so far

Did you look at our advice? Drop the amperstand in the array paremeter

instead of this
void cmove (char (&eightby)[8][8], int a, int b, char c)



Do this
void cmove (char eightby[][8], int a, int b, char c)


Was This Post Helpful? 0
  • +
  • -

#7 Stevebass21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

Re: two dimensional array data input

Posted 19 April 2013 - 08:15 PM

Yes, I did. Misunderstanding on my part. I thought you meant for the function definitions before main. I will change the code accordingly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1