Thread: Passing 2 dimensional Arrays to functions

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    66

    Passing 2 dimensional Arrays to functions

    No idea how to do that I making a tic tac toe game and I wanna pass the grid to the function

    i have tried pointers but it just comes up with a compiler error like char * can not be converted to char *[][12] any help please

    Ryan

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    If you have a 2D array:

    char tiles[3][3];

    Then you can have a function which recieves the tile:

    void TileFunc(char [][3] _tiles)
    {
    _tiles[0][0] = 0;
    }

    Then you can pass the 'tiles'-array simply using:

    TileFunc(tiles);
    // Gliptic

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Here is a example.

    Try to post some code next time!!!!!!!!!!
    http://www.geocities.com/awjackin352.../CPlusPlus.htm

    //Header files.
    #include< iostream >
    #include< iomanip.h >
    #include< fstream.h >
    #include< math.h >

    //Constant integer to set the size of the matrix.
    const int max=7;

    //Prototypes for all of the functions.
    void creatematrix(int avg[][max], int val);
    void makemagicsquare( int avg[][max], int val);
    void outputmatrix(int avg[][max], ostream& os);



    int main() //Main function starts.
    {
    int avg[max][max]; //Define the array.
    int val=0; //Set the value of each array element
    //to zero
    ofstream outFile("A:/7X7.txt"); //Send the output to the floppy.
    int sum=0; //Set the sum of the values in the
    //last row to zero.
    int col; //Declare the j index as a int.

    //Function calls and the loop to count the last row.
    creatematrix(avg, val);
    makemagicsquare(avg, val);
    outputmatrix(avg, outFile);

    //For loop to count the values in the last row.
    for(col=0; col< max; col++){
    sum+=avg[max-1][col];

    }





    //Output the corners of the matrix and the middle value and the sum of
    //the last row.
    outFile<<"THE VALUE IN THE UPPER LEFT-HAND CORNER IS:"<< avg[0][0]<< endl;
    outFile<<"THE VALUE IN THE UPPER RIGHT-HAND CORNER IS:"<< avg[0][max-1]<< endl;
    outFile<<"THE VALUE IN THE BOTTOM RIGHT-HAND CORNER IS:"<< avg[max-1][max-1]<< endl;
    outFile<<"THE VALUE IN THE BOTTOM LEFT-HAND CORNER IS:"<< avg[max-1][0]<< endl;
    outFile<<"THE VALUE IN THE MIDDLE IS:"<< avg[max/2][max/2]<< endl;
    outFile<<"THE SUM OF THE VALUES IN THE LAST ROW:"<<" "<< sum<< endl;
    //The following for loops manipulate the matrix for the different
    //rotations

    outFile<<"HORIZONTAL ROTATION OF ORIGINAL MATRIX"<< endl;
    for(int row=max;row > 0; row--){
    for( col=0; col < max; col++){

    outFile<< setw(7)<< avg[row-1][col];
    }
    outFile<< endl<< endl;

    }
    outFile<<"VERTICAL ROTATION OF ORIGINAL MATRIX"<< endl;
    for( row=0; row < max; row++){
    for(col=max; col > 0;col--){

    outFile<< setw(7)<< avg[row][col-1];
    }
    outFile<< endl<< endl;
    }
    outFile<< endl;
    outFile<<"LEFT DIAGONAL ROTATION OF ORIGINAL MATRIX"<< endl;
    for( row=0; row < max; row++){
    for(int col=0; col < max; col++){

    outFile<< setw(7)<< avg[col][row];
    }
    outFile<< endl<< endl;
    }
    outFile<< endl;


    outFile<<"RIGHT DIAGONAL ROTATION OF ORIGINAL MATRIX"<<endl;
    for( row=max; row > 0; row--){
    for(col=max; col > 0;col--){

    outFile<< setw(7)<< avg[col-1][row-1];
    }
    outFile<< endl<< endl;
    }
    outFile<< endl;

    return 0;
    }
    //-----------------------------------------------------------------
    //Function to initialize the square to zero.
    void creatematrix(int avg[][max], int val)
    {

    for(int row=0; row < max; row++) //For Loops to initialize the array.
    for(int col=0; col < max; col++)
    {
    avg[row][col]=0; //Set the array to zero.
    }

    }


    //--------------------------------------------------------------------
    //Function that makes the magic Square.
    void makemagicsquare( int avg[][max],int val)
    {

    int valmax=max*max; //Sets up the numbers to increment for
    //the while loop.
    int row=0; //Set the row index to zero.
    int col=0; //Set the column index to zero
    int sum=0; //Set the sum value to zero.
    val++; //Increment val to start at one.


    avg[row][max/2]=val; //Places one in the proper column.
    val++; //Increments val again.
    row=0; //Places i in the proper array.
    col=max/2; //Defines the column position for one.
    int rowindex, colindex; //Redefines the indexes to a different
    //variable.
    while(val <= valmax) //Loop to start assigning the numbers
    //to their proper place in the array.
    {
    rowindex=row; //Redefine the row index
    colindex=col; //Redefine the col index

    row--; //Decrement the row index to be outside
    //of the array
    if(row < 0) //If statement to test the row index.
    {
    row=max-1; //Sets the row index to one less than
    // the max value.
    }

    col--; //Decrement the column index
    if(col < 0) //Test the column index.
    {

    col=max-1; //Sets the column index to one less that
    //the max value

    }
    if(avg[row][col] !=0) //Tests to see if the array is equal to
    //zero.
    {
    row=rowindex; //Redefines the row index
    col=colindex; //Redefines the col index
    row++; //Increment the row index
    avg[row][col]=val; //Assigns the number to the array address.
    }
    else
    {
    avg[row][col]=val; //Assigns the number to the array address.
    }
    val++; //Increments the value for the next loop.
    } //end of while loop


    }


    ///-------------------------------------------------------------------
    //Output function to output all of the functions in the program.
    void outputmatrix(int avg[][max],ostream& os)
    {
    int row;
    int col;

    for(row=0; row < max; row++)
    for(col=0; col < max; col++)
    {

    if(col % max ==0)
    os<< endl<< endl;
    os<< setw(7)<< avg[row][col];

    }
    os<< endl<< endl;

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Here is a example.

    Try to post some code next time!!!!!!!!!!
    http://www.geocities.com/awjackin352.../CPlusPlus.htm
    gliptic is correct!!!!

    //Header files.
    #include< iostream >
    #include< iomanip.h >
    #include< fstream.h >
    #include< math.h >

    //Constant integer to set the size of the matrix.
    const int max=7;

    //Prototypes for all of the functions.
    void creatematrix(int avg[][max], int val);
    void makemagicsquare( int avg[][max], int val);
    void outputmatrix(int avg[][max], ostream& os);



    int main() //Main function starts.
    {
    int avg[max][max]; //Define the array.
    int val=0; //Set the value of each array element
    //to zero
    ofstream outFile("A:/7X7.txt"); //Send the output to the floppy.
    int sum=0; //Set the sum of the values in the
    //last row to zero.
    int col; //Declare the j index as a int.

    //Function calls and the loop to count the last row.
    creatematrix(avg, val);
    makemagicsquare(avg, val);
    outputmatrix(avg, outFile);

    //For loop to count the values in the last row.
    for(col=0; col< max; col++){
    sum+=avg[max-1][col];

    }





    //Output the corners of the matrix and the middle value and the sum of
    //the last row.
    outFile<<"THE VALUE IN THE UPPER LEFT-HAND CORNER IS:"<< avg[0][0]<< endl;
    outFile<<"THE VALUE IN THE UPPER RIGHT-HAND CORNER IS:"<< avg[0][max-1]<< endl;
    outFile<<"THE VALUE IN THE BOTTOM RIGHT-HAND CORNER IS:"<< avg[max-1][max-1]<< endl;
    outFile<<"THE VALUE IN THE BOTTOM LEFT-HAND CORNER IS:"<< avg[max-1][0]<< endl;
    outFile<<"THE VALUE IN THE MIDDLE IS:"<< avg[max/2][max/2]<< endl;
    outFile<<"THE SUM OF THE VALUES IN THE LAST ROW:"<<" "<< sum<< endl;
    //The following for loops manipulate the matrix for the different
    //rotations

    outFile<<"HORIZONTAL ROTATION OF ORIGINAL MATRIX"<< endl;
    for(int row=max;row > 0; row--){
    for( col=0; col < max; col++){

    outFile<< setw(7)<< avg[row-1][col];
    }
    outFile<< endl<< endl;

    }
    outFile<<"VERTICAL ROTATION OF ORIGINAL MATRIX"<< endl;
    for( row=0; row < max; row++){
    for(col=max; col > 0;col--){

    outFile<< setw(7)<< avg[row][col-1];
    }
    outFile<< endl<< endl;
    }
    outFile<< endl;
    outFile<<"LEFT DIAGONAL ROTATION OF ORIGINAL MATRIX"<< endl;
    for( row=0; row < max; row++){
    for(int col=0; col < max; col++){

    outFile<< setw(7)<< avg[col][row];
    }
    outFile<< endl<< endl;
    }
    outFile<< endl;


    outFile<<"RIGHT DIAGONAL ROTATION OF ORIGINAL MATRIX"<<endl;
    for( row=max; row > 0; row--){
    for(col=max; col > 0;col--){

    outFile<< setw(7)<< avg[col-1][row-1];
    }
    outFile<< endl<< endl;
    }
    outFile<< endl;

    return 0;
    }
    //-----------------------------------------------------------------
    //Function to initialize the square to zero.
    void creatematrix(int avg[][max], int val)
    {

    for(int row=0; row < max; row++) //For Loops to initialize the array.
    for(int col=0; col < max; col++)
    {
    avg[row][col]=0; //Set the array to zero.
    }

    }


    //--------------------------------------------------------------------
    //Function that makes the magic Square.
    void makemagicsquare( int avg[][max],int val)
    {

    int valmax=max*max; //Sets up the numbers to increment for
    //the while loop.
    int row=0; //Set the row index to zero.
    int col=0; //Set the column index to zero
    int sum=0; //Set the sum value to zero.
    val++; //Increment val to start at one.


    avg[row][max/2]=val; //Places one in the proper column.
    val++; //Increments val again.
    row=0; //Places i in the proper array.
    col=max/2; //Defines the column position for one.
    int rowindex, colindex; //Redefines the indexes to a different
    //variable.
    while(val <= valmax) //Loop to start assigning the numbers
    //to their proper place in the array.
    {
    rowindex=row; //Redefine the row index
    colindex=col; //Redefine the col index

    row--; //Decrement the row index to be outside
    //of the array
    if(row < 0) //If statement to test the row index.
    {
    row=max-1; //Sets the row index to one less than
    // the max value.
    }

    col--; //Decrement the column index
    if(col < 0) //Test the column index.
    {

    col=max-1; //Sets the column index to one less that
    //the max value

    }
    if(avg[row][col] !=0) //Tests to see if the array is equal to
    //zero.
    {
    row=rowindex; //Redefines the row index
    col=colindex; //Redefines the col index
    row++; //Increment the row index
    avg[row][col]=val; //Assigns the number to the array address.
    }
    else
    {
    avg[row][col]=val; //Assigns the number to the array address.
    }
    val++; //Increments the value for the next loop.
    } //end of while loop


    }


    ///-------------------------------------------------------------------
    //Output function to output all of the functions in the program.
    void outputmatrix(int avg[][max],ostream& os)
    {
    int row;
    int col;

    for(row=0; row < max; row++)
    for(col=0; col < max; col++)
    {

    if(col % max ==0)
    os<< endl<< endl;
    os<< setw(7)<< avg[row][col];

    }
    os<< endl<< endl;

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    He just needed to know how to pass 2D arrays to a function. I showed him that.
    // Gliptic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM