Hi I'm trying to finish my program where I can either sum or multiply a matrix. The issue I'm having atm is that I can't figure out how to multiply them if I have 4 variables, 2 for the size in row and column in the first matrix, and another 2 for the row and size in the second matrix. This is the complete code:
import java.io.BufferedReader; import java.io.InputStreamReader; public class MatrizVaria{ public static int x,y,o,p; public static BufferedReader var = new BufferedReader(new InputStreamReader(System.in)); public void Fill(int a[][]){ int i,j; for (i=0;i<x;i++) for (j=0;j<y;j++) a[i][j] = (int)(Math.random()*10+1); } public void Sum(int a[][],int b[][],int c[][]){ int i,j; for (i=0;i<x;i++) for (j=0;j<y;j++) c[i][j] = a[i][j] + b[i][j]; } public void Product(int a[][], int b[][], int c[][]) { int i, j, k, l; for (i=0;i<x;i++) { for (j=0;j<y;j++) { c[i][j] = 0; for (k=0;k<x;k++) { for (l=0;l<y;l++) { c[i][j] += a[i][x]*b[x][j]; c[i][j] += a[y][j]*b[i][y]; } } } } } public void Print(int a[][]){ int i,j; for (i=0;i<x;i++){ for (j=0;j<y;j++) System.out.print("\t" + a[i][j]); System.out.println(); } } public static void main(String[] arg) throws Exception { String carac; do { System.out.print("\nDo you want to Sum(s) or Multiply(m) a matrix?: "); carac = var.readLine(); if (carac.equals("s")) { try{ System.out.println("Input the row size: "); x = Integer.parseInt(var.readLine()); System.out.println("Input the column size: "); y = Integer.parseInt(var.readLine()); int[][] Mat = new int[x][y]; int[][] Mat1 = new int[x][y]; int[][] Mat2 = new int[x][y]; MatrizVaria obj = new MatrizVaria(); obj.Fill(Mat); obj.Fill(Mat1); obj.Sum(Mat,Mat1,Mat2); System.out.println("The first Matrix is: "); obj.Print(Mat); System.out.println("The second Matrix is: "); obj.Print(Mat1); System.out.println("The sum of the Matrixes is : "); obj.Print(Mat2); }catch(NumberFormatException nfe){ System.out.println(" Incorrect Input "); } } if (carac.equals("m")) { try{ System.out.println("Input the row size of the first Matrix: "); x = Integer.parseInt(var.readLine()); System.out.println("Input the column size of the first Matrix: "); y = Integer.parseInt(var.readLine()); System.out.println("Input the row size of the second Matrix: "); o = Integer.parseInt(var.readLine()); System.out.println("Input the column size of the second Matrix: "); p = Integer.parseInt(var.readLine()); int[][] Mat = new int[x][y]; int[][] Mat1 = new int[o][p]; int[][] Mat2 = new int[x][y]; MatrizVaria obj = new MatrizVaria(); obj.Fill(Mat); obj.Fill(Mat1); obj.Product(Mat,Mat1,Mat2); System.out.println("The first Matrix is: "); obj.Print(Mat); System.out.println("The second Matrix is: "); obj.Print(Mat1); System.out.println("The product of both Matrixes is: "); obj.Print(Mat2); }catch(NumberFormatException nfe){ System.out.println(" Incorrect Input "); } } else { System.out.print("Invalid Option"); } System.out.print("\nWould you like to perform another operation (y/n)?: "); carac = var.readLine(); } while(carac.equals("y")); if(carac.equals("n")); System.exit(0); } }
The part of it i want to finish is:
public void Product(int a[][], int b[][], int c[][]) { int i, j, k, l; for (i=0;i<x;i++) { for (j=0;j<y;j++) { c[i][j] = 0; for (k=0;k<x;k++) { for (l=0;l<y;l++) { c[i][j] += a[i][x]*b[x][j]; c[i][j] += a[y][j]*b[i][y]; } } } } }if (carac.equals("m")) { try{ System.out.println("Input the row size of the first Matrix: "); x = Integer.parseInt(var.readLine()); System.out.println("Input the column size of the first Matrix: "); y = Integer.parseInt(var.readLine()); System.out.println("Input the row size of the second Matrix: "); o = Integer.parseInt(var.readLine()); System.out.println("Input the column size of the second Matrix: "); p = Integer.parseInt(var.readLine()); int[][] Mat = new int[x][y]; int[][] Mat1 = new int[o][p]; int[][] Mat2 = new int[x][y]; MatrizVaria obj = new MatrizVaria(); obj.Fill(Mat); obj.Fill(Mat1); obj.Product(Mat,Mat1,Mat2); System.out.println("The first Matrix is: "); obj.Print(Mat); System.out.println("The second Matrix is: "); obj.Print(Mat1); System.out.println("The product of both Matrixes is: "); obj.Print(Mat2); }catch(NumberFormatException nfe){ System.out.println(" Incorrect Input "); } }
Please help me, I really need to finish this part. Thank you