hey guys can anybody help me with this its for java Netbeans
hey guys,, can anybody help me with this? its for java Netbeans...
Write a Java method to multiply two N times N matrices (2D arrays). The two matrices are received as parameters. The result should be returned from the method.Solution
import java.util.*;
public class allmain {
static int mf, nf, ps, qs, add= 0, c1, d1, k1;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print(\"rows and col of 1st matrix : \");
mf = in.nextInt();
nf = in.nextInt();
int first[][] = new int[mf][nf];
int second[][]=null;
int multiply[][]=null;
System.out.print(\"Enter data : \");
for(c1=0 ; c1<mf; c1++)
{
for(d1=0; d1<nf; d1++)
{
first[c1][d1] = in.nextInt();
}
}
System.out.print(\"Enter rows and cols of 2nd matrix : \");
ps = in.nextInt();
qs = in.nextInt();
if ( nf != ps )
{
System.out.print(\"same matrix size should not posible\");
}
else
{
second = new int[ps][qs];
multiply = new int[mf][qs];
System.out.print(\"Enter data\");
for(c1=0; c1<ps; c1++)
{
for(d1=0; d1<qs; d1++)
{
second[c1][d1] = in.nextInt();
}
}
}
matrixmull(first,second,multiply);
}
private static void matrixmull(int[][] f, int[][] s,
int[][] mult) {
for(c1=0; c1<mf; c1++)
{
for(d1=0; d1<qs; d1++)
{
for(k1=0; k1<ps; k1++)
{
add = add + f[c1][k1]*s[k1][d1];
}
mult[c1][d1] = add;
add= 0;
}
}
for(c1=0; c1<mf; c1++)
{
for(d1=0; d1<qs; d1++)
{
System.out.print(mult[c1][d1] + \"\\t\");
}
System.out.print(\"\ \");
}
}
}
output:
rows and col of 1st matrix : 2
3
Enter data : 1
2
3
4
3
1
Enter rows and cols of 2nd matrix : 3
4
Enter data1
2
3
1
2
3
3
4
3
2
4
5
14 14 21 24
13 19 25 21

