英文:
Getting exception error when trying matrix multiplication
问题
以下是您要翻译的内容:
"Code works fine when both matrices have same number of rows and columns. like for eg. 3x3 and 3x3 matrix multiplication works fine.
Getting below error when trying to multiply matrices with different sizes. Tried multiplying a 2x3 matrix and a 3x4 matrix and got error."
英文:
Code works fine when both matrices have same number of rows and columns. like for eg. 3x3 and 3x3 matrix multiplication works fine.
Getting below error when trying to multiply matrices with different sizes. Tried multiplying a 2x3 matrix and a 3x4 matrix and got error.
**Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at arrayprac.ArrayPrac.main(ArrayPrac.java:85)
38C:\Users\a807771\AppData\Local\NetBeans\Cache\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\a807771\AppData\Local\NetBeans\Cache\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 12 seconds)**
package arrayprac;
import java.util.Scanner;
public class ArrayPrac {
public static void main(String[] args) {
System.out.println("Enter number of rows for matrix 1: ");
int row1,col1,row2,col2;
Scanner sc=new Scanner(System.in);
row1=sc.nextInt();
System.out.println("Enter number of Columns for matrix 1: ");
col1=sc.nextInt();
System.out.println("Enter number of rows for matrix 2: ");
row2=sc.nextInt();
System.out.println("Enter number of Columns for matrix 2: ");
col2=sc.nextInt();
int a[][]=new int[row1][col1];
int b[][]=new int[row2][col2];
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
System.out.println("Enter element for 1st matrix: ");
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
System.out.println("Enter element for 2nd matrix: ");
b[i][j]=sc.nextInt();
}
}
System.out.println("Below is the 1st matrix: \n");
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
System.out.print(a[i][j]);
}
System.out.println("");
}
System.out.println("Below is the 2nd matrix: \n");
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
System.out.print(b[i][j]);
}
System.out.println("");
}
if(col1!=row2)
{
System.out.println("Matrix multiplication impossible!");
System.exit(0);
}
else{
int c[][]=new int[row1][col2];
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
{
c[i][j]=0;
for(int k=0;k<row2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("Below is the result matrix: \n");
for(int i=0;i<col1;i++)
{
for(int j=0;j<row2;j++)
{
System.out.print(c[i][j]);
}
System.out.println("");
}
}
}
}
Code works fine when both matrices have same number of rows and columns. like for eg. 3x3 and 3x3 matrix multiplication works fine.
Getting error when trying to multiply matrices with different sizes. Tried multiplying a 2x3 matrix and a 3x4 matrix and got error.
答案1
得分: 1
The iteration bounds in the double for loop for printing the new matrix should match the previous for loops for multiplication. I think this correction in your code will make it work.
for(int i=0;i<row1;i++)//Here
{
for(int j=0;j<col2;j++)//And here
{
System.out.print(c[i][j]);
}
System.out.println("");
}
Generally speaking, in situations like this it will help if you set breakpoints in the lines where the problem is and then using the debugger.
英文:
The iteration bounds in the double for loop for printing the new matrix should match the previous for loops for multiplication. I think this correction in your code will make it work.
for(int i=0;i<row1;i++)//Here
{
for(int j=0;j<col2;j++)//And here
{
System.out.print(c[i][j]);
}
System.out.println("");
}
Generally speaking, in situations like this it will help if you set breakpoints in the lines where the problem is and then using the debugger.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论