Problem:
===========
matrix-matrix multiplication (square matrices)
Algorithm:
===========
1. Initialize the two square matrices using SecureRandom.
2. Declare another square matrices to hold the result.
3. Iterate the two arrays and calculate the product of two array values and assign the result to resultant array.
4. Print the result.
Solution:
==========
import java.util.*;
import java.security.SecureRandom;
class MatrixMultiplicationUtility{
public static void main(String[] args){
int[][] a = new int[3][3];
int[][] b = new int[3][3];
int[][] c = new int[3][3];
initializeArray(a);
System.out.println("Printing array one: ");
printArrayLikeMatrix(a);
initializeArray(b);
System.out.println("Printing array two: ");
printArrayLikeMatrix(b);
multiplyArrays(a, b, c);
System.out.println("Printing result of matrix multiplication ");
printArrayLikeMatrix(c);
}
public static void initializeArray(int[][] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] = random.nextInt(10);
}
}
}
public static void printArrayLikeMatrix(int[][] array){
for(int i=0;i < array.length; i++){
for(int j = 0; j < array.length; j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
public static void multiplyArrays(int[][] arrayOne, int[][] arrayTwo, int[][] arrayThree){
for(int i=0;i < arrayOne.length; i++){
for(int j = 0; j < arrayTwo.length; j++){
arrayThree[i][j] = arrayOne[i][j] * arrayTwo[i][j];
}
}
}
}
Output:
=======
Printing array one:
5 8 4
2 2 4
6 5 6
Printing array two:
8 1 6
2 0 8
7 5 5
Printing result of matrix multiplication
40 8 24
4 0 32
42 25 30
===========
matrix-matrix multiplication (square matrices)
Algorithm:
===========
1. Initialize the two square matrices using SecureRandom.
2. Declare another square matrices to hold the result.
3. Iterate the two arrays and calculate the product of two array values and assign the result to resultant array.
4. Print the result.
Solution:
==========
import java.util.*;
import java.security.SecureRandom;
class MatrixMultiplicationUtility{
public static void main(String[] args){
int[][] a = new int[3][3];
int[][] b = new int[3][3];
int[][] c = new int[3][3];
initializeArray(a);
System.out.println("Printing array one: ");
printArrayLikeMatrix(a);
initializeArray(b);
System.out.println("Printing array two: ");
printArrayLikeMatrix(b);
multiplyArrays(a, b, c);
System.out.println("Printing result of matrix multiplication ");
printArrayLikeMatrix(c);
}
public static void initializeArray(int[][] array){
Random random = new SecureRandom();
for(int i=0;i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] = random.nextInt(10);
}
}
}
public static void printArrayLikeMatrix(int[][] array){
for(int i=0;i < array.length; i++){
for(int j = 0; j < array.length; j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
public static void multiplyArrays(int[][] arrayOne, int[][] arrayTwo, int[][] arrayThree){
for(int i=0;i < arrayOne.length; i++){
for(int j = 0; j < arrayTwo.length; j++){
arrayThree[i][j] = arrayOne[i][j] * arrayTwo[i][j];
}
}
}
}
Output:
=======
Printing array one:
5 8 4
2 2 4
6 5 6
Printing array two:
8 1 6
2 0 8
7 5 5
Printing result of matrix multiplication
40 8 24
4 0 32
42 25 30
No comments:
Post a Comment