Skip to content
Advertisement

How to fix a matrix multiplication in Java

I’m creating a class in Java to perform simple operations with matrices using two-dimensional arrays. I’m running into a problem with my method for matrix multiplication.

Whenever I test my multiply method, no error appears, but my computer CPU utilization increases quite a bit and my tester program never finishes.

This is my multiply method:

JavaScript

Where I construct a matrix as follows:

JavaScript

This occurs even when I test very small matrices, so it must be a problem with my code, but I can’t figure out what it is.

Advertisement

Answer

You’re not incrementing n in your inner n loop. As is mentioned above, for loops are more appropriate when looping a predefined number of times.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement