c - how do I writing function for this code -


i'm trying write function or method code. i'm lost @ how come loop print out number born , number died in output below.

here code:

#include <stdio.h>  char grid[10][10];  // moved function here int occ(int x, int y) {     int i, j, count;     char gridcopy[10][10];   // going in future // make function instead of having same code void printgrid() {     int i, j;     (i = 0; < 10; i++) {         (j = 0; j < 10; j++)             printf("%c", grid[i][j]);         printf("\n");     } } 

you don't need gridcopy in occ function. please remember array transposed. when access element (x,y) need use grid[y][x]

below working code function generatenext() generates next state whole grid array. place need girdcopy. results same in example.

#include <stdio.h> #include <stdlib.h>  char grid[10][10]; int  born,died,generation;  // moved function here int occ(int x, int y) {     int  count;     int  xm1,xp1,ym1,yp1;      xm1=x-1;xp1=x+1;ym1=y-1;yp1=y+1;     if (xm1<0) // handle boundary cases - wrap around         xm1=9;     if (ym1<0)         ym1=9;     if (xp1>9)         xp1=0;     if (yp1>9)         yp1=0;      // checking on value of neighboring cells     count = 0;     if (grid[ym1][x] == '*')         count++;     if (grid[ym1][xp1] == '*')         count++;     if (grid[y][xp1] == '*')         count++;     if (grid[yp1][xp1] == '*')         count++;     if (grid[yp1][x] == '*')         count++;     if (grid[yp1][xm1] == '*')         count++;     if (grid[y][xm1] == '*')         count++;     if (grid[ym1][xm1] == '*')         count++;      return count; }  void generatenext() {     int x,y;     char gridcopy[10][10];      born=0;died=0;     generation++;      (y=0; y<10; y++) {         (x=0; x<10; x++) {             gridcopy[y][x]=grid[y][x];         }     }      (y=0; y<10; y++) {         (x=0; x<10; x++) {             if (grid[y][x]=='*' && occ(x,y)<2)                 {gridcopy[y][x]='-';died++;}             if (grid[y][x]=='*' && occ(x,y)>3)                 {gridcopy[y][x]='-';died++;}             if (grid[y][x]=='-' && occ(x,y)==3)                 {gridcopy[y][x]='*';born++;}         }     }      (y=0; y<10; y++) {         (x=0; x<10; x++) {             grid[y][x]=gridcopy[y][x];         }     } }  // going in future // make function instead of having same code void printgrid() {     int x, y;     (y = 0; y < 10; y++) {         (x = 0; x < 10; x++)             printf("%c", grid[y][x]);         printf("\n");     } }   /*  *   */ int main(int argc, char** argv) {      int x, y, answ;      // setting entire grid '-'     (y = 0; y < 10; y++)         (x = 0; x < 10; x++)             grid[y][x] = '-'; // no need variable, use '-' directly      // printing out grid     printf("this original array\n");     printgrid();      // setting initial state     grid[5][4] = '*'; // no need variable, use '*' directly     grid[5][5] = '*';      grid[5][6] = '*';      grid[4][4] = '*'; grid[3][4] = '*';     grid[4][6] = '*'; grid[3][6] = '*';      // printing out grid     printf("this new array\n");     printgrid();      //printf("the number of neighbors is: %d\n", occ(3, 3));      generation=0;     answ=1;      while(answ)     {         generatenext();         printf("\n\ngeneration number %d",generation);         printf("\nborn=%d,died=%d\n",born,died);         printgrid();         printf("\nprint next generation-1,exit-0:");         scanf("%d",&answ);     }      return (exit_success); } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -