Array advice would be appreciated

Pages: 12
I could see how that would be a problem. what do you think i should do to fix that?
Cut and paste the code so that you set it to ADULTS after the for loops...
Im sorry for not commenting the lines. The reason i have the adult code there is so that it puts a random adult in the array which is needed for the loops to produce any results
but sorry for doubting you it worked only now it produces an array with 1 A and all the dots
ok so i commented the code and moved the random adult code into the beginning of the loop. I really thought that would work but now i just get an array with an A and dots. I still need to do the getline statement and a feel that may finally fix it. Im not sure how to do that for the array though. here is the revised code

#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 kind of feel like you need to take everything after frogs[x][y] = ADULT and discard it.

Your next move is to print the array, not get input.

In this code there are ~ 50 lines of code (after frogx[x][y]=ADULT) with nested loops inside nested loops with some very questionable logic that don't actually do anything useful before you ever actually bother to print the array.

Why are you beginning your array indexing with 1 and why do you actually have more elements than you're using?

Last edited on
If this is for Seyfarth, then there is a skeleton structure to this program.
I just can't figure out where to go from here.

#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;
}
alright everything is working great. Thanks everyone for the help i appreciate it. sorry if i was such a bother again i'm brand new
Topic archived. No new replies allowed.
Pages: 12