Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct virus {
- int gen;
- int neighbours;
- };
- void initGenNeighbours(int n, struct virus a[][100])
- {
- int row, col;
- for(row = 0; row < n; row++)
- for(col = 0; col < n; col++)
- a[row][col].gen = 0;
- for(row = 0; row < n; row++)
- for(col = 0; col < n; col++)
- a[row][col].neighbours = 0;
- }
- void printMatrixGen(int n, struct virus a[][100])
- {
- int i;
- for(i = 0; i < n; i++)
- {
- int j;
- for(j = 0; j < n; j++)
- printf("%d\t", a[i][j].gen);
- printf("\n");
- }
- }
- void getNeighbours(int n, struct virus a[][100])
- {
- int row, col;
- for(row = 0; row < n; row++)
- for(col = 0; col < n; col++)
- {
- //check neghbours
- if((row == 0) && (col == 0)) // upper left
- if(a[row][col] == '*')
- {
- if(a[row + 1][col] == '*')
- a[row][col].neighbours += 1;
- if(a[row][col + 1] == '*')
- a[row][col].neighbours += 1;
- }
- if((row == 0) && (col == n-1))//upper right
- if(a[row][col] == '*')
- {
- if(a[row][col - 1] == '*')
- a[row][col].neighbours += 1;
- if(a[row + 1][col] == '*')
- a[row][col].neighbours += 1;
- }
- if((row == n-1) && (col == n-1))//lower right
- if(a[row][col] == '*')
- {
- if(a[row - 1][col] == '*')
- a[row][col].neighbours += 1;
- if(a[row][col - 1] == '*')
- a[row][col].neighbours += 1;
- }
- if((row == n-1) && (col == 0))//lower left
- if(a[row][col] == '*')
- {
- if(a[row - 1][col] == '*')
- a[row][col].neighbours += 1;
- if(a[row][col + 1] == '*')
- a[row][col].neighbours += 1;
- }
- if((row > 0) && (col > 0)) //normal element that is not in the corner or the sides
- {
- if(a[row - 1][col] == '*') //upper neighbour
- a[row][col].neighbours += 1;
- if(a[row][col + 1] == '*')//right neighbour
- a[row][col].neighbours += 1;
- if(a[row][col - 1] == '*')//left neighbour
- a[row][col].neighbours += 1;
- if(a[row + 1][col] == '*')// lower neighbour
- a[row][col].neighbours += 1;
- }
- //check if element is on the sides
- if((row > 0) && (col == n - 1))//last column
- {
- if(a[row][col - 1] == '*') //left neighbour
- a[row][col].neighbours += 1;
- if(a[row - 1][col] == '*') //upper neighbour
- a[row][col].neighbours += 1;
- if(a[row + 1][col] == '*') //bellow neighbour
- a[row][col].neighbours += 1;
- }
- if((row == n - 1) && (col > 0))//last row
- {
- if(a[row][col - 1] == '*') // left neighbour
- a[row][col].neighbours += 1;
- if(a[row][col + 1] == '*') // right neighbour
- a[row][col].neighbours += 1;
- if(a[row - 1][col] == '*') //upper neighbour
- a[row][col].neighbours += 1;
- }
- if((row > 0 && (col == 0))//prima coloana
- {
- if(a[row + 1][col] == '*') //vecin de deasupra
- a[row][col].neighbours += 1;
- if(a[row - 1][col] == '*')//vecin de jos
- a[row][col].neighbours += 1;
- if(a[row][col + 1] == '*')//vecin dreapta
- a[row][col].neighbours += 1;
- }
- if((row == 0 && (col > 0))//first column
- {
- if(a[row + 1][col] == '*') // bellow neighbour
- a[row][col].neighbours += 1;
- if(a[row][col + 1] == '*')//right neighbour
- a[row][col].neighbours += 1;
- if(a[row][col - 1] == '*')//left neighbour
- a[row][col].neighbours += 1;
- }
- }
- }
- //void getKGenerations
- //void establishLife
- int main(void)
- {
- int n;
- n = 4;
- struct virus a[100][100];
- initGen(n,a);
- printf("\n");
- printMatrixGen(n,a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement