Showing posts with label 2D Array. Show all posts
Showing posts with label 2D Array. Show all posts

Monday, July 16, 2018

How do you write an algorithm for the multiplication of two matrix [2D array]?

Problem statement:
Given two matrices, the task is to multiply them. Matrices can either be square or rectangular.
Example:
Input: int m1[][] = {{2, 3, 4}, {5, 6, 7}}; // 2 X 3 matrix
int m2[][] = {{1, 2}, {3, 4}, {5, 6}}; // 3 X 2 matrix
Output: {{31, 40},{58,76}}
  1. public class MatrixMultiplication {
  2.     public static void main(String args[]) {
  3.         int m1[][] = {{2, 3, 4}, {5, 6, 7}};    // 2 X 3 matrix
  4.         int m2[][] = {{1, 2}, {3, 4}, {5, 6}};  // 3 X 2 matrix
  5.         int res[][] = new int[2][2];    // resultant matrix of 2 X 2
  6.         for (int i = 0; i < 2; i++) {   // i represent row
  7.             for (int j = 0; j < 2; j++) {   // j represent column
  8.                 res[i][j] = 0;  // assume res[0][0] = 0
  9.                 for (int k = 0; k < 3; k++) {
  10.                     // k represent first matrix i.e. m1 -> number of column for                        // counter sum
  11.                     res[i][j] = res[i][j] + m1[i][k] * m2[k][j];
  12.                 }
  13.             }
  14.         }
  15.         // printing the resultant matrix
  16.         for (int i = 0; i < 2; i++) {
  17.             for (int j = 0; j < 2; j++) {
  18.                 System.out.print(res[i][j] + " ");
  19.             }
  20.             System.out.println();
  21.         }
  22.     }
  23. }
Method-II:
  1. public class MatrixMultiplication2D {
  2.     public static void main(String args[]) {
  3.         int m1[][] = {{2, 3, 4}, {5, 6, 7}};    // 2 X 3 matrix
  4.         int m2[][] = {{1, 2}, {3, 4}, {5, 6}};  // 3 X 2 matrix
  5.         int sum = 0;    // initial sum value is 0
  6.         int res[][] = new int[2][2];    // resultant matrix of 2 X 2
  7.         for (int i = 0; i < 2; i++) {   // i represent row
  8.             for (int j = 0; j < 2; j++) {   // j represent column
  9.                 res[i][j] = 0;  // assume res[0][0] = 0
  10.                 for (int k = 0; k < 3; k++) {
  11.                     // k represent first matrix i.e. m1 -> number of column for                        // counter sum
  12.                     sum = sum + m1[i][k] * m2[k][j];
  13.                 }
  14.                 res[i][j] = sum;    //  assigned sum value to resultant matrix
  15.                 sum = 0;    // reset to 0 for next iteration
  16.             }
  17.         }
  18.         // printing the resultant matrix
  19.         for (int i = 0; i < 2; i++) {
  20.             for (int j = 0; j < 2; j++) {
  21.                 System.out.print(res[i][j] + " ");
  22.             }
  23.             System.out.println();
  24.         }
  25.     }
  26. }
Output:
31 40
58 76

Tuesday, July 3, 2018

How do you print all elements of a matrix in diagonal order?

  1. public class PrintDiagonalMatrix
  2. {
  3.   static final int ROW = 5;
  4.   static final int COL = 4;
  5.   // min of two numbers
  6.   static int min(int a, int b)
  7.   {
  8.     return (a < b) ? a : b;
  9.   }
  10.   // min of three numbers
  11.   static int min(int a, int b, int c)
  12.   {
  13.     return min(min(a, b), c);
  14.   }
  15.   // max of two numbers
  16.   static int max(int a, int b)
  17.   {
  18.     return (a > b) ? a : b;
  19.   }
  20.   // print matrix in diagonal order
  21.   static void diagonalOrder(int matrix[][])
  22.   {
  23.     for (int line = 1; line <= (ROW + COL - 1); line++)
  24.     {
  25.       int start_col = max(0, line - ROW);
  26.       int count = min(line, (COL - start_col), ROW);
  27.       for (int j = 0; j < count; j++)
  28.         System.out.print(matrix[min(ROW, line) - j - 1][start_col + j] + " ");
  29.       System.out.println();
  30.     }
  31.   }
  32.   // print a matrix
  33.   static void printMatrix(int matrix[][])
  34.   {
  35.     for (int i = 0; i < ROW; i++)
  36.     {
  37.       for (int j = 0; j < COL; j++)
  38.         System.out.print(matrix[i][j] + " ");
  39.       System.out.print("\n");
  40.     }
  41.   }
  42.   public static void main(String[] args)
  43.   {
  44.     int M[][] =
  45.     {
  46.         { 1, 2, 3, 4 },
  47.         { 5, 6, 7, 8 },
  48.         { 9, 10, 11, 12 },
  49.         { 13, 14, 15, 16 },
  50.         { 17, 18, 19, 20 }, };
  51.     System.out.print("Given matrix is \n");
  52.     printMatrix(M);
  53.     System.out.print("\nDiagonal printing of matrix is \n");
  54.     diagonalOrder(M);
  55.   }
  56. }
Output:
Given matrix is
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20

Diagonal printing of matrix is
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20 

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...