Java matrix problem

Joined
Sep 10, 2023
Messages
2
Reaction score
0
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.

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(" ");
    }
}
 
Joined
May 23, 2022
Messages
4
Reaction score
1
you can't compare arrays as
Java:
arr1 == arr2
it compares object references instead of values

Java:
while (! check) {
      matrix1 = fillWithRandom( matrix1, min, max);
      matrix2 = fillWithRandom( matrix2, min, max);
      int[][] attempt = multiply( matrix1, matrix2);

      // if (result == attempt) {
      if ( Arrays.deepEquals( result, attempt) ) {
        System.out.println("First matrix: ");
        System.out.println();
        printArray( matrix1);
      
        System.out.println("Second matrix: ");
        System.out.println();
        printArray( matrix2);
      
        System.out.println("Result: ");
        System.out.println();
        printArray( result);
        check = true;
      }
    }

but searching randomly for the result is an ineffective method, I have generated 500,000 attempts for a 3x3 matrix with no success
 
Last edited:
Joined
Sep 10, 2023
Messages
2
Reaction score
0
Yeah, I realized after some time, that this method wouldn't work effectively due to the gigantic amounts of combinations.
Still thanks a lot for your help.
 
Joined
May 23, 2022
Messages
4
Reaction score
1
perhaps you can guess inputs for first number in result matrix,
when found it, then for second ..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top