1.Given a 2D array / matrix of integers. Write a program to print the
elements in spiral order. Consider a matrix as show in the diagram to
the right. The desired output of the program should be as:
1,2,3,4,8,12,16,20,19,18,17,13,9,5,6, 7,11,15,14,10.
http://www.programcreek.com/2013/01/leetcode-spiral-matrix-java/
http://www.programcreek.com/2014/05/leetcode-spiral-matrix-ii-java/
3.Display a square matrix spirally using only 2 'for' loops.
http://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/
http://www.geeksforgeeks.org/print-n-x-n-spiral-matrix-using-o1-extra-space/
Solution 1: Square Matrix :
#include < stdio.h > #include < conio.h > /* HELICAL MATRIX */ int main() { int arr[][5] = { {1,2,3,4,17}, {5,6,7,8,18}, {9,10,11,12,19}, {13,14,15,16,20}, {21,22,23,24,25} }; int i, j, k,middle,size; printf("\n\n"); size = 5; 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"); getch(); return 0; }
Solution 2: 2D Matrix:
The idea is to consider the matrix similar to onion which can be peeled layer after layer. We can use the same approach to print the outer layer of the matrix and keep doing it recursively on a smaller matrix (with 1 less row and 1 less column).
We start by printing the top-right layer of the matrix by calling the print_layer_top_right. It will print 1,2,3,4,8,12,16,20. The print_layer_top_right method then calls the print_layer_bottom_left method which will print 19,18,17,13,9,5. If you observe the size of the target matrix is reducing after each call. Each method basically calls the other method and passes the matrix indexes for the reduced matrix. Both methods take 4 index parameters which represent the target matrix. When the target matrix size is such that there is only one layer left the recursion terminates and by this time the code has printed all the numbers in the full matrix.Code: #include <stdio.h> void print_layer_top_right(int a[][4], int x1, int y1, int x2, int y2); void print_layer_bottom_left(int a[][4], int x1, int y1, int x2, int y2); int main(void) { int a[5][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}, {17,18,19,20} }; print_layer_top_right(a,0,0,3,4); } // // prints the top and right shells of the matrix // void print_layer_top_right(int a[][4], int x1, int y1, int x2, int y2) { int i = 0, j = 0; // print the row for(i = x1; i<=x2; i++) { printf("%d,", a[y1][i]); } //print the column for(j = y1 + 1; j <= y2; j++) { printf("%d,", a[j][x2]); } // see if we have more cells left if(x2-x1 > 0) { // 'recursively' call the function to print the bottom-left layer print_layer_bottom_left(a, x1, y1 + 1, x2-1, y2); } } // // prints the bottom and left shells of the matrix // void print_layer_bottom_left(int a[][4], int x1, int y1, int x2, int y2) { int i = 0, j = 0; //print the row of the matrix in reverse for(i = x2; i>=x1; i--) { printf("%d,", a[y2][i]); } // print the last column of the matrix in reverse for(j = y2 - 1; j >= y1; j--) { printf("%d,", a[j][x1]); } if(x2-x1 > 0) { // 'recursively' call the function to print the top-right layer print_layer_top_right(a, x1+1, y1, x2, y2-1); } }
http://www.technicalinterviewquestions.net/2009/03/print-2d-array-matrix-spiral-order.html
ReplyDelete2 for loops:
ReplyDeletehttp://mycareerstack.com/question/42/
Other methods:
ReplyDeleteProjectsseminar.blogspot.com