Here is the code for printing an Array in Spiral order:
public static void PrintInSpiral(int [][] numbers, int row, int col) { int k=0,i=0,j=0,m=0; for(i=col-1, m=row-1, j=0; i>= 0&& m>=0 && j<(col-1) && j<(row-1); m–,j++,i–) { for( k = j ; k < i; k++) System.out.print(numbers[j][k]+ " "); for( k = j ; k j; k–) System.out.print(numbers[m][k]+ " "); for( k = m ; k > j; k–) System.out.print(numbers[k][j]+ " "); } if(row==col&& row%2!=0){ k=(int)((row-1)/2); System.out.print( numbers[k][k]); } System.out.println(); }
No comments:
Post a Comment