Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you please implement the following function to run the Conways Game of Lif

ID: 3677254 • Letter: C

Question

Could you please implement the following function to run the Conways Game of Life *_driver.c

BinaryMatrix*FirstGeneration(intnum_rows,intnum_cols,List* seed_cells);

Creates a BinaryMatrix representing the first generation on a num_rowsby num_colsgrid. The first generation is described by the coordinates in seed_cells.

BinaryMatrix*NextGeneration(BinaryMatrix*generation);

Creates a BinaryMatrix representing the next generation after *generation.

int Live(BinaryMatrix*generation,Coordinatecoord);

Returns true if coorddescribes a live cell of *generation.

intLivingNeighbors(BinaryMatrix*M,List*neighbors)
Returns the number of cells described by a coordinatein neighbors.

BinaryMatrix, Coordinate and List has been declared.

typedef struct {
int num_rows;
int num_cols;
int** data;
} BinaryMatrix;

struct ListNode;

typedef struct {
struct ListNode* head;
int size;
} List;

typedef struct ListNode {
Coordinate data;
struct ListNode* next;
} ListNode;

typedef struct {
int x, y;
} Coordinate;

#include <stdio.h>

#include <stdlib.h>
#include <time.h>

#include "conways_game_of_life.h"
#include "coordinate.h"

int main(int argc, char* argv[]) {
FILE* fp;
List* seed;
BinaryMatrix* current_generation, *next_generation;
int rows, cols, generations, i;
struct timespec sleep_time;
sleep_time.tv_sec = 0;
sleep_time.tv_nsec = 500000000L;
if (argc != 5) {
printf("Usage: %s <filename> <rows> <columns> <generations> ", argv[0]);
exit(EXIT_FAILURE);
}
/* Get first generation cells */
fp = fopen(argv[1], "r");
seed = ReadList(fp);
fclose(fp);
/* Get game parameters */
sscanf(argv[2], "%d", &rows);
sscanf(argv[3], "%d", &cols);
sscanf(argv[4], "%d", &generations);
/* Run game */
current_generation = FirstGeneration(rows, cols, seed);
PrintGeneration(current_generation, stdout);
for (i = 1; i < generations; i++) {
nanosleep(&sleep_time, NULL);
next_generation = NextGeneration(current_generation);
DeleteBinaryMatrix(current_generation);
current_generation = next_generation;
printf("");
printf("[%d;%dH", 0, 0);
PrintGeneration(current_generation, stdout);
}
DeleteBinaryMatrix(current_generation);
DeleteList(seed);
return 0;
}

Explanation / Answer

BinaryMatrix*FirstGeneration(intnum_rows,intnum_cols,List* seed_cells);
BinaryMatrix*NextGeneration(BinaryMatrix*generation);
int Live(BinaryMatrix*generation,Coordinatecoord);
typedef struct {
int num_rows;
int num_cols;
int** data;
} BinaryMatrix;
struct ListNode;
typedef struct {
struct ListNode* head;
int size;
} List;
typedef struct ListNode {
Coordinate data;
struct ListNode* next;
} ListNode;
typedef struct {
int x, y;
} Coordinate;

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "conways_game_of_life.h"
#include "coordinate.h"
int main(int argc, char* argv[]) {
FILE* fp;
List* seed;
BinaryMatrix* current_generation, *next_generation;
int rows, cols, generations, i;
struct timespec sleep_time;
sleep_time.tv_sec = 0;
sleep_time.tv_nsec = 500000000L;
if (argc != 5) {
printf("Usage: %s <filename> <rows> <columns> <generations> ", argv[0]);
exit(EXIT_FAILURE);
}
/* Get first generation cells */
fp = fopen(argv[1], "r");
seed = ReadList(fp);
fclose(fp);
/* Get game parameters */
sscanf(argv[2], "%d", &rows);
sscanf(argv[3], "%d", &cols);
sscanf(argv[4], "%d", &generations);
/* Run game */
current_generation = FirstGeneration(rows, cols, seed);
PrintGeneration(current_generation, stdout);
for (i = 1; i < generations; i++) {
nanosleep(&sleep_time, NULL);
next_generation = NextGeneration(current_generation);
DeleteBinaryMatrix(current_generation);
current_generation = next_generation;
printf("");
printf("[%d;%dH", 0, 0);
PrintGeneration(current_generation, stdout);
}
DeleteBinaryMatrix(current_generation);
DeleteList(seed);
return 0;
}

//implementing function
BinaryMatrix*FirstGeneration(int num_rows,int num_cols,List* seed_cells){
//creating structure
BinaryMatrix* current_generationTemp;
current_generationTemp.num_rows = num_rows;
current_generationTemp.num_cols = num_cols;
current_generationTemp.data = seed_cells;
return current_generation;   
}

//creating next generation
BinaryMatrix*NextGeneration(BinaryMatrix*generation){
int rows = generation.num_rows;
int cols = generation.num_cols;

//creating structure
BinaryMatrix* next_generationTemp;
next_generationTemp.num_rows = rows;
next_generationTemp.num_cols = cols;
next_generationTemp.data = generation.data;
return next_generationTemp;
}

//checking cell is alive or not
int Live(BinaryMatrix*generation,Coordinatecoord){
if(generation.num_rows == Coordinatecoord.x && generation.num_cols == Coordinatecoord.y){
return 1; //1 means true
}
return 0; //0 means false
}

//getting neighbour ive count
intLivingNeighbors(BinaryMatrix*M,List*neighbors){
int count = 0;
if(M[neighbors.x][neighbors.y-1] == 1){
count++;
}
if(M[neighbors.x][neighbors.y+1] == 1){
count++;
}
if(M[neighbors.x-1][neighbors.y] == 1){
count++;
}
if(M[neighbors.x][neighbors.y] == 1){
count++;
}
if(M[neighbors.x+1][neighbors.y] == 1){
count++;
}
if(M[neighbors.x-1][neighbors.y-1] == 1){
count++;
}
if(M[neighbors.x][neighbors.y+2] == 1){
count++;
}
return count;
}