Tuesday 24 October 2017

Java Code for Matrix Multiplication

Problem:

Java Code for Matrix Multiplication

Solution:

package com.basics;

import java.util.Scanner;

public class MatrixMultiplication {

static int[][] array1;
static int[][] array2;
static int[][] array3;
int rowLimit;
int columnLimit;


public static void main(String[] args) {
MatrixMultiplication matrixMultiplication = new MatrixMultiplication();
matrixMultiplication.initializeTheArrays();
System.out.println("the first matrix elements are: ");
matrixMultiplication.printMatrix(array1);
System.out.println("the second matrix elements are: ");
matrixMultiplication.printMatrix(array2);
matrixMultiplication.doMatrixMultiplication();
System.out.println("the resulted matrix elements are: ");
matrixMultiplication.printMatrix(array3);
}

public void initializeTheArrays(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array row limit");
rowLimit = scanner.nextInt();
System.out.println("enter the array column limit");
columnLimit = scanner.nextInt();
array1 = new int[rowLimit][columnLimit];
System.out.println("enter the array1 elements");
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.println("enter the element");
array1[i][j] = scanner.nextInt();
}

}

array2 = new int[rowLimit][columnLimit];
System.out.println("enter the array2 elements");
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.println("enter the element");
array2[i][j] = scanner.nextInt();
}
}

array3 = new int[rowLimit][columnLimit];
scanner.close();
}

public void doMatrixMultiplication(){
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
for (int k = 0; k < rowLimit; k++)
                {
                    array3[i][j] = array3[i][j] + array1[i][k] * array2[k][j];
                }
}
}
}

public void printMatrix(int[][] array){
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}

}


Output:

enter the array row limit
2
enter the array column limit
2
enter the array1 elements
enter the element
1
enter the element
2
enter the element
3
enter the element
4
enter the array2 elements
enter the element
4
enter the element
3
enter the element
2
enter the element
1
the first matrix elements are:
1 2
3 4
the second matrix elements are:
4 3
2 1
the resulted matrix elements are:
8 5
20 13


enter the array row limit
3
enter the array column limit
3
enter the array1 elements
enter the element
1
enter the element
2
enter the element
3
enter the element
4
enter the element
5
enter the element
6
enter the element
7
enter the element
8
enter the element
9
enter the array2 elements
enter the element
2
enter the element
3
enter the element
4
enter the element
5
enter the element
6
enter the element
7
enter the element
8
enter the element
9
enter the element
1
the first matrix elements are: 
1 2 3 
4 5 6 
7 8 9 
the second matrix elements are: 
2 3 4 
5 6 7 
8 9 1 
the resulted matrix elements are: 
36 42 21 
81 96 57 
126 150 93 


1 comment:

  1. Need to check one edge case where number of column in matrix1 is not same as row number in matrix2.

    public static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2) {
    int cols1 = matrix1[0].length;
    int rows1 = matrix1.length;
    int cols2 = matrix2[0].length;
    int rows2 = matrix2.length;
    int sum = 0;
    if(cols1 != rows2) {
    throw new SomeCustomRuntimeException();//to signify matrix1 * matrix2 is not possible
    }

    int[][] matrix3 = new int[rows1][cols2];

    for(int i = 0; i < rows1; i++) {

    for(int j = 0; j < cols2; j++) {
    sum = 0;
    for(int k = 0; k < cols1; k++) {
    sum += matrix1[i][k] * matrix2[k][j];
    }
    matrix3[i][j] = sum;
    }
    }
    return matrix3;
    }

    ReplyDelete

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...