Implementation for Conway\'s Game of Life. I have a function BinaryMatrix* First
ID: 3677252 • Letter: I
Question
Implementation for Conway's Game of Life.
I have a function
BinaryMatrix* FirstGeneration(int num_rows, int num_cols, List* seed_cells);
/*struct ListNode; typedef struct { struct ListNode* head; int size; } List; typedef struct ListNode { Coordinate data; struct ListNode* next; } ListNode;*/
/*typedef struct { int num_rows; int num_cols; int** data; } BinaryMatrix;*/
Could you help me implement this function to Creates a BinaryMatrix representing the first generation on a num_rowsby num_colsgrid. The first generation is described by the coordinates in seed_cells.
Thanks.
Explanation / Answer
Solution: See the code below:
------------------------------------------------
#include <stdlib.h>
BinaryMatrix* FirstGeneration(int num_rows, int num_cols, List* seed_cells);
struct ListNode;
typedef struct {
struct ListNode* head;
int size;
} List;
typedef struct ListNode {
Coordinate data;
struct ListNode* next;
} ListNode;
typedef struct {
int num_rows;
int num_cols;
int** data;
} BinaryMatrix;
BinaryMatrix* FirstGeneration(int num_rows, int num_cols, List* seed_cells)
{
BinaryMatrix* bin_mat=(BinaryMatrix*)malloc(sizeof(BinaryMatrix));
int data[num_rows][num_cols];
int i=0,j=0;
int x,y; //for coordinate information
ListNode* node;
//Initialize data to zero
for(i=0;i,num_rows,i++)
{
for(j=0;j<num_cols,j++)
{
data[i][j]=0;
}
}
//traverse the list and fill up data
node=seed_cells->head;
while(node!=NULL)
{
//assuming Cordinate has x and y parts
x=node->data.x;
y=node->data.y;
data[x][y]=1;
node=node->next;
}
//Set BinaryMatrix
bin_mat->num_rows=num_rows;
bin_mat->num_cols=num_cols;
bin_mat->data=data;
return bin_mat;
}
--------------------------------------------------