#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <time.h> #include…
- User
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <time.h> #include <unistd.h> #include <string.h>
int c = 1; int num_animals = 3;
typedef enum { BEAR, BIRD, PANDA } AnimalType;
typedef enum { ALIVE, DEAD } AnimalStatus;
typedef struct { int x; int y; } Location;
typedef enum { FEEDING, NESTING, WINTERING } SiteType;
typedef struct { /** animal can be DEAD or ALIVE*/ AnimalStatus status; /** animal type, bear, bird, panda*/ AnimalType type; /** its location in 2D site grid*/ Location location; } Animal;
/example usage/ Animal bird, bear, panda;
/** type of Hunter*/ typedef struct { /** points indicate the number of animals, a hunter killed*/ int points; /** its location in the site grid*/ Location location; } Hunter;
/** type of a site (a cell in the grid)/ typedef struct { /** array of pointers to the hunters located at this site/ Hunter hunters; / the number of hunters at this site*/ int nhunters; /** array of pointers to the animals located at this site*/ Animal animals; / the number of animals at this site*/ int nanimals; /** the type of site*/ SiteType type; } Site;
/** 2D site grid*/ typedef struct { /** number of rows, length at the x-coordinate*/ int xlength; /** number of columns, length at the y-coordinate*/ int ylength; /** the 2d site array*/ Site **sites; } Grid;
/* initial grid, empty*/ Grid grid = { 0, 0, NULL };
/**
- @brief initialize grid with random site types
- @param xlength
- @param ylength
- @return Grid */ Grid initgrid (int xlength, int ylength) { grid.xlength = xlength; grid.ylength = ylength;
grid.sites = (Site **) malloc (sizeof (Site *) * xlength); for (int i = 0; i < xlength; i++) { grid.sites[i] = (Site *) malloc (sizeof (Site) * ylength); for (int j = 0; j < ylength; j++) { grid.sites[i][j].animals = NULL; grid.sites[i][j].hunters = NULL; grid.sites[i][j].nhunters = 0; grid.sites[i][j].nanimals = 0; double r = rand () / (double) RAND_MAX; SiteType st; if (r < 0.33) st = WINTERING; else if (r < 0.66) st = FEEDING; else st = NESTING; grid.sites[i][j].type = st; } }
return grid; }
/**
- @brief
*/ void deletegrid () { for (int i = 0; i < grid.xlength; i++) { free (grid.sites[i]); }
free (grid.sites);
grid.sites = NULL; grid.xlength = -1; grid.ylength = -1; }
/**
-
@brief prints the number animals and hunters in each site
-
of a given grid
-
@param grid */ void printgrid () { for (int i = 0; i < grid.xlength; i++) { for (int j = 0; j < grid.ylength; j++) { Site do not forget to initialize */ for (int a = 0; a < site->nanimals; a++) { Animal *animal = site->animals[a]; count[animal->type]++; }
- Assistant