Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8581435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:02:56+00:00 2026-06-11T21:02:56+00:00

I have a 2D fixed size object array of spot class Spot map[row][col];//row &

  • 0

I have a 2D fixed size object array of “spot” class
Spot map[row][col];//row & col are dynamic changed integer
I want to pass it to a function
bool isFilled(int row,int col,Spot[row][col]){}
How to define the function? How to delete this array ?Please see my code. Thanks for your help.

Spot.h

    #ifndef SPOT_H
#define SPOT_H

class Spot
{
private:
    bool isBunny;
    int nextCycle;
public:
    static const int UP = 0;
    static const int RIGHT = 1;
    static const int DOWN = 2;
    static const int LEFT = 3;
    static const int SLEEP = 4;

    virtual void setSpot(bool newIsBunny);
    Spot();
    ~Spot();
    virtual int getNextCycle();
    virtual void setNextCycle();
    virtual bool getIsBunny();
    virtual void makeBunny();
};

void Spot::setSpot(bool newIsBunny)
{
    isBunny = newIsBunny;
    nextCycle = UP;
}

Spot::Spot()
{
    isBunny = false;
    nextCycle = UP;
}

Spot::~Spot()
{
}

void Spot::setNextCycle()
{
    if (nextCycle != SLEEP)
    {
        nextCycle++;
    }
}

int Spot::getNextCycle()
{
    return nextCycle;
}

bool Spot::getIsBunny()
{
    return isBunny;
}

void Spot::makeBunny()
{
    if (!isBunny)
        nextCycle = UP;
    isBunny = true;
}


#endif  /* SPOT_H */


Bunny.cpp



#include "Spot.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <boost/algorithm/string.hpp>


using namespace std;


static string line;
static ifstream inFile;
static ofstream outFile;
bool isFilled(int x, int y, Spot **myMap);

int main () {
  int numSims = 0;
  inFile.exceptions ( ifstream::failbit | ifstream::badbit );

  try {
    inFile.open ("/home/mike/Desktop/input.txt");
    outFile.open ("/home/mike/Desktop/output.txt");
   // while(!inFile.eof())
      {

         getline (inFile,line);
        numSims= atoi(line.c_str());
       //cout<<"numSims: "<<numSims<<endl;
        for (int i = 0;i < numSims;i++)
            {
                int minPerCycle = 1;
                getline (inFile,line);
                minPerCycle= atoi(line.c_str());

            //cout << "minPerCycle: "<<minPerCycle <<endl;

                int row = 0;
                int col = 0;
                    getline (inFile,line);
                    std::vector<std::string> xy;
                    boost::split(xy, line, boost::is_any_of(" "));
                    row=atoi(xy.at(0).c_str());
                    col=atoi(xy.at(1).c_str());
                    //cout <<"row: "<< row<<endl;
                    //cout << "col: "<<col<<endl;


                    Spot** myMap = new Spot* [col];
                    for(int i = 0; i < col; ++i)
                        myMap[i] = new Spot [row];

                    //std::vector<std::vector<Spot> > myMap(x, std::vector<Spot>(y));
                    for (int i = 0;i < row;i++)
                            {
                                getline (inFile,line);
                                //cout<<line<<endl;
                                for (int j = 0;j < col;j++)
                                {
                                    if (line[j] == 'B')
                                    {
                                        myMap[i][j].setSpot(true);

                                    }
                                    else
                                    {
                                        myMap[i][j].setSpot(false);

                                    }
                                }
                            }
                int numCycles = 1;

                if (isFilled(row,col,myMap))
                {
                    numCycles = 0;
                }

                while (!isFilled(row,col,myMap))
                {
                    numCycles++;

                    for (int j = 0;j < row;j++)
                    {
                        for (int k = 0;k < col;k++)
                        {
                            if (myMap[j][k].getIsBunny())
                            {       //cout<< j<<" "<<k<<" " <<"true"<<endl;
                                    switch (myMap[j][k].getNextCycle())
                                    {
                                        case Spot::UP :
                                            if (j>0)
                                            myMap[j-1][k].makeBunny();
                                            break;
                                        case Spot::RIGHT :
                                            if (k<col-1)
                                            myMap[j][k + 1].makeBunny();
                                            break;
                                        case Spot::DOWN :
                                            if (j<row-1)
                                            myMap[j+ 1][k].makeBunny();
                                            break;
                                        case Spot::LEFT :
                                            if (k>0)
                                            myMap[j][k - 1].makeBunny();
                                            break;
                                    }
                                    myMap[j][k].setNextCycle();
                                }
                            //cout<< j<<" "<<k<<" " <<"outside"<<endl;
                        }
                    }

                }
                int time = numCycles*minPerCycle;
                outFile<<"It took " <<time <<" minutes for the bunnies to take over the world!\n";
                cout<<"It took " <<time <<" minutes for the bunnies to take over the world!\n";
                for(int i=0; i < col; i++) {
                   delete [] myMap[i];
                }
                delete myMap;

            }
      }

       inFile.close();
       outFile.close();

}
  catch (ifstream::failure e) {
    cout << "Exception opening/reading file";
  }

  return 0;
}


bool isFilled(int row, int col,Spot **myMap)
{
    for (int i = 0;i < row;i++)
    {
        for (int j = 0;j < col;j++)
        {
            if (!myMap[i][j].getIsBunny())
            {
                //cout<<"true ";
                return false;

            }
            //else
            //  cout<<"false ";


        }
        //cout<<endl;

    }
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:02:57+00:00Added an answer on June 11, 2026 at 9:02 pm

    Have to tried passing with pointers?

    static bool isFilled(int row, int col,Spot** yourMap)
    

    I’m unsure if Spot myMap[row][col] will be accepted by the compiler as row and col seem to be set at runtime.

    You may need to allocate memory in the following manner

    Spot** myMap = new Spot* [col];
    for(int i = 0; i < col; ++i)
        myMap[i] = new Spot [row];
    

    Deleting the memory would require you to use a similar for loop but remember to call delete []

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a C++ class with two private variables. A fixed size array,
I've an array of fixed size in C . There I might have any
I have a list of thumbnails. They have fixed size. I would like that
The problem: 1) I have buckets of fixed size, in my case 64. This
I have a div with a fixed size of 100px. scrollWidth and scrollHeight works
I have a CDHTMLDialog running in IE that has a fixed size that I
SOLVED: Per the two responses about having a fixed size, i do have a
I have docbook generated HTML that contains small, fixed size images. All these images
I have two UILabels, one above the other. The top one has fixed size
I have a ListView whose size is fixed to maximum of 10 items View

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.