Print a given n x m matrix in the zig - zag format

Print a given n x m array in the below format. If the starting position is in the top row, direction should be downward from that position and the pattern should continue. If the starting position is at the bottom row, direction should be upward from that position and the pattern should continue.


Input: row position = 4,
           col position   = 0


5              6              15           16           25
4              7              14           17           24
3              8              13           18           23
2              9              12           19           22
1              10            11           20           21

Output: 
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15   16  17  18  19  20  21  22  23  24  25
#include <stdio.h>
#include <stdlib.h>
#define n 5
int main(void) 
{
  int A[n][n]={{5,6,15,16,25},
         {4,7,14,17,24},
         {3,8,13,18,23},
         {2,9,12,19,22},
         {1,10,11,20,21}};
   int flip,i,j,col,row;
   
   /* get the row index to decide where we have to start
   If the index(row) is last index, we have to move upwards
   else if the index(row) is first, we have to move downwards */
   scanf("%d",&row);
   
   /* To set the orientation, upwards if row index is last row 
   else downwards, if row index is first row */
   if(row==n-1)
     flip=1;
   else
     flip=0;
    
    /* get the column index from which column we have to start */
   scanf("%d",&col);
   
   /* set the column idle */
   for(j=col;j<n;j++)
   {
    /* based on the orientation,
    if flip == 1, move upwards (from last row to first row of the same column ) */
     if(flip)
     {
      /* traverse the same column from last row to first row */
      for(i=n-1;i>=0;i--)
        printf("%d ",A[i][j]);
       
       /* change the orientation */
      flip=!(flip);
     }
     else
     {
      /* traverse the same column from first row to last column */
      for(i=0;i<n;i++)
        printf("%d ",A[i][j]);
       
       /* change the orientation */
      flip=!(flip);
     }
   }
   
   /* traverse from 0th column to the column you have started traversing in the previous part 
   & do the same type of traversal here */
   for(j=0;j<col;j++)
   {
     if(flip)
     {
      for(i=n-1;i>=0;i--)
        printf("%d ",A[i][j]);
      flip=!(flip);
     }
     else
     {
      for(i=0;i<n;i++)
        printf("%d ",A[i][j]);
      flip=!(flip);
     }
   }
  return 0;
}