game-of-life

Conway's Game of Life
git clone git://src.adamsgaard.dk/game-of-life # fast
git clone https://src.adamsgaard.dk/game-of-life.git # slow
Log | Files | Refs | README | LICENSE Back to index

utility.c (965B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 int allocate_matrix(int ***M, int nx, int ny)
      5 {
      6     int i;
      7     (*M) = (int**) malloc(sizeof(double*)*nx);
      8     if (*M == NULL)
      9         return 1;
     10 
     11     for (i=0; i<nx; i++) {
     12         (*M)[i] = (int*) malloc(sizeof(double)*ny);
     13         if ((*M)[i] == NULL)
     14             return 1;
     15     }
     16     return 0;
     17 }
     18 
     19 void free_matrix(int ***M, int nx)
     20 {
     21     int i;
     22     for (i=0; i<nx; i++)
     23         free((*M)[i]);
     24     free(*M);
     25 }
     26 
     27 void print_matrix(char* description, int **M, int nx, int ny)
     28 {
     29     int i, j;
     30     printf("%s:\n", description);
     31     for (j=0; j<ny; j++) {
     32         for (i=0; i<nx; i++) {
     33             printf("%d ", M[i][j]);
     34         }
     35         printf("\n");
     36     }
     37 }
     38 
     39 void print_cell_matrix(int **M, int nx, int ny)
     40 {
     41     int i, j;
     42     for (j=0; j<ny; j++) {
     43         for (i=0; i<nx; i++) {
     44             if (M[i][j] == 1)
     45                 printf("X");
     46             else
     47                 printf(".");
     48         }
     49         printf("\n");
     50     }
     51 }