Advertisement
Lisaveta777

Zmeika 2

Nov 22nd, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #define SIZE 8//может быть число до 10
  5. //populate array in quite interesting way, works both with if/else if/else and trenary operator
  6.  
  7. void pr_arr(int s,int a[SIZE][SIZE]);
  8. void snake(int s,int a[SIZE][SIZE]);
  9. int main()
  10. {
  11.     int arr[SIZE][SIZE];
  12.  
  13.     printf("SNAKE is:\n");
  14.     snake(SIZE,arr);
  15.     pr_arr(SIZE,arr);
  16.  
  17.     return 0;
  18. }
  19.  
  20. void pr_arr(int s,int a[SIZE][SIZE])
  21. {
  22.     int i,j;
  23.     for(i=0;i<s;i++)
  24.     {
  25.         for(j=0;j<s;j++)
  26.             printf("%d\t",a[i][j]);
  27.         printf("\n");
  28.     }
  29. }
  30.  
  31. void snake(int s,int a[SIZE][SIZE])
  32. {
  33.  
  34.     int i,j,step_down,step_right,start = 1;
  35.     step_down = step_right = 0;
  36.  
  37.     for(i=0;i<s;i++)
  38.     {
  39.  
  40.         for(j=0;j<s;j++)
  41.         {
  42.            a[i][j]=(j==0)? start:  //assigns value to element
  43.                (j<s-i)?a[i][j-1]+step_right++:
  44.                    a[i][j-1]-1+step_right--;
  45.  
  46.             if(j==0)step_right= i+2;
  47.             /*if(j==0)//replace with ternary operator!
  48.                 a[i][j]= start, step_right =i+2 ;//step_right initialized once for each i, when j==0
  49.             else if(j<s-i)
  50.                 a[i][j] = a[i][j-1]+ step_right++;
  51.             else
  52.                 a[i][j] = a[i][j-1]-1+step_right--;*/
  53.         }
  54.         step_down++;
  55.         start += step_down;
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement