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()

and this is how i initialized the grid:

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.

Before I spent anymore time on this question, could you please state what line number you are talking about.

Before I spent anymore time on this question, could you please state what line number you are talking about.

line 35 of the first block of code.. as i said earlier, the second if statement of my populate random function. please read what I said in the original post first.

line 35 of the first block of code.. as i said earlier, the second if statement of my populate random function. please read what I said in the original post first.

I read it... You said

I tried debugging it, and found that there is something wrong in the second if statement in my lifePopulateRandom function.

Then you did something wrong somewhere. Maybe a more accurate description of what something means is required to understand your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.