Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working on a Langton\'s Ant simulation and I want to make sure I am declari

ID: 3708531 • Letter: I

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 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<bRows; i++){

           antBoard[i] = new char [bCols];

       }

}

To initialize the 2d array:

void Ant::initializeAntBoard()

{

   for(int i=0; i<bRows; i++){

       for(int j=0; j <bCols; j++)

       {

           antBoard[i][j] = ' ';

       }

   }

  

}

Please help me correct my code.

Thank you,

Tracey

Explanation / Answer

In the constructor, you are creating 2D array of size 0x0, which is incorrect. Please select a default size which is greater than 0.

Code

#include<iostream>

using namespace std;

class Ant

{

    int bRows;

    int bCols;

    int maxSteps;

    char **antBoard;

   

    public:

       

        // constructor

        Ant()

        {

            this->bRows = 0;

            this->bCols = 0;

            this->maxSteps = 0;

            //orient = NORTH;

           

            // allocate memory to rows

            this->antBoard = new char*[bRows];

           

            int i;

           

            // allocate memmory to columns

            for( i = 0 ; i < this->bRows ; i++ )

                this->antBoard[i] = new char[ this->bCols ];

        }

       

        void initializeAntBoard()

        {

            int i, j;

           

            // traverse rows

            for(i = 0 ; i< bRows ; i++)

                // traverse columns

                for(j = 0; j < bCols ; j++)

                    antBoard[i][j] = ' ';

        }

       

        void board(int bRows, int bCols)

        {

            this->bRows = bRows;

            this->bCols = bCols;

           

            // allocate memory to rows

            this->antBoard = new char*[bRows];

           

            int i;

           

            // allocate memmory to columns

            for( i = 0 ; i < this->bRows ; i++ )

                this->antBoard[i] = new char[ this->bCols ];

        }

};

int main()

{

   

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote