SHARE
TWEET

Untitled

a guest Oct 16th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void print(int n, int **grid)
  5. {
  6.     int i, j;
  7.     for(i=0; i<n; i++)
  8.     {
  9.         for(j=0; j<n; j++)
  10.         {
  11.             printf("%3d ", grid[i][j]);
  12.         }
  13.         printf("\n");
  14.     }
  15. }
  16.  
  17. void fillin(int n, int ***grid){
  18.     int val = 1, loop = 0, z, bound = n, i = 0, j = 0;
  19.     *grid = malloc(n*sizeof(int*));
  20.     for(i = 0; i < n; i++)
  21.     {
  22.         *(*grid+i)=malloc(n*sizeof(int));
  23.  
  24.     }
  25.  
  26.     for(z = 0; z < n/2; z++)
  27.     {
  28.         for(i = loop; i < bound-loop; i++)
  29.         {
  30.             (*grid)[i][j] = val;
  31.             val++;
  32.         }
  33.         i--;
  34.         val--;
  35.         for(j = loop; j < bound-loop; j++)
  36.         {
  37.             (*grid)[i][j] = val;
  38.             val++;
  39.         }
  40.         j--;
  41.         val--;
  42.         for(; i > 0+loop; i--)
  43.         {
  44.             (*grid)[i][j] = val;
  45.             val++;
  46.         }
  47.         for(; j > 0+loop; j--)
  48.         {
  49.             (*grid)[i][j] = val;
  50.             val++;
  51.         }
  52.         j++;
  53.         loop++;
  54.     }
  55.  
  56.     if(n % 2 != 0)
  57.     {
  58.         (*grid)[n/2][n/2] = n*n;
  59.  
  60.     }
  61.     return grid;
  62. }
  63.  
  64. int main()
  65. {
  66.     int n, i, j, **grid;
  67.     printf("Iveskite skaiciu N - ");
  68.     scanf("%d", &n);
  69.     fillin(n, &grid);
  70.     print(n, grid);
  71.     return 0;
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top