Hi everybody, I ran into a problem while working on my seminary paper, which is called matrix calculation, creation, and automatization.
The code, that is posted with this as an attachment takes a matrix as an input and creates two matrices. And when you multiply them, the result is the inputted matrix. While testing the final version of the code I ran into a problem. After I filled out all of the inputs the code just didn't continue working and no output was printed out. It might be due to my poor coding or just the fact that it uses semi-random numbers. Any ideas on a fix, or an improvement?
Any reply would be highly appreciated.
The code, that is posted with this as an attachment takes a matrix as an input and creates two matrices. And when you multiply them, the result is the inputted matrix. While testing the final version of the code I ran into a problem. After I filled out all of the inputs the code just didn't continue working and no output was printed out. It might be due to my poor coding or just the fact that it uses semi-random numbers. Any ideas on a fix, or an improvement?
Any reply would be highly appreciated.
Java:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
System.out.println("Enter the number of rows of your final matrix: ");
int m = sc.nextInt();
System.out.println("Enter the number of columns of your final matrix: ");
int n = sc.nextInt();
int[][] result = new int[m][n];
System.out.println("Enter the values of the final matrix: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = sc.nextInt();
if (result[i][j] > max) max = result[i][j];
if (result[i][j] < min) min = result[i][j];
}
}
if (max > 0 && min >= 0) {
min = -1 * max;
} else if (max <= 0 && min < 0) {
max = -1 * min;
}
for (int[] row : result) {
System.out.println(Arrays.toString(row));
}
System.out.println(" ");
System.out.println("!!!Parameters of matrices must be multiplied in the following way -> m*p * p*n = m*n !!!");
System.out.println("Enter the shared parameter of both matrices (p): ");
int p = sc.nextInt();
int[][] matrix1 = new int[m][p];
int[][] matrix2 = new int[p][n];
boolean check = false;
while (!check) {
if (result == multiply(fillWithRandom(matrix1, min, max), fillWithRandom(matrix2, min, max))) {
System.out.println("First matrix: ");
System.out.println();
printArray(fillWithRandom(matrix1, min, max));
System.out.println("Second matrix: ");
System.out.println();
printArray(fillWithRandom(matrix2, min, max));
System.out.println("Result: ");
System.out.println();
printArray(result);
check = true;
} else {
multiply(fillWithRandom(matrix1, min, max), fillWithRandom(matrix2, min, max));
}
}
}
public static int[][] fillWithRandom(int[][] array, int minimum, int maximum){
Random random = new Random();
int length = array[0].length;
int height = array.length;
for(int i = 0; i < height; i++){
for (int j = 0; j < length; j++){
array[i][j] = random.nextInt( maximum - minimum + 1) + minimum;
}
}
return array;
}
public static int[][] multiply(int[][] array1, int[][] array2){
int r1 = array1.length;
int rc = array2.length;
int c2 = array2[0].length;
int[][] finalMatrix = new int[r1][c2];
for(int i = 0; i < r1; i++){
for(int j = 0; j < c2; j++){
int number = 0;
for(int k = 0; k < rc; k++){
number += array1[i][k] * array2[k][j];
}
finalMatrix[i][j] = number;
}
}
return finalMatrix;
}
public static void printArray(int[][] array) {
for (int[] row : array){
System.out.println(Arrays.toString(row));
}
System.out.println(" ");
}
}