I hope the topic name doesn't scare away too many people, because I'm looking for help, once again lol.
I've been working on a program to multiply two matrices (two dimensional arrays). I think I've got most of it down, but the problem is, I can't figure out how to store the result in the 3rd array.
ie
C[1][1] = (B[1][1] * A[1][1]) + (B[1][1] * A[1][2]) + (B[1][1] * A[1][3])
(if thats right, I've become so confused, and I know it starts at 0,0 but starting off at 1 helps me remember lol).
So here is my source code:
After reading the thread of why offering help, I feel sort of guilty to ask, but I feel accomplished that I've made it this far, but I don't expect someone to write the code for me, so if you'd rather just generally explain it (without a code), or explain how to keep track and store the result in matrixC, I would appreciate that just as much--I just feel stuck and confused lol.
Thanks for all the help!
I've been working on a program to multiply two matrices (two dimensional arrays). I think I've got most of it down, but the problem is, I can't figure out how to store the result in the 3rd array.
ie
C[1][1] = (B[1][1] * A[1][1]) + (B[1][1] * A[1][2]) + (B[1][1] * A[1][3])
(if thats right, I've become so confused, and I know it starts at 0,0 but starting off at 1 helps me remember lol).
So here is my source code:
Code:
import java.util.Scanner;
public class MulitplyingMatrices {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the width of matrix a");
int matrixA_rows = scanner.nextInt();
System.out.println("Please enter the height of matrix a");
int matrixA_cols = scanner.nextInt();
System.out.println("Please enter the width of matrix b");
int matrixB_rows = scanner.nextInt();
System.out.println("Please enter the height of matrix b");
int matrixB_cols = scanner.nextInt();
/*
if(matrixA_rows != matrixB_cols)
exit();
*/
int[][] matrixA = new int[matrixA_rows][matrixA_cols];
int[][] matrixB = new int[matrixB_rows][matrixB_cols];
int[][] matrixC = new int[matrixB_rows][matrixA_cols];
GenerateArray(matrixA);
GenerateArray(matrixB);
MultiplyArrays(matrixA, matrixB, matrixC);
}
public static void GenerateArray(int[][] matrix) {
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[i].length; j++ )
{
matrix[i][j] = 1 + (int)(Math.random() * 10);
// System.out.println("Matrix[" + i + "][" + j + "] - " + matrix[i][j]);
}
}
}
public static void MultiplyArrays(int[][] matrixA, int[][] matrixB, int[][] matrixC) {
int temp = 0;
int counter = 1;
int temp2 = 0;
for(int i = 0; i < matrixB.length; i++)
{
for(int j = 0; j < matrixA[i].length; j++)
{
temp += matrixB[i][j] * matrixA[j][i];
// System.out.println(temp);
}
temp2 = counter - 1;
matrixC[temp2][i] = temp;
System.out.println("MatrixC[" + temp2 + "][" + i + "] - " + matrixC[temp2][i]);
if(counter == matrixB.length)
counter = 1;
else
counter++;
}
}
}
After reading the thread of why offering help, I feel sort of guilty to ask, but I feel accomplished that I've made it this far, but I don't expect someone to write the code for me, so if you'd rather just generally explain it (without a code), or explain how to keep track and store the result in matrixC, I would appreciate that just as much--I just feel stuck and confused lol.
Thanks for all the help!