I'm new in c++. I have an assignment as follows......
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 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...
right now i am working on just printing the .'s so I know that they work.
When I run my code it prints the dots but between each dot is a random symbol.I would appreciate help in fixing this problem and any advice as to how i might continue my assignment. Here is my code so far....
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//array
char frogs[22][52];
int row;
int col;
//enums
enum type {
NONE,
BABIES,
ADULTS
};
//print
for (row = 0; row < 51; row++)
{
for (col = 0; col < 22; col++)
You did not initialize elements of your array. And I do not see the enum with enumerators as you are describing. In your program NONE is 0, BABIES is 1, and ADULTS is 2.
I think that you should declare the enum the following way
The symbols are there because, like vlad said, you never initialized your array. When you declare an array, the computer grabs the memory space for it, but does not clean that space out. This means that until you EXPLICITLY say "fill this array with these values" it has whatever leftover garbage was in that memory space already.
Awesome thanks. I think i can figure out the part with setting random cells, but could the explain how i would change blank cells to babies and babies to adults please?
int tadpoles( char frogs[][22], int wide, int deep ) // a function
{
int row, col, children=0;
for (row = 0; row < deep; row++)
{
for (col = 0; col < wide ; col++)
{
if ( frogs[col][row] == ADULTS) // Check if the location is an adult
{
if(frogs[col-1][row] == NONE && rand()%10 < 2) // If col-1 and row is empty AND
// a random number less than 2
{
frogs[col-1][row] = BABIES; // Change blank to a baby
children++; // I was keeping track of how many tadpoles. Not really needed
}
if(frogs[col+1][row] == NONE && rand()%10 < 2) // Check col+1 and row, etc.
{
frogs[col+1][row] = BABIES;
children++;
} // And check each location next to an adult
Then have a routine that checks if frog[x][y] == babies, and change it to an adult.
Oh yeah. I'm sure there are better ways to check if space is an adult, etc., but I don't know them. So, I'm sticking with what I do know.
int tadpoles( char frogs[][22], int wide, int deep ) // Same function as above.. ( but with changes )
{
int row, col, children=0;
for (row = 0; row < deep; row++)
{
for (col = 0; col < wide ; col++)
{
if ( frogs[col][row] == ADULTS)
{
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
}
}
}
}
}
}
return children; // Leave out if not needing.
}
After changing some cells to BABIES, the array should be printed.
, so the rand()%10 <2 gives a 20% chance only, of changing a cell next to an ADULT to a baby. You could also do rand()%10>7, or even rand()%10==5 (or any number from 0 to 9). Otherwise, the pattern produced would always look circular, since ALL the cells would change every time. Of course, when changing BABIES to ADULT, there isn't a random factor, since your post then says
Then you need to call a function to change all the BABIES to ADULTS
Thanks for posting this problem. It was a lot of fun to make. I have my version to always print in the same screen locations, with different colors for the Frog, Tadpole and land. I then watch the frogs spread across the screen by using a sleep routine to slow it down, for viewing.
Well, according to your post, it says you call a routine to get the row then column. It would look like
1 2 3
int a=Propagate_Cell( 50 ); // Get a random column position
int b=Propagate_Cell( 20 ); // Get a random row position
frogs[a][b]=ADULTS; // frogs[][] is a char array, of [52] by [22]. Assigns a pair of Cane Toads at that location.
The function, of course, is located elsewhere.
1 2 3 4 5
int Propagate_Cell( int x) // First time through, x equals 50, then 20, in second call
{
int fill = 1+rand()%x; // Gets a random number from 1 to x
return fill; // sends the number to calling line
}
do i need to replace them with my grid size of leave as is?
. I believe these are your grid sizes, according to your post. It's 52 by 22, but you use only 1 to 50, and 1 to 20. I think that was to make it easier for programming, since you didn't have to remember arrays start at 0, not 1.
In the top two functions, I am passing the 2D array. to it.
When you declare your array the first time, do this. Now instead of random symbols there will be spaces
char frogs[22][52]={' '};
This isn't accurate.
frogs[0][0] would be a space. Every other element would be '\0' or just 0 if you prefer. When you provide an initializer list for an aggregate such as frogs, and the initializer list doesn't contain expressions to initialize every element, the elments there are no initializer's for are given the default value of type() (char(), in this case,) which is 0 for built-in types.
Yay for the preview button working correctly again!
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;
}
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;
}
}
Because after you set frogx[x][y] to ADULTS you turn around and set it to NONE? I didn't actually follow the logic of your code since you didn't bother to use code tags, and that was the first thing that jumped out at me.