public class perkalianMatrik {
void cetakmatrik(int[][]data){
for(int i=0;i
for(int j=0;j
System.out.print(data[i][j]+" ");
}
System.out.println();
}
}
int[][]perkalianmatrik(int[][]data1,int[][]data2){
int[][]data3=new int[data1.length][data1[0].length];
int kolomdata1=data1[0].length;
int barisdata2=data2.length;
if(kolomdata1==barisdata2){
for(int i=0;i
for(int j=0;j
data3[i][j]=0;
for(int k=0;k
data3[i][j]+=(data1[i][k]*data2[k][j]);
}
}
}
}else{
System.out.println("Ukuran matrik tidak sesuai");
}
return data3;
}
public static void main(String[]args){
perkalianMatrik pm=new perkalianMatrik();
int[][]a={{1,1},{2,2}};
int[][]b={{2,2},{1,1}};
System.out.println("Matrik A : ");
pm.cetakmatrik(a);
System.out.println("Matrik B : ");
pm.cetakmatrik(b);
System.out.println("Matrik A * Matrik B");
pm.cetakmatrik(pm.perkalianmatrik(a, b));
}
}
output :
Matrik A :
1 1
2 2
Matrik B :
2 2
1 1
Matrik A * Matrik B
3 3
6 6
0 Komentar
Penulisan markup di komentar