Advertisement
Mr_kindle

spiralprinting.c

Nov 24th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | Source Code | 0 0
  1.  
  2.  
  3. /*
  4.     Name: spiralprinting.c
  5.     Copyright:
  6.     Author: Mr.Kindle
  7.     Date: 25-11-22 00:30
  8.     Description: A matrix is dynamically allocated and its value is being printed spirally in clock-vise direction.
  9.    
  10. */
  11. #include<stdio.h>
  12. #include<malloc.h>
  13.  
  14.  
  15. void print_spiral(int *array, int row,int col);
  16. int main()
  17. {   int i,j,row,col;
  18.     int * array;
  19.     srand(time(0));//for random generation of numbers
  20.     printf("\nEnter row and col of matrix: ");
  21.     scanf("%d %d",&row,&col);
  22.     /*dynamically allocating the space for matrix*/
  23.     array = calloc(row * col, sizeof(int));
  24.     printf("\nAutomatically generated matrix of dimension %d * %d is following: \n",row,col);
  25.     for(i=0;i<row;i++)
  26.         for(j=0;j<col;j++)
  27.             array[i*col + j] = rand()%20 + 1;
  28.  
  29.     /*DISPLAY array*/
  30.     for(i=0;i<row;i++)
  31.         {
  32.             for(j=0;j<col;j++)
  33.                 printf("%-2d ",array[i* col + j]);
  34.             printf("\n");
  35.         }
  36.     printf("\nSpiral printing of the matrix is: \n");
  37.      
  38.    print_spiral(array,row,col);
  39.    
  40.    
  41. free(array);
  42. return 0;
  43. }//main
  44.  
  45. void print_spiral(int *array, int row, int col)
  46.     {  
  47.         int r = row, c = col;//so that row and col doesnot changed
  48.         /*since array is dynamically allocated so in order to access its element 'col' value required so cannot
  49.         change it hence taking 'c' and assigning 'col' value to it and making all changes in 'c'*/
  50.         int rownow = 0, colnow = 0,i;
  51.         /*each time while loop iterated the value of 'r' and 'c' decreased by '1' and the value of 'rownow' and
  52.         'colnow' incresed by '1'. Hence the difference between 'c' and 'colnow' or 'r' and'rownow' decrease by 2*/
  53.         while(rownow < r && colnow < c)
  54.             {
  55.                 /*print the first row from left to right*/
  56.                 for(i = colnow; i< c; i++)
  57.                     printf("%d ",array[rownow * col + i]);
  58.                 rownow++; r--;
  59.                 /*print the last column from top to bottom*/
  60.                 for(i=rownow;i<=r;i++)
  61.                     printf("%d ",array[i * col+ (c - 1)]);
  62.                 c--;
  63.                 /*print the last row from right to left*/
  64.                 for(i=c-1;i>=colnow;i--)
  65.                     printf("%d ",array[r * col + i]);
  66.                 /*print the first column from bottom to top*/
  67.                 //if(rown > colnow)
  68.                 for(i=r-1;i>=rownow;i--)
  69.                     printf("%d ",array[i * col + colnow]);
  70.                 colnow++;
  71.             }
  72.     }//end of print_spiral
  73.  
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement