Advertisement
Guest User

matrix_check

a guest
Jun 16th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct virus {
  4.     int gen;
  5.     int neighbours;
  6.     };
  7.  
  8. void initGenNeighbours(int n, struct virus a[][100])
  9. {
  10.  
  11.     int row, col;
  12.     for(row = 0; row < n; row++)
  13.         for(col = 0; col < n; col++)
  14.             a[row][col].gen = 0;
  15.     for(row = 0; row < n; row++)
  16.         for(col = 0; col < n; col++)
  17.             a[row][col].neighbours = 0;
  18.  
  19. }
  20.  
  21. void printMatrixGen(int n, struct virus a[][100])
  22. {
  23.     int i;
  24.     for(i = 0; i < n; i++)
  25.     {
  26.         int j;
  27.         for(j = 0; j < n; j++)
  28.             printf("%d\t", a[i][j].gen);
  29.         printf("\n");
  30.     }
  31. }
  32. void getNeighbours(int n, struct virus a[][100])
  33. {
  34.     int row, col;
  35.  
  36.     for(row = 0; row < n; row++)
  37.         for(col = 0; col < n; col++)
  38.         {
  39.  
  40.  
  41.             //check neghbours
  42.  
  43.             if((row == 0) && (col == 0)) // upper left
  44.                 if(a[row][col] == '*')
  45.                 {
  46.  
  47.                     if(a[row + 1][col] == '*')
  48.                         a[row][col].neighbours += 1;
  49.                     if(a[row][col + 1] == '*')
  50.                         a[row][col].neighbours += 1;
  51.                 }
  52.             if((row == 0) && (col == n-1))//upper right
  53.                 if(a[row][col] == '*')
  54.                 {
  55.                     if(a[row][col - 1] == '*')
  56.                         a[row][col].neighbours += 1;
  57.                     if(a[row + 1][col] == '*')
  58.                         a[row][col].neighbours += 1;
  59.                 }
  60.             if((row == n-1) && (col == n-1))//lower right
  61.                 if(a[row][col] == '*')
  62.                 {
  63.                     if(a[row - 1][col] == '*')
  64.                         a[row][col].neighbours += 1;
  65.                     if(a[row][col - 1] == '*')
  66.                         a[row][col].neighbours += 1;
  67.                 }
  68.             if((row == n-1) && (col == 0))//lower left
  69.                 if(a[row][col] == '*')
  70.                 {
  71.                     if(a[row - 1][col] == '*')
  72.                         a[row][col].neighbours += 1;
  73.                     if(a[row][col + 1] == '*')
  74.                         a[row][col].neighbours += 1;
  75.                 }
  76.             if((row > 0) && (col > 0)) //normal element that is not in the corner or the sides
  77.                 {
  78.                     if(a[row - 1][col] == '*') //upper neighbour
  79.                         a[row][col].neighbours += 1;
  80.                     if(a[row][col + 1] == '*')//right neighbour
  81.                         a[row][col].neighbours += 1;
  82.                     if(a[row][col - 1] == '*')//left neighbour
  83.                         a[row][col].neighbours += 1;
  84.                     if(a[row + 1][col] == '*')// lower neighbour
  85.                         a[row][col].neighbours += 1;
  86.                 }
  87.             //check if element is on the sides
  88.             if((row > 0) && (col == n - 1))//last column
  89.             {
  90.  
  91.                     if(a[row][col - 1] == '*') //left neighbour
  92.                         a[row][col].neighbours += 1;
  93.                     if(a[row - 1][col] == '*') //upper neighbour
  94.                         a[row][col].neighbours += 1;
  95.                     if(a[row + 1][col] == '*') //bellow neighbour
  96.                         a[row][col].neighbours += 1;
  97.             }
  98.             if((row == n - 1) && (col > 0))//last row
  99.             {
  100.  
  101.                     if(a[row][col - 1] == '*') // left neighbour
  102.                         a[row][col].neighbours += 1;
  103.                     if(a[row][col + 1] == '*') // right neighbour
  104.                         a[row][col].neighbours += 1;
  105.                     if(a[row - 1][col] == '*') //upper neighbour
  106.                         a[row][col].neighbours += 1;
  107.             }
  108.             if((row > 0 && (col == 0))//prima coloana
  109.             {
  110.  
  111.                     if(a[row + 1][col] == '*') //vecin de deasupra
  112.                         a[row][col].neighbours += 1;
  113.                     if(a[row - 1][col] == '*')//vecin de jos
  114.                         a[row][col].neighbours += 1;
  115.                     if(a[row][col + 1] == '*')//vecin dreapta
  116.                         a[row][col].neighbours += 1;
  117.             }
  118.             if((row == 0 && (col > 0))//first column
  119.             {
  120.  
  121.                     if(a[row + 1][col] == '*') // bellow neighbour
  122.                         a[row][col].neighbours += 1;
  123.                     if(a[row][col + 1] == '*')//right neighbour
  124.                         a[row][col].neighbours += 1;
  125.                     if(a[row][col - 1] == '*')//left neighbour
  126.                         a[row][col].neighbours += 1;
  127.             }
  128.         }
  129. }
  130.  
  131. //void getKGenerations
  132. //void establishLife
  133.  
  134. int main(void)
  135. {
  136.     int n;
  137.     n = 4;
  138.     struct virus a[100][100];
  139.     initGen(n,a);
  140.     printf("\n");
  141.     printMatrixGen(n,a);
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement