0

I'm trying to fill and print with 2d array, yet every time I try to compile it says undeclared identifier c. And points an arrow at the c in the print function. I tried declaring it many ways yet for some reason it will not work.

#include <iostream>

using namespace std;

void print(char x[][c], int r)
{
    for(int r  = 0; r < 25; r++)
    {
        for(int c = 0; c < 25; c++)
        {
          cout << x[r][c]; 
        } 
        cout << endl;    
    } 
}

int main()
{
    void print(char x[][c], int r);

    char p[25][25];

    for(int r = 0; r < 25; r++)
    {

        for(int c = 0; c < 25; c++)
        {
            if(r==0)p[r][c]='x';
            else    p[r][c]='o';
        }    
    }   

    print(p[25][25]);


    return(0);
}
5
  • Use of undeclared identifier c Commented Jul 23, 2015 at 22:33
  • 1
    what is c?? void print(char x[][c], int r) ?? and in this line void print(char x[][c], int r); Commented Jul 23, 2015 at 22:33
  • What means char x[][c]? Particularly "c"? Try to understand compiler error. Commented Jul 23, 2015 at 22:34
  • You haven't declared c. What is it? Commented Jul 23, 2015 at 22:34
  • 1
    Your code is wrong on so many levels... What do you want to achieve? Could you write the output you want? Commented Jul 23, 2015 at 22:34

2 Answers 2

2

Write the following way

#include <iostream>

using namespace std;


const size_t ROWS = 25;
const size_t COLS = 25;

void print(char x[][COLS], size_t r )
{
    for ( size_t i  = 0; i < r; i++ )
    {
        for ( size_t j = 0; j < COLS; j++ )
        {
          cout << x[i][j]; 
        } 
        cout << endl;    
    } 
}

int main()
{
    char p[ROWS][COLS];

    for ( size_t i = 0; i < ROWS; i++ )
    {
        for ( size_t j = 0; j < COLS; j++ )
        {
            if ( i == 0 ) p[i][j] = 'x';
            else p[i][j] = 'o';
        }    
    }   

    print( p, ROWS );


    return(0);
}

Another approach is to define the function the following way

void print( char ( &x )[ROWS][COLS] )
{
    for ( size_t i  = 0; i < ROWS; i++ )
    {
        for ( size_t j = 0; j < COLS; j++ )
        {
          cout << x[i][j]; 
        } 
        cout << endl;    
    } 
}

and call it like

    print( p );
Sign up to request clarification or add additional context in comments.

2 Comments

can you explain another approach ? I don't know char ( &x )[ROWS][COLS]
@itsnotmyrealname It is a declaration of a reference to a 2D array. For example if you have a declaration int x = 10; then a reference to x is declared like int &rx = x; If you have a 1D array int a[10]; then a reference to the array is declared like int ( &ra )[10] = a;
0

Try this:

#include <iostream>

using namespace std;

void print(char x[25][25])
{
    for(int r  = 0; r < 25; r++)
    {
        for(int c = 0; c < 25; c++)
        {
          cout << x[r][c]; 
        } 
        cout << endl;    
    } 
}

int main()
{
    char p[25][25];

    for(int r = 0; r < 25; r++)
    {
        for(int c = 0; c < 25; c++)
        {
            if(r==0)p[r][c]='x';
            else    p[r][c]='o';
        }    
    }   

    print(p);

    return(0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.