This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help me with matrix...please!!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help me with matrix...please!!!!!!!" Watch "Help me with matrix...please!!!!!!!" New topic

Help me with matrix...please!!!!!!!


Joined: Sep 18, 2004
Posts: 1
how to use three two dimensional arrays. how to get this program :
� Ask size (rows and columns) of matrix A (using message dialog box)
� Ask size of Matrix B
� Check the sizes of A and B, if not compatible display waning
message and exit the program
� Input matrix A (using message dialog box),
� Input matrix B
� Multiply A and B giving matrix C
� Display matrix A and matrix B to command window
� Display matrix C to command window
The output looks like
Matrix A:
1.0 2.0 3.0
4.0 5.0 6.0
Matrix B:
7.0 8.0 9.0
0.0 9.0 8.0
7.0 6.0 5.0
Product of Matrix A and B:
28.0 44.0 40.0
70.0 113.0 106.0

thank you
// Perform matrix multiplication

public class MatrixOps {

public static void print_array(double[][] numbers)
for (int i=0; i<numbers.length; i++) {
for (int j=0; j<numbers[i].length; j++)
System.out.print(numbers[i][j] + " ");

public static double[][] multiply(double[][] a, double[][] b)
// determine sizes of matrices
int m = a.length;
int n = a[0].length;
int p = b[0].length;
int q = b.length;

// create array to hold product
double[][] c = new double[m][p]; // elements initialized to 0

// test for valid multiplication
if (n != q)
System.out.println("Number of rows of second matrix must match "
+ "number of colums of first matrix");
return c;

// create the product row by row
for (int i=0; i<m; ++i)
// produce the current row
for (int j=0; j < p; ++j)
for (int k=0; k<n; ++k)
c[i][j] += a[i][k] * b[k][j];

return c;

public static double[][] add(double[][] a, double[][] b)
// create array to hold result
// elements initialized to 0
double[][] c = new double[a.length][a[0].length];

// ensure correct matrix dimensions
if (a.length != b.length || a[0].length != b[0].length)
System.out.println("Number of rows and columns must be equal");
return c;

// add contents of corresponding elements
for (int i=0; i<a.length; i++)
for (int j=0; j<a[0].length; j++)
c[i][j] = a[i][j] + b[i][j];

return c;

public static void main(String[] args)
// declare and initialize
double[][] m1 = {{1, 2, 3}, {4, 5, 6}};
double[][] m2 = {{7, 8, 9}, {0, 9, 8}, {7, 6, 5}};

System.out.println("Matrix A :" );
System.out.println("Matrix B :" );


double[][] m3 = multiply(m1, m2);
System.out.println("\nThe Product of Matrix A and B: ");

Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hey Rush

I don't undersatnd what your question is ?

1> Do you want to know how the code works.
2> Do you want to know the concept behind two-dimensional arrays in java.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Help me with matrix...please!!!!!!!
It's not a secret anymore!