[RESOLVED] Matrix Multiplication
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

#### Hybrid View

1. Junior Member
Join Date
Feb 2010
Posts
3

## [RESOLVED] Matrix Multiplication

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(" ");
}

}
}

2. Member +
Join Date
Feb 2008
Posts
966

## Re: Matrix Multiplication

Alright, you've written code to populate to nxn matricies with random values.

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.
The math doesn't change. The algorithm for performing multiplication on a 2x2 matrix and that of an 8x8 matrix are the same.

answer array[i][j] = array1[row][M] * array2[M][col]

Where M is the current position in the multiplication. Illustrated example:

| M 2 | * | M 6 | => 1st iteration of M
| 3 4 | * | 7 8 |

| 1 M | * | 5 6 | => 2nd iteration of M
| 3 4 | * | M 8 |
M will always go from the first index to n, so in Java array talk from 0 to (n - 1).

3. Junior Member
Join Date
Feb 2010
Posts
3

## Re: Matrix Multiplication

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;
{
{
}
System.out.println();
}
}
}
Last edited by Dexter-fan; February 10th, 2010 at 06:36 PM.

4. Member +
Join Date
Feb 2008
Posts
966

## Re: Matrix Multiplication

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•