- Joined
- Aug 21, 2022
- Messages
- 1
- Reaction score
- 0
Hi there,
I wrote this program but it is not working. Can anyone please fix this. It's function is to accept two matrices from the user and then display the sum matrix.
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Rows and Columns of First Matrix");
int row1 = sc.nextInt();
int col1 = sc.nextInt();
int A[][] = new int[row1][col1];
System.out.println("Enter Number of Rows and Columns of Second Matrix");
int row2 = sc.nextInt();
int col2 = sc.nextInt();
int B[][] = new int[row2][col2];
System.out.println("Enter Elements of First Matrix");
for (int i = 0; i <= row1 - 1; i++) {
for (int j = 0; j <= col1 - 1; j++) {
A[j] = sc.nextInt();
}
}
System.out.println("Enter Elements of Second Matrix");
for (int k = 0; k <= row2 - 1; k++) {
for (int l = 0; l <= col2 - 1; l++) {
A[k][l] = sc.nextInt();
}
}
System.out.println("The first Matrix is: ");
for (int w = 0; w <= row1 - 1; w++) {
for (int x = 0; x <= col1 - 1; x++) {
System.out.print(A[w][x] + " ");
}
System.out.println("");
}
System.out.println("The Second Matrix is: ");
for (int y = 0; y <= row2 - 1; y++) {
for (int z = 0; z <= col2 - 1; z++) {
System.out.print(A[y][z] + " ");
}
System.out.println("");
}
int C[][];
int row3 = 0;
int col3 = 0;
if (row1 > row2) {
row3 = row1;
} else {
row3 = row2;
}
if (col1 > col2) {
col3 = col1;
} else {
col3 = col2;
}
C = new int[row3][col3];
System.out.println("The Sum Matrix is");
for (int g = 0; g <= row3 - 1; g++) {
for (int h = 0; h <= col3 - 1; h++) {
C[g][h] = A[g][h] + B[g][h];
System.out.print(C[g][h] + " ");
}
System.out.println("");
}
System.out.println("The sum of two matrices is: ");
for (int s = 0; s <= row3 - 1; s++) {
for (int t = 0; t <= col3 - 1; t++) {
System.out.print(C[t] + " ");
}
System.out.println("");
I wrote this program but it is not working. Can anyone please fix this. It's function is to accept two matrices from the user and then display the sum matrix.
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Rows and Columns of First Matrix");
int row1 = sc.nextInt();
int col1 = sc.nextInt();
int A[][] = new int[row1][col1];
System.out.println("Enter Number of Rows and Columns of Second Matrix");
int row2 = sc.nextInt();
int col2 = sc.nextInt();
int B[][] = new int[row2][col2];
System.out.println("Enter Elements of First Matrix");
for (int i = 0; i <= row1 - 1; i++) {
for (int j = 0; j <= col1 - 1; j++) {
A[j] = sc.nextInt();
}
}
System.out.println("Enter Elements of Second Matrix");
for (int k = 0; k <= row2 - 1; k++) {
for (int l = 0; l <= col2 - 1; l++) {
A[k][l] = sc.nextInt();
}
}
System.out.println("The first Matrix is: ");
for (int w = 0; w <= row1 - 1; w++) {
for (int x = 0; x <= col1 - 1; x++) {
System.out.print(A[w][x] + " ");
}
System.out.println("");
}
System.out.println("The Second Matrix is: ");
for (int y = 0; y <= row2 - 1; y++) {
for (int z = 0; z <= col2 - 1; z++) {
System.out.print(A[y][z] + " ");
}
System.out.println("");
}
int C[][];
int row3 = 0;
int col3 = 0;
if (row1 > row2) {
row3 = row1;
} else {
row3 = row2;
}
if (col1 > col2) {
col3 = col1;
} else {
col3 = col2;
}
C = new int[row3][col3];
System.out.println("The Sum Matrix is");
for (int g = 0; g <= row3 - 1; g++) {
for (int h = 0; h <= col3 - 1; h++) {
C[g][h] = A[g][h] + B[g][h];
System.out.print(C[g][h] + " ");
}
System.out.println("");
}
System.out.println("The sum of two matrices is: ");
for (int s = 0; s <= row3 - 1; s++) {
for (int t = 0; t <= col3 - 1; t++) {
System.out.print(C
}
System.out.println("");