Close

Perkalian Matriks Dua Dimensi Dengan Struktur Perulangan While

/*
dibuat oleh Muhammad Barja Sanjaya
tanggal Sabtu, 1 Desember 2018

mail me
mbarja@tass.telkomuniversity.ac.id
*/
import java.util.Random;

public class WhileHasilKaliMatriks {
  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");
    int i = 0, j;
    while (i < baris_matriksA) {
      j = 0;
      while (j < kolom_matriksA) {
        matriksA[i][j] = random.nextInt(100);
        System.out.print(matriksA[i][j] + " ");
        j++;
      }
    System.out.println();
    i++;
    }

    System.out.println("inisialisasi Matriks B");
    i = 0;
    while (i < baris_matriksB) {
      j = 0;
      while (j < kolom_matriksB) {
        matriksB[i][j] = random.nextInt(100);
        System.out.print(matriksB[i][j] + " ");
        j++;
      }
      System.out.println();
      i++;
    }

    System.out.println("Proses Perkalian Matriks A dan B");
    int[][] matriksC = new int[baris_matriksA][kolom_matriksB];
    int k;
    i = 0;
    while (i < baris_matriksA) {
      j = 0;
      while (j < kolom_matriksB) {
        matriksC[i][j] = 0;
        k = 0;
        while (k < baris_matriksB) {
          matriksC[i][j] = matriksC[i][j] + (matriksA[i][k] * matriksB[k][j]);
          k++;
        }
        System.out.print(matriksC[i][j] + " ");
        j++;
      }
      System.out.print('\n');
      i++;
    }

  }
}

Leave a Reply

%d bloggers like this: