Home
Questions
Search
Forum
Contact
Guest Book
Polls!
Got a Question?
 
PREV
Programs
NEXT
 
(34 / 301)
 
 



Write a C program to print a square matrix helically.




Here is a C program to print a matrix helically. Printing a matrix helically means printing it in this spiral fashion



 >-----------+
             |
 +---->--+   |
 |       |   |
 |       |   |
 |   <---+   |
 |           |
 +-----------+





This is a simple program to print a matrix helically.


#include<stdio.h>

/* HELICAL MATRIX */

int main()
{
       int arr[][4] = { {1,2,3,4},
                        {5,6,7,8},
                        {9,10,11,12},
                        {13, 14, 15, 16}
                       };
     
       int i, j, k,middle,size;
       printf("\n\n");
       size = 4;
     
       for(i=size-1, j=0; i>0; i--, j++)
       {
              for(k=j; k<i; k++) printf("%d ", arr[j][k]);
              for(k=j; k<i; k++) printf("%d ", arr[k][i]);
              for(k=i; k>j; k--) printf("%d ", arr[i][k]);
              for(k=i; k>j; k--) printf("%d ", arr[k][j]);
       }
     
       middle = (size-1)/2;
       if (size % 2 == 1) printf("%d", arr[middle][middle]);
       printf("\n\n");
       return 1;
}




PREV
COMMENTS                                  INDEX                                  PRINT
NEXT



Last updated: November 3, 2005

www.cracktheinterview.com - Your destination for the most common IT interview questions, answers, frequently asked interview questions (FAQ), C Programs, C Datastructures for technical interviews conducted by the top IT companies around the world!