Close

Perkalian Matriks Dua Dimensi Dengan Struktur Perulangan For

import java.util.Random;




public class HasilKaliMatriks {

  public static void main (String[] args) {




     int baris_matriksA = 1, kolom_matriksA = 4;

     int baris_matriksB = 4, kolom_matriksB = 3;




     int[][] matriksA = new int [baris_matriksA][kolom_matriksA];

     int[][] matriksB = new int [baris_matriksB][kolom_matriksB];




     Random random = new Random();




     System.out.println (“inisialisasi Matriks A”);

     for (int i = 0; i < baris_matriksA; i++) {

       for (int j = 0; j < kolom_matriksA; j++) {

         matriksA [i][j] =  random.nextInt(100);

         System.out.print (matriksA[i][j] + “ ”);

       }

       System.out.print (‘\n’);

     }




     System.out.println (“inisialisasi Matriks B”);

     for (int i = 0; i < baris_matriksB; i++) {

       for (int j = 0; j < kolom_matriksB; j++) {

         matriksB [i][j] =  random.nextInt(100);

         System.out.print (matriksB[i][j] + “ ”);

       }

       System.out.print (‘\n’);

     }




     System.out.println (“Proses Perkalian Matriks A dan B”);

     int[][] matriksC = new int [baris_matriksA][kolom_matriksB];

     for (int i = 0; i < baris_matriksA; i++) {

       for (int j = 0; j < kolom_matriksB; j++) {

         matriksC [i][j] =  0;

         for (int k = 0; k < baris_matriksB; k++) {

           matriksC[i][j] = matriksC[i][j] + (matriksA[i][k] * matriksB[k][j]);

         }

         System.out.print (matriksC[i][j] + “ ”);

       }

       System.out.print (‘\n’);

     }




  }

}

Leave a Reply

%d bloggers like this: