Cane Toads Array Program

I have been racking my brain all day and night trying to figure this out. A skeleton program was provided by the teacher.

#include <iostream>
#include <cstdlib>

using namespace std;

const int ROWS = 20;
const int COLS = 50;

void initialize ( char toads[][COLS+2] )
{
int row, col;
//
// Fill all 22 rows and 52 cols with '.'
//
//
// Select a random row number using rand() mod the number of rows plus one
//

//
// Select a random column number using rand() mod the number of columns
// plus one
//

//
// Place an 'A' (Adult) in location (row,col) of the toads array
//

}

bool full ( char toads[][COLS+2] )
{
//
// Return true if all 20x50 interior cells are Adults
//
return true;
}

void propagate ( char toads[][COLS+2] )
{
//
// For all 20 rows and 50 columns of interior cells
// Convert any neighboring '.' cells to 'b' (baby)
//
}

void maturate ( char toads[][COLS+2] )
{
//
// For all 20 rows and 50 columns of interior cells
// Convert any 'b' cells (baby) to 'A' (Adult)
//
}

void print ( char toads[][COLS+2] )
{
//
// Print all 20 rows and 50 columns of the toads array
//

//
// Use getline to force the user to press return
//
}

int main()
{
char toads[ROWS+2][COLS+2];

//
// Seed the random number generator with the number of seconds since the
// beginning of the UNIX epoch (Jan. 1, 1970).
//
srand(time(NULL));

//
// Fill the toads array with dots and one randomly placed Adult toad
//
initialize ( toads );

while ( !full(toads) ) {
propagate(toads);
print(toads);
maturate(toads);
}

return 0;
}

Now I have tried to figure this out. I will post my code on a separate reply because its so long. But here is the instructions from the teacher also:

Cane toads are an invasive species in Australia which are causing environmental havoc. The problem is that cane toads are poisonous and nearly all the Australian predators are susceptible to cane toad poisoning. Wherever the toads have reached they have caused a tremendous loss of native species like snakes which attempt to eat toads which look tasty to a snake. About 90% of the at risk populations are killed.

The goal of this assignment is to use a 2D array to represent a region of the earth which will start with one cell selected at random which will have 1 pair of cane toads. The array can be an array of enums where the enums are defined as NONE, BABIES, or ADULTS. The initial configuration will be all cells of the 2D array having value NONE. Then you would call the random function twice to get random integers. Use a random number mod the number of columns (plus one) to get a random column number and do the same to get a random row number. Set this cell's value to ADULTS. This will represent having at least 1 pair of adults who can propagate to neighboring cells.

Your program should then print the array and use getline to read a line of input. The purpose of the getline call is to allow the user to look at the array. I suggest having 20 rows and 50 columns in the array and using "." to represent NONE, "A" to represent ADULTS and "b" to represent BABIES.

After getting the input line the program should propagate to all cells with a NONE value a BABIES value if there is a neighboring cell with ADULTS. There will be 8 neighbors for all cells in the interior of the array. A significant programming simplification can be made by having the actual array be 2 rows and 2 columns larger - that means using a 22x52 array and then if your process rows 1-20 and columns 1-50 the processed cells will all be interior cells.

After changing some cells to BABIES, the array should be printed. Then you need to call a function to change all the BABIES to ADULTS and use getline again to wait until the user is ready to see the next step.

Your program should exit when all the interior cells (20*50 = 1000) are all ADULTS. At that point Australia will be toast.

At some point in execution it might be possible to view a region like this - or maybe it will look like some other common figure...

..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................bbb.............................
.................bbAbb............................
................bbAAAbb...........................
................bAAAAAb...........................
................bbAAAbb...........................
.................bbAbb............................
..................bbb.............................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................

A skeleton program is available in toads.cpp in the class Moodle source collection.


Ok bear with me, it is extremely long and probably completely wrong.

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
#include <cstring>

using namespace std;

const int ROWS = 20;
const int COLS = 50;

void initialize ( char toads[][COLS+2] )
{
int row, col;
int i, j;
enum frogs
{
NONE = '.',
BABIES = 'b',
ADULTS = 'A'
};
//
// Fill all 22 rows and 52 cols with '.'
//
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS+2; j++)
{
toads[i][j] = NONE;
}
//
// Select a random row number using rand() mod the number of rows plus one
//
row = rand()%ROWS+1;
//
// Select a random column number using rand() mod the number of columns
// plus one
//
col = rand()%COLS+1;
//
// Place an 'A' (Adult) in location (row,col) of the toads array
//
toads[row][col]=ADULTS;

}
}

bool full ( char toads[][COLS+2] )
{
// Return true if all 20x50 interior cells are Adults
//
int i, j;
enum frogs
{
NONE = '.',
BABIES = 'b',
ADULTS = 'A'
};
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if (toads[i][j] == ADULTS)
return true;
}
}
}

void propagate ( char toads[][COLS+2] )
{
int i, j;
enum frogs
{
NONE = '.',
BABIES = 'b',
ADULTS = 'A'
};

for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
bool adultPresent = false;
for (int d; d < ROWS; d++)
{
for (int s; s < COLS; s++)
{
if (toads[i][j]==ADULTS)
{
adultPresent = true;
}
}
}
if (adultPresent)
{
for (int r=-1; r<=1; r++)
{
for (int c =-1; c<=1; c++)
{
if (toads[i+r][j+c] == NONE)
{
toads[i+r][j+c] == BABIES;
}
}
}
}
}
}

//
// For all 20 rows and 50 columns of interior cells
// Convert any neighboring '.' cells to 'b' (baby)
//
}

void maturate ( char toads[][COLS+2] )
{
//
// For all 20 rows and 50 columns of interior cells
// Convert any 'b' cells (baby) to 'A' (Adult)
//
int i, j;
enum frogs
{
NONE = '.',
BABIES = 'b',
ADULTS = 'A'
};

for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
bool babiesFound = false;
for (int d; d < ROWS; d++)
{
for (int s; s < COLS; s++)
{
if (toads[i][j] == BABIES)
{
babiesFound = true;
}
}
}
if (babiesFound)
{
toads[i][j] = ADULTS;
}
}
}

}

void print ( char toads[][COLS+2] )
{
//
//
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
cout << toads[i][j] << " ";
}
cout << endl;
}
}
// Print all 20 rows and 50 columns of the toads array
//

//
// Use getline to force the user to press return
//

int main()
{
char toads[ROWS+2][COLS+2];

//
// Seed the random number generator with the number of seconds since the
// beginning of the UNIX epoch (Jan. 1, 1970).
//
srand(time(NULL));
rand();
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
cout << toads[i][j] << " ";
}
cout << endl;
}
//
// Fill the toads array with dots and one randomly placed Adult toad
//
initialize ( toads );

while ( !full(toads) ) {
propagate(toads);
print(toads);
maturate(toads);
}

return 0;
}
Topic archived. No new replies allowed.