Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.     int size,numberOfIteration;
  8.     int count;
  9.     int iteration,row,column;
  10.     int index;
  11.     scanf("%d",&size);
  12.     scanf("%d",&numberOfIteration);
  13.     char tab1[size][size];
  14.     char tab2[size][size];
  15.     /*for(index = 0; index < size; index++)
  16.     {
  17.         scanf("%s",tab1[index]);
  18.     }*/
  19.     for( row = 0; row < size; row++)
  20.     {
  21.         for( column = 0; column < size; column++)
  22.         {
  23.             scanf("%c",tab1[row][column]);
  24.         }
  25.     }
  26.  
  27.     printf("\n");
  28.     for( iteration = 0; iteration < numberOfIteration; iteration++)
  29.     {
  30.         for( row = 0; row < size; row++)
  31.         {
  32.             for( column = 0; column < size; column++)
  33.             {
  34.                 count = 0;
  35.  
  36.                 if(tab1[row-1][column-1] == '*' && row>0 && column>0 )
  37.                 {
  38.                     count++;
  39.                 }
  40.                 if(tab1[row-1][column] == '*'&&row>0)
  41.                 {
  42.                     count++;
  43.                 }
  44.                 if(tab1[row-1][column+1] == '*'&& row>0&&column+1<size)
  45.                 {
  46.                     count++;
  47.                 }
  48.                 if(tab1[row][column-1] == '*'&&column>0)
  49.                 {
  50.                     count++;
  51.                 }
  52.                 if(tab1[row][column+1] == '*'&&column+1<size)
  53.                 {
  54.                     count++;
  55.                 }
  56.                 if(tab1[row+1][column-1] == '*'&&row+1<size)
  57.                 {
  58.                     count++;
  59.                 }
  60.                 if(tab1[row+1][column] == '*'&&row+1<size)
  61.                 {
  62.                     count++;
  63.                 }
  64.                 if(tab1[row+1][column+1] == '*'&&row+1<size&&column+1<size)
  65.                 {
  66.                     count++;
  67.                 }
  68.                 if(tab1[row][column] == '*')
  69.                 {
  70.                     if(count == 2||count == 3)
  71.                         tab2[row][column] = '*';
  72.                     else
  73.                         tab2[row][column] = '.';
  74.                 }
  75.                 else
  76.                 {
  77.                     if(count == 3)
  78.                         tab2[row][column] = '*';
  79.                     else
  80.                         tab2[row][column] = '.';
  81.                 }
  82.             }
  83.  
  84.         }
  85.         int temp = sizeof(char)*(size+1);
  86.         for(index=0; index<size; ++index)
  87.         {
  88.             strncpy(tab1[index],tab2[index],temp);
  89.         }
  90.     }
  91.     for( row = 0; row < size; row++)
  92.     {
  93.         for( column = 0; column < size; column++)
  94.         {
  95.             printf("%c",tab1[row][column]);
  96.         }
  97.         printf("\n");
  98.     }
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement