Problem With initializing function for the game of life

Page 1 of 1

3 Replies - 1145 Views - Last Post: 22 September 2011 - 05:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248082&amp;s=c6f771aaef769b04d91af07d8a949593&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 dimmu1

Reputation: 0
• Posts: 13
• Joined: 10-April 11

Problem With initializing function for the game of life

Posted 21 September 2011 - 04:34 PM

I'm stuck on this function that is related to the game of life. It is supposed to randomly populate the game board with '*'s based on the user input (what percentage they want to be '*'). this is what I have so far, but it crashes when I try to run it. I tried debugging it, and found that there is something wrong in the second if statement in my lifePopulateRandom function. Any help is really appreciated:

```static int nrow, ncol, ntot;
static int nrow_t, ncol_t, ntot_t;
static int alive_in_grid=0;

static char** g0=NULL;
static char** g1=NULL;

#define Current(I, J) g_current[(I) + 1][(J) + 1]
#define Next(I, J)    g_next[(I) + 1][(J) + 1]

void lifePopulateRandom(float percent) {
int i, j, location, num_to_insert;

if (percent < 0.0 || percent > 100.0) {
printf("Invalid percentage: %f.\n", percent);
return;
}
percent /= 100.0;

// Initialize both grids to all blank spaces.
for (i = 0; i < ntot_t; i++) {
g0[i] = ' ';
g1[i] = ' ';
}

alive_in_grid = 0;

num_to_insert = (int) (percent * ntot);

while (alive_in_grid < num_to_insert) {
location = rand() % ntot;
j = location % ncol;
i = location / ncol;

if (Current(i, j) == ' ') {

Current(i, j) = '*';
printf ("\nbye\n");
alive_in_grid++;
}
}

return;
} // lifePopulateRandom()

```

this is how the grid was initialized:

```void lifeInit(int the_nrow, int the_ncol) {
int i;

nrow = the_nrow;
ncol = the_ncol;
ntot = nrow * ncol;

nrow_t = nrow + 2;
ncol_t = ncol + 2;
ntot_t = nrow_t * ncol_t;

// Free memory if previously initialized.
if (g0 != NULL)
free(g0);
if (g1 != NULL)
free(g1);

g0 = (char**)malloc(ntot_t * sizeof(char*));
g1 = (char**)malloc(ntot_t * sizeof(char*));

if (g0 == NULL || g1 == NULL) {
printf("Initialization memory allocation failed.  Terminating...\n");
exit(-1);
}

// Initialize both grids to all blank spaces.
for (i = 0; i < ntot_t; i++) {
g0[i] = ' ';
g1[i] = ' ';
}

g_current = g0;
g_next = g1;

return;
} // lifeInit()

```

thanks.

Is This A Good Question/Topic? 0

Replies To: Problem With initializing function for the game of life

#2 RevTorA

• D.I.C Regular

Reputation: 78
• Posts: 255
• Joined: 22-April 11

Re: Problem With initializing function for the game of life

Posted 21 September 2011 - 06:33 PM

What error messages are you getting when it's crashing, and what "problem" did you find with the second if statement?

#3 dimmu1

Reputation: 0
• Posts: 13
• Joined: 10-April 11

Re: Problem With initializing function for the game of life

Posted 21 September 2011 - 10:17 PM

RevTorA, on 21 September 2011 - 06:33 PM, said:

What error messages are you getting when it's crashing, and what "problem" did you find with the second if statement?

Im not getting any error messeges, it crashed at run time and that little window comes up that says program has stopped working( on windows). the problem is that the conditions for the second if statement of my lifepopularerandom function ( line 35 of the first block of code) is never met, so it never even goes into the if statement. Either there is something wrong with my macros( which i tied to solve by replacing the actual code instead of a macro and didn't work) or my doing something fundamentally wrong here.. either in the declaration or somewhere else.

#4 baavgai

• Dreaming Coder

Reputation: 7442
• Posts: 15,437
• Joined: 16-October 07

Re: Problem With initializing function for the game of life

Posted 22 September 2011 - 05:53 AM

DO NOT USE GLOBAL! Great crap.

What's with that silly g_current[(I) + 1][(J) + 1] That's one of the few globals you don't have defined.

```void lifeInit(int the_nrow, int the_ncol) {
int i;

// setting globals...
nrow = the_nrow;
ncol = the_ncol;
ntot = nrow * ncol;

// Why?!?  What the hell?
// Is this where you're trying to allow for edges?
// don't do this
nrow_t = nrow + 2;
ncol_t = ncol + 2;
ntot_t = nrow_t * ncol_t;

// Free memory if previously initialized.
if (g0 != NULL) { free(g0); }
if (g1 != NULL) { free(g1); }

// g0 = (char**)malloc(ntot_t * sizeof(char*));
// g1 = (char**)malloc(ntot_t * sizeof(char*));
g0 = malloc(ntot_t * sizeof(char*));
g1 = malloc(ntot_t * sizeof(char*));

if (g0 == NULL || g1 == NULL) {
printf("Initialization memory allocation failed.  Terminating...\n");
exit(-1);
}

// Initialize both grids to all blank spaces.
// Fail
for (i = 0; i < ntot_t; i++) {
// g0[i] is of type char *
// this doesn't work
g0[i] = ' ';
g1[i] = ' ';
}

// ouch, more global?
g_current = g0;
g_next = g1;

// why?
return;
}

```

Ok, first, you don't seem to know if you want a 2D or a 1D allocation for your massive global mess. The 1D is actually easier, but then you have to define your own row stops. Using structs would make this easier. If you really want 2D, look up how it's properly allocated, because you haven't done it.

You currently have an array of pointers. Those pointers don't point anywhere. You need to initialize them. Usually, they're initialized to point to columns.