Thursday, October 22, 2015

Algoritma dan Pemograman Perkalian Matriks

Algoritma dan Pemrograman Perkalian Matriks

Nama Kelompok :
1. Ninda Fitria Pratiwi (D03112023)
2. Risa Vianita (D03112051)
3. Felicia Feliana Maharani Sibarani (D03112047)

Soal :
Buatlah algoritma program untuk menentukan perkalian matriks 2x2.

Jawaban :
a. Algoritma

Int A[2][2] , B[2][2] , C[2][2] , i, j , k, l;
for (int i = 0; i <=  1; i++) {
for (int j = 0; j <= 1; j++) {
System.out.print("[" + i + "][" + j + "]: ");
A[i][j] = input.nextInt(); //program disini merupakan inputan dari baris
}
}

for (int k = 0; k <= 1; k++) {
for (int l = 0; l <= 1; l++) {
System.out.print("[" + k + "][" + l + "]: ");
B[k][l] = input.nextInt(); //program disini merupakan inputan dari kolom
}
}

C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0];
C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1];
[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0];
C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1];
      
b. Source code dengan bahasa pemrograman  java
public class matrikstambah {
public static void main(String[] args) {
            System.out.println("Program Perkalian Matrix 2×2  dengan Matrix 2×2");
            Scanner input = new Scanner(System.in);///inputan dari keyboard
            int x[][] = new int[2][2];//x merupakan baris
            int y[][] = new int[2][2];//y merupakan kolom
            int z[][] = new int[2][2];//z merupakan hasil
            System.out.println("\nMasukkan nilai matrix 1 ordo 2×2: ");
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
                        System.out.print("[" + i + "][" + j + "]: ");
                        x[i][j] = input.nextInt();//program disini merupakan inputan dari baris
 }
        }
            System.out.println("\nMasukkan nilai matrix 2 ordo 2×2: ");
            for (int k = 0; k <= 1; k++) {
for (int l = 0; l <= 1; l++) {
                        System.out.print("[" + k + "][" + l + "]: ");
                        y[k][l] = input.nextInt();//program disini merupakan inputan dari kolom
}
}
//dibawah ini merupakan perakalian matriks dalam program
            z[0][0] = x[0][0] * y[0][0] + x[0][1] * y[1][0];
            z[0][1] = x[0][0] * y[0][1] + x[0][1] * y[1][1];
//        z[0][2] = x[0][0] * y[0][2] + x[0][1] * y[1][2];
            z[1][0] = x[1][0] * y[0][0] + x[1][1] * y[1][0];
            z[1][1] = x[1][0] * y[0][1] + x[1][1] * y[1][1];
//        z[1][2] = x[1][0] * y[0][2] + x[1][1] * y[1][2];
            System.out.println("\nMatrix 1 * Matrix 2= ");
            for (int m = 0; m <= 1; m++) {
for (int n = 0; n <= 1; n++) {
System.out.print(z[m][n] + "\t");
}
System.out.println("");
}
}
}                                                                                                                   

No comments:

Post a Comment