Contoh Program Matrik Perkalian dan Pengurangan dengan Inputan dari User
Berikut adalah contoh program menggunakan JCreator
import java.util.Scanner;
public class matrikmaman
{
public static void main(String[] args)
{
Scanner matrik = new Scanner(System.in);
int a;
int b;
int matrikA [][] = new int[2][2];
int matrikB [][] = new int[2][2];
int matrikHasil [][] = new int[2][2];
System.out.println("INPUT MATRIK A");
for (a = 0; a < matrikA.length; a++)
{
for (b = 0; b < matrikA.length; b++)
{
System.out.print("Matrik A baris ke-" + (a + 1) + "kolom ke-" + (b + 1) + ": ");
matrikA[a][b] = matrik.nextInt();
}
}
System.out.println("INPUT MATRIK B");
for (a = 0; a < matrikB.length; a++)
{
for (b = 0; b < matrikB.length; b++)
{
System.out.print("Matrik B baris ke-" + (a + 1) + "kolom ke-" + (b + 1) + ": ");
matrikB[a][b] = matrik.nextInt();
}
}
System.out.println("MATRIK A ADALAH");
for (a = 0; a < matrikA.length; a++)
{
for (b = 0; b <matrikA.length; b++)
{
System.out.print(matrikA[a][b] + " ");
}
System.out.println();
}
System.out.println("MATRIK B ADALAH");
for (a = 0; a < matrikB.length; a++)
{
for (b = 0; b < matrikB.length; b++)
{
System.out.print(matrikB[a][b] + " ");
}
System.out.println();
}
for (a = 0; a < matrikHasil.length; a++)
{
for (b = 0; b < matrikHasil.length; b++)
{
matrikHasil[a][b] = matrikA[a][b] - matrikB[a][b];
}
}
System.out.println("PENGURANGAN MATRIK A DAN MATRIK B");
for (a = 0; a < matrikHasil.length; a++)
{
for (b = 0; b < matrikHasil.length; b++)
{
System.out.print(matrikHasil[a][b] + " ");
}
System.out.println();
}
for (a = 0; a <matrikHasil.length; a++)
{
for (b = 0; b < matrikHasil.length; b++)
{
matrikHasil[a][b] = matrikA[a][b] * matrikB[a][b];
}
}
System.out.println("PERKALIAN MATRIK A DAN MATRIK B");
for (a = 0; a < matrikHasil.length; a++)
{
for (b = 0; b < matrikHasil.length; b++)
{
System.out.print(matrikHasil[a][b] + " ");
}
System.out.println();
}
}
}
dan berikut adalah contoh flowchart dari pengurangan dan perkalian matriks, namun tidak sesuai dengan program JCreator diatas :)
0 komentar:
Posting Komentar