I am trying to make a program that will take an n x n matrix(2-dimensional array of size n) and multiply it by another n x n matrix(2 dimensional array of the same size). I need to do this without using the predefined method for multiplying matrices. To do this for a 2 x 2 array would be simple however my confusion is caused by figuring out how to do the multiplication for all n x n matrices. Here is the code I have written so far.
import java.util.*;
class matrixX
{
public static void main (String args[])
{
int n=0;
Scanner scan = new Scanner(System.in);
System.out.println("The array will be n x n. What do you want n to be?");
n=scan.nextInt();
while (n<2)
{
System.out.println("Error: Value not greater than 2. Try Again: ");
n=scan.nextInt();
}
int[][] number1 = new int[n][n];
for (int i=0; i<number1.length; i++)
{
for (int j=0; j<number1[i].length; j++)
{
number1[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number1[i][j]+" ");
}
System.out.println(" ");
}
System.out.println();
int[][] number2 = new int[n][n];
for (int i=0; i<number2.length; i++)
{
for (int j=0; j<number2[i].length; j++)
{
number2[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number2[i][j]+" ");
}
System.out.println(" ");
}
Okay, your last post helped me a lot in understanding this algorithm. My next question is how do I keep track of M. I get that it needs to be the location of the multiplication that is taking place. Also, what about the addition of the numbers after multiplication, when is that taken care of?
Take these 2 x 2 matrices for instance:
[ 4, 1 ] * [ 1, -2 ] = [ 4*1+1*4, 4*-2+1*-2] = [ 8, -10 ]
[ 5, 3 ] * [ 4, -2 ] = [ 5*1+3*4, 5*-2+3*-2] = [ 17, -16 ]
Note: This code does not currently compile due to the fact that I have yet to set up M as a variable.
import java.util.*;
import java.lang.Object;
class matrixX
{
public static void main (String args[])
{
int n=0;
Scanner scan = new Scanner(System.in);
System.out.println("The array will be n x n. What do you want n to be?");
n=scan.nextInt();
while (n<2)
{
System.out.println("Error: Value not greater than 2. Try Again: ");
n=scan.nextInt();
}
int[][] number1 = new int[n][n];
for (int i=0; i<number1.length; i++)
{
for (int j=0; j<number1[i].length; j++)
{
number1[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number1[i][j]+" ");
}
System.out.println(" ");
}
int x=number1.length;
System.out.println();
int[][] number2 = new int[n][n];
for (int i=0; i<number2.length; i++)
{
for (int j=0; j<number2[i].length; j++)
{
number2[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number2[i][j]+" ");
}
System.out.println(" ");
}
int y=number2.length;
int[][] answer = new int[n][n];
for (int row=0; row<answer.length; row++)
{
for(int col=0; col<answer.length; col++)
{
answer[row][col]=number1[row][M]*number2[M][col];
System.out.print(answer[row][col]+"\t");
}
System.out.println();
}
}
}
Last edited by Dexter-fan; February 10th, 2010 at 06:36 PM.
Please use the CODE tags and replace your code out there with proper formatting. It is hard to read that jumble the way it is.
I'm not sure what you mean by "how do I keep track of M?" M will be a variable... you will update it and access it as any other variable.
As far as the addition, try to work it out first. I don't want to just give you the answer and code. Think on it for a second, write it out on paper and it'll become clear.
Bookmarks