Question: I am working on a Langton's Ant simulation and I want to make sure I am declaring, creating and passing my 2d array properly to

I am working on a Langton's Ant simulation and I want to make sure I am declaring, creating and passing my 2d array properly to functions properly. For my declaration in my class in the private section I have:

char **antBoard; //2d variable

the prototype function in the class public:

void antboard(int bRows, int bCols);

should I be listing the creation of the array in the constructor like this?

Ant::Ant()

{ bRows = 0;

bCols = 0;

maxSteps = 0;

orient = NORTH;

antBoard[bRows][bCols];}

the array initialization function I have like this:

void initializeAntBoard();

the create the array in the implementation file:

// ** dynamic 2D ** creat & Initialization and dynamically allocate memory for the antBoard array based on user inputs

void Ant::board(int bRows, int bCols) //create 2d array dynamically

{

*bRows = bRows;

*bCols = bCols;

char **antBoard = new char *[bRows];

for(int i=0; i

antBoard[i] = new char [bCols];

}

}

To initialize the 2d array:

void Ant::initializeAntBoard()

{

for(int i=0; i

for(int j=0; j

{

antBoard[i][j] = ' ';

}

}

}

Please help me correct my code.

Thank you,

Tracey

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!