+ 2

java.lang.ArrayIndexOutOfBoundsException error when multiply matrices

Hello everyone. So the problem that I have is when I try to do this part of my code : cMatrix m2 = new cMatrix(10,20); m2.AssignRandom(); cMatrix m3 = new cMatrix(20,10); m3.AssignRandom(); cMatrix m4 = new cMatrix(); m4 = m2.multiplyMatrices(m3); m4.printMatrixWithPrime(); I get the error on the title. Letme show you my "multiplyMatrices" method. public cMatrix multiplyMatrices(cMatrix Multiplicand){ long sum = 0; for (int i = 0; i < Multiplicand.row; i++){ for (int j = 0; j < Multiplicand.col; j++){ for (int k = 0; k < col; k++){ sum = sum + elements[i][k] + elements[k][j]; } Multiplicand.elements[i][j] = sum; sum = 0; } } return null; } For your information : public cMatrix(){ row = 10; col = 10; elements = new long [row][col]; } public cMatrix(int row, int col){ this.row = row; this.col = col; elements = new long [row][col]; } those are the constructors. Every other methods are working without errors but the multiplaction gives me the eror on the title and compiler says that : sum = sum + elements[i][k] + elements[k][j]; m4 = m2.multiplyMatrices(m3); this two has got errors. Can someone help ?

8th Nov 2021, 3:17 PM
Güneş Özkan
Güneş Özkan - avatar
20 Respostas
+ 2
What's the error now?Güneş What's your update in code?
8th Nov 2021, 6:22 PM
Jayakrishna 🇮🇳
+ 1
You have problem with an index of an array. It should be analyzed step by step. The whole code can be divided on small moduls and tested with every biger cicle. And then you will see where comming error.
8th Nov 2021, 3:36 PM
JaScript
JaScript - avatar
+ 1
I think , From multiply matrices() method, you are returning null and calling print method. That may problem. Try return Multiplicand; And check also what is cal value there? may it depends on that also.
8th Nov 2021, 3:55 PM
Jayakrishna 🇮🇳
+ 1
Showing the full code may help.. not enough information now. Also for matrix multiplication, there must be a condition m1.column==m2.row I think , not sure exactly what it is. edit: Güneş Özkan Are you checked this:?? To multiply two matrices, the number of columns of the first matrix should be equal to the number of rows of the second matrix.
8th Nov 2021, 4:05 PM
Jayakrishna 🇮🇳
+ 1
Unfortunately, you don't know what's in the rest of the code. Please save your whole code on SL Payground.
8th Nov 2021, 4:11 PM
JaScript
JaScript - avatar
+ 1
Now is the code without errors. https://code.sololearn.com/cNfz96Mc7nNp/?ref=app
8th Nov 2021, 4:46 PM
JaScript
JaScript - avatar
+ 1
Its hard to find like that copy.Güneş Özkan Save it in fresh temp file and share link. You can delete later. [Don't change original code you posted first to not confuse readers..]
8th Nov 2021, 6:29 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I tried it but it gives me the same error. Any other suggestions ? JaScript I don't understand what you mean there. Can you explain more ?
8th Nov 2021, 3:59 PM
Güneş Özkan
Güneş Özkan - avatar
0
https://code.sololearn.com/cM2P75MVaoMp/#java Here is the whole code. Jayakrishna🇮🇳 JaScript
8th Nov 2021, 4:23 PM
Güneş Özkan
Güneş Özkan - avatar
0
Jayakrishna🇮🇳 I shared the whole code man. You can check it. JaScript
8th Nov 2021, 4:30 PM
Güneş Özkan
Güneş Özkan - avatar
0
JaScript What did you change exactly ? I noticed that you deleted 10000 in the nextLong in assignRandom but it has to be 10000. I could not see what did you change. Edit : It still gives the same error in your code.
8th Nov 2021, 4:56 PM
Güneş Özkan
Güneş Özkan - avatar
0
public cMatrix multiplyMatrices(cMatrix Multiplicand){ long sum = 0; for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ for (int k = 0; k < row; k++){ //System.out.println(k); sum = sum + (elements[i][k] * elements[k][j]); } Multiplicand.elements[i][j] = sum; sum = 0; } } return Multiplicand; } //Try this..
8th Nov 2021, 5:58 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 it didn't work unfortunatelly.
8th Nov 2021, 6:04 PM
Güneş Özkan
Güneş Özkan - avatar
0
Well I updated to this one: long sum = 0; int row2 = row; int col2 = col; Multiplicand.elements = new long [row2][col2]; for (int i = 0; i < row2; i++){ for (int j = 0; j < col2; j++){ for (int k = 0; k < row; k++){ sum = sum + elements[i][k] * elements[k][j]; } Multiplicand.elements[i][j] = sum; sum = 0; } } return Multiplicand; Finally it can print the output but the strange thing is, it gives me an error in line 98 which is : if(isPrime(elements[i][j])) and the error is : Index 10 out of bounds for length 10 Jayakrishna🇮🇳
8th Nov 2021, 6:25 PM
Güneş Özkan
Güneş Özkan - avatar
8th Nov 2021, 6:31 PM
Güneş Özkan
Güneş Özkan - avatar
0
There are points to remember that cMatrix m4 = new cMatrix(); this matrix have default row,col values m4 = m2.multiplyMatrices(m3); here you are passing m3 matrix of your 2nd (row 10,col 20) and doing multiplication m2 matrix only saving back reslut in m2 which row,col are (20,10) Actually you need to use (m3 matrix) and multiply with m2 matrix, So use this.row , m2.col then create a new matrix of (m3.row,m2.col) and save result in this. Return this matrix.
8th Nov 2021, 7:09 PM
Jayakrishna 🇮🇳
0
So I guess the problem is in : public cMatrix(){ row = 10; col = 10; elements = new long [row][col]; } this constructor. But the problem is in my homework, we have to create a default matrix which is 10*10. How can I transform it so the code works perfect ? Btw, the teacher is writing this part : cMatrix m1 = new cMatrix(); m1.AssignRandom(); m1.printMatrix(); System.out.println(""); m1.printMatrixWithPrime(); cMatrix m2 = new cMatrix(10,20); m2.AssignRandom(); cMatrix m3 = new cMatrix(20,10); m3.AssignRandom(); cMatrix m4 = new cMatrix(); m4 = m2.multiplyMatrices(m3); m4.printMatrixWithPrime(); So I can not make any changes here. He is going to imply those in my code. Jayakrishna🇮🇳
8th Nov 2021, 7:16 PM
Güneş Özkan
Güneş Özkan - avatar
0
//I hope this works...Güneş Özkan public long[][] multiplyMatrices(cMatrix Multiplicand){ long sum = 0; int row2 = row; int col2 = col; long[][] elements = new long[this.row][Multiplicand.col]; for (int i = 0; i < this.row; i++){ for (int j = 0; j < Multiplicand.col; j++){ for (int k = 0; k <this.col; k++){ sum = sum +( this.elements[i][k] * Multiplicand.elements[k][j] ); } elements[i][j] = sum; sum = 0; } } return elements; } /* And you need calling statements must be like : cMatrix m4 = new cMatrix(m2.row,m3.col); //cMatrix m4 = new cMatrix(); //also works in your case m4.elements = m2.multiplyMatrices(m3); m4.printMatrix(); also matrix possible only if (m2.col==m3.row) is true */
8th Nov 2021, 7:28 PM
Jayakrishna 🇮🇳
0
Thanks for the effort man I appreciate it but like I said, I can not change the calling statements. The calling statements that I used are directly copied from my teachers evaluation. I tried your code, it solved the error but output is changed. Now, its 4 digit numbers. Jayakrishna🇮🇳
8th Nov 2021, 7:37 PM
Güneş Özkan
Güneş Özkan - avatar
0
Without changes in call statements also works in your case. I think now, Output is depends on your random number generations.. Try with default values , if it correct then go for random values.
8th Nov 2021, 7:45 PM
Jayakrishna 🇮🇳