Close

Perkalian Matriks Dua Dimensi Dengan Struktur Perulangan Do While

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

mail me
mbarja@tass.telkomuniversity.ac.id
*/

import java.util.Random;

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

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

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

  }
}

Leave a Reply

%d bloggers like this: