Having trouble with array program

Here is the assignment the teacher gave us:


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.


And here is what I have so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
char frogs[22][52];
int row;
int col;
int wide= 20;
int deep =50;

enum type {
NONE = '.',
BABIES = 'b',
ADULTS = 'A'
};
int children=0;
int adult=0;

int x, y = 0;
x = rand() % wide;
y = rand() % deep;
frogs[x][y] = ADULTS;



for(int i=0; i < wide +2; i++){
for(int j=0; j < deep + 2; j++){
frogs[i][j] = NONE;
} 
}

for (row = 1; row < deep; row++)
{
for (col = 1; col < wide ; col++)
{

bool foundAdult = false;
for(int i=1; i<deep; i++){
for(int j=1; j<wide; j++){
if(frogs[i][j] == ADULTS)
foundAdult = true; 
} 
}

if (foundAdult)
{
for(int r=-1;r<=1;r++)
{
for(int c=-1;c<=1;c++)
{ 
if(frogs[col+c][row+r] == NONE && rand()%10 < 2) // Checks all 9 spaces for
// '.' character. If found
{
frogs[col+c][row+r] = BABIES; // Put a tadpole at that location
children++; // Increase by one
}
}
}
}
}
}

//mature
for(int r=-1;r<=1;r++)
{
for(int c=-1;c<=1;c++)
{ 
if(frogs[col+c][row+r] == BABIES && rand()%10 < 2) // Checks all 9 spaces for
// 'b' character. If found
{
frogs[col+c][row+r] = ADULTS; // Put adult at that location
adult++; // Increase by one
}
}
}


//print 
for(int y=1; y < wide; y++){
for(int x = 1; x < deep; x++){
cout << frogs[y][x];
} 
cout << endl; 
}




system ("pause");
return 0;
}


Honestly, im lost, all it does at this point is print b's. I am really new at programming and I need a push in the right direction or just some help. Thanks in advance.
Last edited on
I'm going to focus on the start of your code, with your print code. I think you may be able to complete this with these few simple changes. This is currently your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

int main(){
	char frogs[22][52];
	int row;
	int col;
	int wide= 20;
	int deep =50;

	enum type {
		NONE = '.',
		BABIES = 'b',
		ADULTS = 'A'
	};
	int children=0;
	int adult=0;

	int x, y = 0;
	x = rand() % wide;
	y = rand() % deep;
	frogs[x][y] = ADULTS;

	//print 
	for(int y=1; y < wide; y++){
		for(int x = 1; x < deep; x++){
			cout << frogs[y][x];
		} 
		cout << endl; 
	}
	system ("pause");
	return 0;
}


If this code is executed you will get random data. This is because frogs is not initialised and contains random data. I will not get to this until later because there are some other problems that should be taken care of first.

First, the enum does not need to be in main(). you can simply take it out, putting it above main(). You also do not need to set the enum variables, although you can if you want too. e.g
1
2
3
4
5
6
7
8
enum type {
	NONE,
	BABIES,
	ADULTS
};

int main()
{

This makes it a little easier to work with in my opinion. If you do this you will need to change how you print the array.

Next, you should make frogs into a type instead of char. e.g type frogs[22][52];

Now I will explain how to initialise frogs. There are two ways, the first initialises when frogs is created:
type frogs[22][52] = {NONE};
The second, you can add after you have created frogs. It requires looping through and setting each value to NONE. I wont go through the code for this.

The program should now print: (or something similar)
.................................................
.................................................
.......................................A.........
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................
.................................................


If you use the same print function, it may print:

0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000020000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000


Last edited on
Okay I have this now,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

enum type {
NONE = '.',
BABIES = 'b',
ADULTS = 'A'
};

int main()
{
type frogs[22][52] = {NONE};
int row;
int col;
int wide= 20;
int deep =50;


int children=0;
int adults=5;

int x, y = 0;
x = rand() % wide;
y = rand() % deep;
frogs[x][y] = ADULTS;



for(int i=0; i < wide +2; i++){
for(int j=0; j < deep + 2; j++){

} 
}

for (row = 1; row < deep; row++)
{
for (col = 1; col < wide ; col++)
{

bool foundAdult = false;
for(int i=1; i<deep; i++){
for(int j=1; j<wide; j++){
if(frogs[i][j] == ADULTS)
foundAdult = true; 
} 
}

if (foundAdult)
{
for(int r=-1;r<=1;r++)
{
for(int c=-1;c<=1;c++)
{ 
if(frogs[col+c][row+r] == NONE && rand()%10 < 2) // Checks all 9 spaces for
// '.' character. If found
{
frogs[col+c][row+r] = BABIES; // Put a tadpole at that location
children++; // Increase by one
}
}
}
}
}
}

//mature
for(int r=-1;r<=1;r++)
{
for(int c=-1;c<=1;c++)
{ 
if(frogs[col+c][row+r] == BABIES && rand()%10 < 2) // Checks all 9 spaces for
// 'b' character. If found
{
frogs[col+c][row+r] = ADULTS; // Put adult at that location
adults++; // Increase by one
}
}
}


//print 
for(int y=1; y < wide; y++){
for(int x = 1; x < deep; x++){
cout << frogs[y][x];
} 
cout << endl; 
}




system ("pause");
return 0;
}


It now just prints number because I could not figure out the loops you wanted me to do. Like I said im very new at this :\
Last edited on
I have a similar problem. Mine reads

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
 #include <iomanip>
 #include <cstdlib>
 using namespace std;

 int main()
 {
 char frogs[22][52];
 int row;
 int col;
 int wide= 20;
 int deep =50;
 int adult = 0;
 enum type {
 NONE = '.',
 BABIES = 'b',
 ADULTS = 'A'
 };
 int children=0;

 srand(time(NULL));



 for(int i=0; i < wide +2; i++){
 for(int j=0; j < deep + 2; j++){
 frogs[i][j] = NONE;
 } 
 }
 //random adult 
 int x, y = 0;
 x = rand() % wide;
 y = rand() % deep;
 frogs[x][y] = ADULTS;

 for (row = 1; row < deep; row++)
 {
 for (col = 1; col < wide ; col++)
 {

 bool foundAdult = false;
 for(int i=1; i<deep; i++){
 for(int j=1; j<wide; j++){
 if(frogs[i][j] == ADULTS)
 foundAdult = true; 
 } 
 }
 //baby
 if (foundAdult)
 {
 for(int r=-1;r<=1;r++)
 {
 for(int c=-1;c<=1;c++)
 { 
 if(frogs[col+c][row+r] == NONE) // Checks all 9 spaces for
 // '.' character. If found
 {
 frogs[col+c][row+r] = BABIES; // Put a tadpole at that location
 children++; // Increase by one
 }
 }
 }
 }
 }
 }

 //mature
 for(int r=-1;r<=1;r++)
 {
 for(int c=-1;c<=1;c++)
 { 
 if(frogs[col+c][row+r] == BABIES) // Checks all 9 spaces for
 // 'b' character. If found
 {
 frogs[col+c][row+r] = ADULTS; // Put adult at that location
 adult++; // Increase by one
 }
 }
 }



 //print 
 for(int y=1; y < wide; y++){
 for(int x = 1; x < deep; x++){
 cout << frogs[y][x];
 } 
 cout << endl; 
 }




 system ("pause");
 return 0;

 } 


I get a TON of babies and no adults, except for the middle one. It's a weird problem.
Honestly, im lost at this point.
I see you have put some serious effort into this task, so I dont mind posting this for you.

I don't know if it compiles or even runs, check it out and see what you think.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
void printRegion();
bool allAdults();
bool hasAdultNeighbour(int row, int col);

// 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.

const int ROWS = 20;
const int COLUMNS = 50;
enum population { NONE='.', ADULTS='A', BABIES='b'};
population region[ROWS][COLUMNS];




int main()
{
	// The initial configuration will be all cells of the 2D array having value NONE.
	
	for (int row=0; row<ROWS; ++row)
	{
	   for (int col=0; col<COLUMNS; ++col)
	   {
	      region[row][col] = NONE;
	   }
	}

	// 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.

	region[rand() % ROWS][rand() % COLUMNS] = ADULTS;

	// Your program should then print the array and use getline 
        // (i used cin.ignore) to read a line of input.
	
	 printRegion();

 	do 
	{
		cout << "Press ENTER to see the next generation.";
		cin.ignore();

		// 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

		for (int row=0; row<ROWS; ++row)
		{
		   for (int col=0; col<COLUMNS; ++col)
		   {
		      if (NONE == region[row][col])
		      {
		         if (hasAdultNeighbour(row,col))
		         {
		            region[row][col] = BABIES;
		         }
		      }
		   }
		}

		// After changing some cells to BABIES, the array should be printed.
        
                printRegion();

                // Then you need to call a function to change all the BABIES to ADULTS

		for (int row=0; row<ROWS; ++row)
		{
		   for (int col=0; col<COLUMNS; ++col)
		   {
		      if (BABIES == region[row][col])
		      {
		         region[row][col] = ADULTS;		         
		      }
		   }
		}

            //Your program should exit when all the cells are all ADULTS

	} while (!allAdults())
}


void printRegion()
{
	for (int row=0; row<ROWS; ++row)
	{
	   for (int col=0; col<COLUMNS; ++col)
	   {
	      cout << region[row][col];
	   }
	   cout << endl;
	}
}

bool allAdults()
{
	for (int row=0; row<ROWS; ++row)
	{
	   for (int col=0; col<COLUMNS; ++col)
	   {
	      if (ADULTS != region[row][col])
	      	return false;
	   }
	}

	return true;
}


bool hasAdultNeighbour(int row, int col)
{
	for (int r=row-1; r<=row+1; ++r)
	{
		for (int c=col-1; c<=col+1; ++c)
		{
			if (c>=0 && c<COLUMNS && r>=0 && r<ROWS && region[r][c]==ADULTS)
				return true;
		}
	}
	return false;
}
Last edited on
Topic archived. No new replies allowed.