# strassens method error

hello, i need to finish this assignment but i dont understand whats wrong. it works for arrays of size 2x2 and 4x4 but not any larger and i cant figure it out. i just need some help in where to look for the problem

import java.io.*;

public class StrassensMatrixMult {

//get subArray from dimensions given
public static int[][] getSubArr(int[][]a ,int i0, int i1, int j0, int j1) {
int [][] subArray = new int[i1-i0+1][j1-j0+1];
for (int i = i0; i <= i1; i++) {
for (int j = j0; j <= j1; j++) {
subArray[i-i0][j-j0] = a[i][j];
}
}

return subArray;
}

public static int[][] addArrays(int n,int[][] a,int[][] b) {
for (int i = 0; i < n; i++) {
for (int y = 0; y <n; y++) {
}
}
}

//subtract two arrays
public static int[][] subArrays(int n,int[][] a,int[][] b) {
int[][] subA = new int[n][n];
for (int i = 0; i < 2; i++) {
for (int y = 0; y < 2; y++) {
subA[i][y] = a[i][y]-b[i][y];
}
}
return subA;
}

public static int[][] strassensMult(int n, int[][] a, int[][] b) {
int m;
int [][] C= new int[n][n];
int P1,P2,P3,P4,P5,P6,P7;
int [][] ap00,ap01,ap10,ap11,bp00,bp01,bp10,bp11;
int [][] nP1,nP2,nP3,nP4,nP5,nP6,nP7,C1,C2,C3,C4;

if (n==2)
{
P1=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
P2=(a[1][0]+a[1][1])*b[0][0];
P3=a[0][0]*(b[0][1]-b[1][1]);
P4=a[1][1]*(b[1][0]-b[0][0]);
P5=(a[0][0]+a[0][1])*b[1][1];
P6=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
P7=(a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

C[0][0]=P1+P4-P5+P7;
C[0][1]=P3+P5;
C[1][0]=P2+P4;
C[1][1]=P1+P3-P2+P6;

return (C);

}
else
{
m = n/2;

ap00=getSubArr(a,0,m-1,0,m-1);
ap01=getSubArr(a,0,m-1,m,n-1);
ap10=getSubArr(a,m,n-1,0,m-1);
ap11=getSubArr(a,m,n-1,m,n-1);

bp00=getSubArr(b,0,m-1,0,m-1);
bp01=getSubArr(b,0,m-1,m,n-1);
bp10=getSubArr(b,m,n-1,0,m-1);
bp11=getSubArr(b,m,n-1,m,n-1);

nP3 = strassensMult(m,ap00, subArrays(m,bp01,bp11));
nP4 = strassensMult(m,ap11, subArrays(m,bp10,bp00));

for (int i = 0; i < m; i++) {
for (int y = 0; y < m; y++) {

C[i][y]=C1[i][y];
C[i][y+m]=C2[i][y];
C[i+m][y]=C3[i][y];
C[i+m][y+m]=C4[i][y];

}

}
return C;
}

}

public static void printMatrix(int[][] c){

for (int i = 0; i < c.length; i++) {
for (int y = 0; y < c.length; y++) {
System.out.print(c[i][y]+" ");;
}
System.out.println("");
}

}

public static void main(String[] args) {
int[][]  a,finMat;

int matrixSize,hold;
String currLine;

if (args.length==0){System.out.println("Enter name of text file");}
else {
try {

a= new int[matrixSize][matrixSize];
for (int row=0;row<matrixSize;row++){
for (int col=0;col<matrixSize;col++){
}
}

finMat=strassensMult(matrixSize,a,a);
printMatrix(finMat);
}
}

}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
could u please post the contents of the file u r trying to read
0
Commented:
also this:
change it to this and post the results:

catch (Exception ex) {
ex.printStackTrace();
}
0
Author Commented:
8
5
7
7
2
9
1
6
8
9
9
4
2
6
2
6
7
7
2
5
5
3
2
1
0
8
8
6
8
4
4
5
2
5
9
8
8
7
1
8
6
9
0
1
0
1
2
0
7
3
0
4
8
0
5
6
0
5
6
0
7
1
7
7
7

(its the matrix in row major form with the first line showing the size of the matrix NxN)
0
Commented:
could u please also apply the my 2nd comment and post the result Error stack trace
0
Commented:
ok I solve it for u, update ur try{...}catch(){...} block to the following:

try {
matrixSize = 0;
try
{
}
catch(NumberFormatException ne)
{

}
a = new int[matrixSize][matrixSize];
for (int row = 0; row < matrixSize; row++) {
for (int col = 0; col < matrixSize; col++) {
try
{
}
catch(NumberFormatException ne)
{

}

}
}

finMat = strassensMult(matrixSize, a, a);
printMatrix(finMat);
} catch (Exception ex) {
ex.printStackTrace();
}
0
Commented:
offcourse replac:

with:

(ur version)

the reason for this if a line is empty in the line the process will exit with a NumberFormatException

0
Author Commented:
i dont think i was having a number format exception though, it just wasnt calculating the correct matrix...
0
Commented:
well, I tried it on my machine with the same file u have posted it throws number format exception because the last line was empty, when I catch the NumberFormatException the matrixes r printed, please give it a try
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.