Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print two-dimensional array in spiral order

How do I print a 5×5 two-dimensional array in spiral order?

Is there any formula so that I can print an array of any size in spiral order?

like image 808
Rahul Vyas Avatar asked Apr 07 '09 17:04

Rahul Vyas


People also ask

How do I print numbers from spiral order?

Print the right column, i.e. Print the last column or n-1th column from row index k to m and decrease the count of n. Print the bottom row, i.e. if k < m, then print the elements of m-1th row from column n-1 to l and decrease the count of m.

What is 2D spiral array?

The Spiral Matrix problem takes a 2-Dimensional array of N-rows and M-columns as an input, and prints the elements of this matrix in spiral order. The spiral begins at the top left corner of the input matrix, and prints the elements it encounters, while looping towards the center of this matrix, in a clockwise manner.

How do you print a 2D array for loop?

Code: public class Print2DArrayInJava { public static void main(String[] args) { //below is declaration and intialisation of a 2D array final int[][] matrx = { { 11, 22}, { 41, 52}, }; for (int r = 0; r < matrx. length; r++) { //for loop for row iteration. for (int c = 0; c < matrx[r].


1 Answers

The idea is to treat the matrix as a series of layers, top-right layers and bottom-left layers. To print the matrix spirally we can peel layers from these matrix, print the peeled part and recursively call the print on the left over part. The recursion terminates when we don't have any more layers to print.

Input matrix:

1 2 3 4  5 6 7 8 9 0 1 2    3 4 5 6  7 8 9 1 

After peeling top-right layer:

 1 2 3 4         8 5 6 7  2 9 0 1  6    3 4 5  1  7 8 9 

After peeling bottom-left layer from sub-matrix:

   6 7 5  0 1    9  4 5 3  7 8 9  

After peeling top-right layer from sub-matrix:

    6 7       1       0  5    4 

After peeling bottom-left layer from sub-matrix:

  0   4 

Recursion terminates.


C functions:

// function to print the top-right peel of the matrix and  // recursively call the print bottom-left on the submatrix. void printTopRight(int a[][COL], int x1, int y1, int x2, int y2) {     int i = 0, j = 0;      // print values in the row.     for(i = x1; i<=x2; i++) {         printf("%d ", a[y1][i]);     }      // print values in the column.     for(j = y1 + 1; j <= y2; j++)         {         printf("%d ", a[j][x2]);     }      // see if more layers need to be printed.     if(x2-x1 > 0) {         // if yes recursively call the function to          // print the bottom left of the sub matrix.         printBottomLeft(a, x1, y1 + 1, x2-1, y2);     } }  // function to print the bottom-left peel of the matrix and  // recursively call the print top-right on the submatrix. void printBottomLeft(int a[][COL], int x1, int y1, int x2, int y2) {     int i = 0, j = 0;      // print the values in the row in reverse order.     for(i = x2; i>=x1; i--) {         printf("%d ", a[y2][i]);     }      // print the values in the col in reverse order.     for(j = y2 - 1; j >= y1; j--) {         printf("%d ", a[j][x1]);     }      // see if more layers need to be printed.     if(x2-x1 > 0) {         // if yes recursively call the function to          // print the top right of the sub matrix.         printTopRight(a, x1+1, y1, x2, y2-1);     } }  void printSpiral(int arr[][COL]) {     printTopRight(arr,0,0,COL-1,ROW-1);     printf("\n"); } 
like image 99
codaddict Avatar answered Sep 17 '22 00:09

codaddict