Reputation: 1
Hi I am fairly new to Java coding so please excuse for any silly errors or questions. I got this code from some internet source which multiplies two matrices and gives the resultant one in Java. I have edited it for my own use. The code is as follow:
for ( c = 0 ; c < 3 ; c++ )
{
for ( d = 0 ; d < 1 ; d++ )
{
for ( int k = 0 ; k < 3 ; k++ )
{
Math.sum = sum + transformation[c][k]*sub[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
System.out.println("Product of entered matrices:-");
for ( c = 0 ; c < 3 ; c++ )
{
for ( d = 0 ; d < 1 ; d++ )
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
So now im getting red lines under sum and multiply saying 'sum cannot be resolved or is not a field' and 'multiply cannot be resolved to a variable'. Can anyone please explain the reason for the error and how it can be resolved. Thanks
Upvotes: 0
Views: 583
Reputation: 10695
public class Multiply {
public static void main(String[] args) {
int rows=3, columns=3;
double multiply[][] = new double[rows][columns]; // product of transformation X sub
double matA[][] = { { 2, 3, 6 }, { 1, 4, 6 }, { 4, 1, 3 } },
matB[][] = { { 2, 1, 0 }, { 3, 5, 1 }, { 3, 2, 1 } },
sum;
for (int k = 0; k < columns; k++) {
for (int c = 0; c < rows; c++) {
sum = 0;
for (int d = 0; d < columns; d++) {
sum = sum + matA[c][d] * matB[d][k];
}
multiply[c][k] = sum;
}
}
System.out.println("Product of Matrix A & B matrices:-");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
System.out.print(multiply[i][j] + "\t");
System.out.print("\n");
}
}
}
Upvotes: 1
Reputation: 31
I really is only that your taking the Math library sum method and setting it to a value; The method is supposed to return a value to you Sum = Math.sum(5,4)
Upvotes: 0
Reputation: 109
You need this:
int multiply[][] = new int[somesize][somesize] ;
for ( c = 0 ; c < 3 ; c++ )
{
for ( d = 0 ; d < 1 ; d++ )
{ int sum = 0; // Creating local variable 'sum'
for ( int k = 0 ; k < 3 ; k++ )
{
sum = sum + transformation[c][k]*sub[k][d];
}
multiply[c][d] = sum;
}
}
Upvotes: 0