'Game of Life, building the public API - C programming

I'm building the public API for Game of Life and I don't know how to code this part.

This is a picture of "flow chart" we're suppose to follow:

enter image description here

Steps : enter image description here

These are the public APIs we're supposed to fill out;

void gofl_get_world(int n_rows, int n_cols, cell_t world[][n_cols], double percent_alive){} 

this first function should: Get the initial randomly ordered distribution of cells in the world. This is the usage: gofl_get_world(MAX_ROWS, MAX_COLS, world, 0.1)

void gofl_next_state(int n_rows, int n_cols, cell_t world[][n_cols]){}

This function does this: Calculate the next state of the world according to the rules and the actual state. I.e. this will mark all cells in the world as alive or dead.

Here are the functions we're supposed to build the public API from, I've made these myself as well so they are not pre-defined (they are tested and all returned true):

static void get_cells(cell_t arr[], int size, double percent_alive) {
int n = (int) round(percent_alive*size);                          //checking if cell dead or alive with size
for (int i = 0; i < size; i++){
    if (i < n ){                                                   //cell needs to be over certain thresh hold to be alive
        arr[i] = 1;                                                //alive
    }
    else{
        arr[i] = 0;                                               //dead
    }
}


static int get_living_neighbours(int n_rows, int n_cols, const cell_t world[][n_cols], int row, int col) {
int sum = 0;                                                                    
for (int r = row - 1; r <=row + 1; r++){
    for(int c = col-1; c<=col +1; c++){
        if(!(row == r && col == c) && is_valid_location(n_rows, n_cols, r, c)){       
            sum = sum + world[r][c];                                                
        }
    }
}
return sum;


static void array_to_matrix(int n_rows, int n_cols, cell_t matrix[][n_cols], const cell_t arr[], int size) {
for (int i = 0; i < size; i++){                        
    matrix[i/n_rows][i%n_cols] = arr[i];                
}                                                      


static void shuffle_cells(cell_t arr[], int size) {
for(int i = size; i > 1; i--){
    int j = rand()%i;
    int tmp = arr[j];
    arr[j] = arr[i-1];
    arr[i-1] = tmp;

}

Anyone know how I can solve this? I don't know how to perform this action, thanks!

c


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source