Advertisement
ivan_yosifov

4_Types_Of_Matrices

Jun 7th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package demo;
  2.  
  3. public class HelloJava {
  4.     public static void main(String[] args) {
  5.         int N = 4;
  6.        
  7.         int[][] arr = new int[N][N];
  8.         int counter = 1;
  9.        
  10.         // Columns matrix
  11.         for(int col = 0; col < arr.length; col++){
  12.             for(int row = 0; row < arr[col].length; row++){
  13.                 arr[row][col] = counter;
  14.                 counter++;
  15.             }
  16.         }
  17.        
  18.         printMatrix(arr);
  19.        
  20.         System.out.println();
  21.        
  22.         // Alternating columns matrix
  23.         counter = 1;
  24.         for(int col = 0; col < arr.length; col++){
  25.             if(col % 2 == 0){
  26.                 for(int row = 0; row < arr[col].length; row++){
  27.                     arr[row][col] = counter;
  28.                     counter++;
  29.                 }
  30.             }else{
  31.                 for(int row = arr[col].length - 1; row >= 0; row--){
  32.                     arr[row][col] = counter;
  33.                     counter++;
  34.                 }
  35.             }
  36.         }
  37.        
  38.         printMatrix(arr);
  39.        
  40.         System.out.println();
  41.        
  42.         // Diagonal matrix
  43.         counter = 1;
  44.         int row = N - 1;
  45.         int col = 0;
  46.        
  47.         while(counter <= (N * N)){
  48.             arr[row][col] = counter;
  49.             counter++;
  50.            
  51.             if(row == N - 1 && col != N - 1){              
  52.                 row = N - 2 - col;
  53.                 col = 0;
  54.             }else if(col == N - 1){
  55.                 col = col - row + 1;
  56.                 row = 0;
  57.             }else{
  58.                 row++;
  59.                 col++;
  60.             }
  61.         }
  62.        
  63.         printMatrix(arr);
  64.        
  65.         System.out.println();
  66.        
  67.         for(int i = 0; i < arr.length; i++){
  68.             for(int j = 0; j < arr[i].length; j++){
  69.                 arr[i][j] = 0;
  70.             }
  71.         }
  72.        
  73.         // Spiral matrix
  74.         counter = 1;
  75.         row = 0;
  76.         col = 0;
  77.         String direction = "down";
  78.        
  79.         while(counter <= (N * N)){
  80.             arr[row][col] = counter;
  81.             counter++;
  82.            
  83.             if(direction == "down"){
  84.                 row++;
  85.                
  86.                 if(arr[row][col] != 0){
  87.                     direction = "right";
  88.                     row--;
  89.                     col++;
  90.                 }else if(row == (N - 1)){
  91.                     direction = "right";
  92.                 }
  93.  
  94.             }else if(direction == "right"){
  95.                 col++;
  96.                 if(arr[row][col] != 0){
  97.                     direction = "up";
  98.                     col--;
  99.                     row--;
  100.                 }else if(col == (N - 1)){
  101.                     direction = "up";
  102.                 }
  103.  
  104.             }else if(direction == "up"){
  105.                 row--;
  106.                
  107.                 if(arr[row][col] != 0){
  108.                     direction = "left";
  109.                     row++;
  110.                     col--;
  111.                 }else if(row == 0){
  112.                     direction = "left";
  113.                 }
  114.                
  115.             }else if(direction == "left"){
  116.                 col--;
  117.                 if(col == (N - 1)){
  118.                     direction = "down";
  119.                 }else if(arr[row][col] != 0){
  120.                     direction = "down";
  121.                     col++;
  122.                     row++;
  123.                 }
  124.                
  125.             }
  126.         }
  127.        
  128.         printMatrix(arr);
  129.        
  130.         System.out.println();      
  131.        
  132.        
  133.     }  
  134.    
  135.     public static void printMatrix(int[][] arr){
  136.         int spaces = Integer.toString(arr.length * arr.length).length();
  137.        
  138.         for(int i = 0; i < arr.length; i++){
  139.             for(int j = 0; j < arr[i].length; j++){
  140.                 System.out.print(padSpace(arr[i][j], spaces));
  141.                 System.out.print((j != arr[i].length - 1) ? " " : "");
  142.             }
  143.             System.out.println();
  144.         }
  145.     }
  146.    
  147.     public static String padSpace(int num, int spaces){
  148.         String result = String.valueOf(num);
  149.         int difference = spaces - result.length();
  150.        
  151.         if(difference > 0){
  152.             for(int i = 0; i < difference; i++){
  153.                 result = " " + result;
  154.             }
  155.         }
  156.         return result;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement