Monday, April 28, 2014

Perkalian Matriks Dengan Inputan BufferredReader

package TugasFadli;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class perkalianMR {
    public static void main(String[] args) {
        try {
            int barA = 0, barB = 0, kolA = 0, kolB = 0;
            BufferedReader ak = new BufferedReader(
                    new InputStreamReader(System.in));
            System.out.println("<< PERKALIAN MATRIK >>");
            System.out.print("Masukan \nBaris matrik A : ");
            barA = Integer.parseInt(ak.readLine());
            System.out.print("Kolom matrik A : ");
            kolA = Integer.parseInt(ak.readLine());
            barB=kolA;
            System.out.print("Kolom matrik B : ");
            kolB = Integer.parseInt(ak.readLine());

            int matA[][] = new int[barA][kolA];
            int matB[][] = new int[barB][kolB];
            int matC[][] = new int[barA][kolB];
            for (int i = 0; i < barA; i++) {
                for (int j = 0; j < kolA; j++) {
                    matA[i][j] = (int) (Math.random() * 10);
                }
            }
            System.out.println("Matrik A");
            for (int i = 0; i < barA; i++) {
                for (int j = 0; j < kolA; j++) {
                    System.out.print(matA[i][j] + "\t");
                }
                System.out.println();
            }
            for (int i = 0; i < barB; i++) {
                for (int j = 0; j < kolB; j++) {
                    matB[i][j] = (int) (Math.random() * 10);
                }
            }
            System.out.println("Batrik B");
            for (int i = 0; i < kolB; i++) {
                for (int j = 0; j < kolB; j++) {
                    System.out.print(matB[i][j] + "\t");
                }
                System.out.println();
            }
            for (int i = 0; i < barA; i++) {
                for (int j = 0; j < kolB; j++) {
                    matC[i][j] = 0;
            
                   for (int k = 0; k < barB; k++) {
                        matC[i][j] += (matA[i][k] * matB[k][j]);
                      
                    }
                   
                }
            }
            System.out.println("Matrik A * B :");
            for (int i = 0; i < barA; i++) {
                for (int j = 0; j < kolB; j++) {
                    System.out.print(matC[i][j] + "\t");
                }
                System.out.println();
            }
        } catch (Exception e) {
            System.out.println("\nData yang di masukan salah !");
        }
    }
}