Im trying to write a code that multiplies and adds two matri
I\'m trying to write a code that multiplies and adds two matrices together. The mutiplication is working fine, but the addition part is not working and giving an incorrect output. I\'m not sure how to fix it.
package proj5;
import java.util.Scanner;
public class Proj5 {
public static void main(String[] args) {
// multiplies two matrices together
int m, n, p, q, mult = 0, c, d, k;
Scanner key = new Scanner(System.in);
System.out.println(\"Enter number of rows and columns of first matrix\");
m = key.nextInt();
n = key.nextInt();
int first [][] = new int [m][n];
int add [][] = new int [m][n];
int sum [][] = new int [m][n];
System.out.println(\"Enter the elements of the first matrix\");
for (c=0; c<m; c++)
for (d=0; d<n; d++)
first[c][d] = key.nextInt();
System.out.println(\"Enter the numbers of rows and columns of the second matrix\");
p = key.nextInt();
q = key.nextInt();
if(n != p)
System.out.println(\"Matrices can not be multiplied\");
else{
int second[][] = new int [p][q];
int multiply[][] = new int [m][q];
System.out.println(\"Enter the elements of second matrix\");
for (c=0; c<m; c++)
for (d=0; d<q; d++)
second[c][d] = key.nextInt();
for (c=0; c<m; c++)
{
for (d=0; d<q; d++)
{
for (k=0; k<p; k++)
{
mult = mult + first[c][k]*second[k][d];
}
multiply[c][d] = mult;
mult = 0;
}
}
System.out.println(\"Product of entered matrices:\");
for (c=0; c<m; c++)
{
for (d=0; d<q; d++)
System.out.print(multiply[c][d] + \"\\t\");
System.out.print(\"\ \");
}
for (c=0; c<m; c++)
for (d=0; d<q; d++)
sum[d][d] = first[c][d]+add[c][d];
System.out.println(\"Sum of entered matrices:\");
for (c=0; c<m; c++)
{
for (d=0; d<q; d++)
System.out.print(sum[c][d]+ \"\\t\");
System.out.println();
}
}
}
}
Solution
package proj5;
import java.util.Scanner;
public class Proj5 {
public static void main(String[] args) {
// multiplies two matrices together
int m, n, p, q, mult = 0, c, d, k;
Scanner key = new Scanner(System.in);
System.out.println(\"Enter number of rows and columns of first matrix\");
m = key.nextInt();
n = key.nextInt();
int first [][] = new int [m][n];
int add [][] = new int [m][n];
int sum [][] = new int [m][n];
System.out.println(\"Enter the elements of the first matrix\");
for (c=0; c<m; c++)
for (d=0; d<n; d++)
first[c][d] = key.nextInt();
System.out.println(\"Enter the numbers of rows and columns of the second matrix\");
p = key.nextInt();
q = key.nextInt();
if(n != p)
System.out.println(\"Matrices can not be multiplied\");
else{
int second[][] = new int [p][q];
int multiply[][] = new int [m][q];
System.out.println(\"Enter the elements of second matrix\");
for (c=0; c<m; c++)
for (d=0; d<q; d++)
second[c][d] = key.nextInt();
for (c=0; c<m; c++)
{
for (d=0; d<q; d++)
{
for (k=0; k<p; k++)
{
mult = mult + first[c][k]*second[k][d];
}
multiply[c][d] = mult;
mult = 0;
}
}
System.out.println(\"Product of entered matrices:\");
for (c=0; c<m; c++)
{
for (d=0; d<q; d++)
System.out.print(multiply[c][d] + \"\\t\");
System.out.print(\"\ \");
}
for (c=0; c<m; c++)
for (d=0; d<q; d++)
sum[c][d] = first[c][d]+second[c][d]; //changesmade here,sum[d][d] changed to sum[c][d] and add[c] //[d] replaced by second[c][d].
System.out.println(\"Sum of entered matrices:\");
for (c=0; c<m; c++)
{
for (d=0; d<q; d++)
System.out.print(sum[c][d]+ \"\\t\");
System.out.println();
}
}
}
}
********OUTPUT*****
Enter number of rows and columns of first matrix
2
2
Enter the elements of the first matrix
1
2
3
4
Enter the numbers of rows and columns of the second matrix
2
2
Enter the elements of second matrix
1
2
3
4
Product of entered matrices:
7 10
15 22
Sum of entered matrices:
2 4
6 8
*********OUTPUT********
Please let me know in case of any doubt,Thanks.



