Home » » membuat perkalian dan pembagian matriks menggunakan random pada java netbeans

membuat perkalian dan pembagian matriks menggunakan random pada java netbeans

selamat sore sahabat kreatif, kali ini saya akan memberikan codingan perkalian dan pembagian matriks menggunakan random pada java netbeans.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Praktikum_2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author MOHAMMAD RAMLI
 * NIM     1310651201
 */
public class matriks_perkalian_random {
       public static void main(String[] args) {
               
         BufferedReader matriks = new BufferedReader (new InputStreamReader(System.in)) ; 
         
  int barisA = 0;
  int kolomA = 0;
  int barisB = 0;
  int kolomB = 0;
         
  String a = "" ;
  String b = "" ;
  String c = "" ;
  String d = "" ;
  
  
try {          
    System.out.print("Masukkan Baris A =  ");
   a = matriks.readLine();

   System.out.print("Masukkan Kolom A =  ");
   b = matriks.readLine();
   
   System.out.print("Masukkan Baris B =  ");
   c = matriks.readLine();
   
   System.out.print("Masukkan Kolom B =  ");
   d = matriks.readLine();
   
   }catch(IOException e){
       
       
   System.out.println("Data Yang Anda Masukkan Bukan Angka");
   
   } 
   
  barisA  =     Integer.parseInt(a);
  kolomA  =     Integer.parseInt(b);
  barisB  =     Integer.parseInt(c);
  kolomB  =     Integer.parseInt(d);
         
        int[][] matriksA = new int[barisA][kolomA];
        int[][] matriksB = new int[barisB][kolomB];
        int[][] matriksC = new int[barisA][kolomB];

        for (int x = 0; x < barisA; x++) {
            for (int y = 0; y < kolomA; y++) {
                matriksA[x][y] = (int) (Math.random() * 10+1);
            }
        }
       for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomA; j++) {
                matriksA[i][j] = (int) (Math.random() * 10+1);
            }
        }
        for (int i = 0; i < barisB; i++) {
            for (int j = 0; j < kolomB; j++) {
                matriksB[i][j] = (int) (Math.random() * 10+1);
            }
        }
        for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomB; j++) {
                matriksC[i][j] = 0;
                for (int k = 0; k < barisB; k++) {
                    matriksC[i][j] += (matriksA[i][k] * matriksB[k][j]);
                }
            }
        }
           System.out.println("\n");
        System.out.println("Matriks A");
        for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomA; j++) {
                System.out.print(matriksA[i][j] + " ");
            }
            System.out.println();
        }
           System.out.println("\n");
        System.out.println("Matriks B");
        for (int i = 0; i < barisB; i++) {
            for (int j = 0; j < kolomB; j++) {
                System.out.print(matriksB[i][j] + " ");
            }

            System.out.println();
        }
           System.out.println("\n");
        System.out.println("Hasil perkalian A x B :");
        for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomB; j++) {
                System.out.print(matriksC[i][j] + " ");
            }    
            System.out.println();
    }             
       }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Praktikum_2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author MOHAMMAD RAMLI
 * NIM     1310651201
 */
public class matriks_pembagian_random {
       public static void main(String[] args) {
               
         BufferedReader matriks = new BufferedReader (new InputStreamReader(System.in)) ; 
         
  int barisA = 0;
  int kolomA = 0;
  int barisB = 0;
  int kolomB = 0;
         
  String a = "" ;
  String b = "" ;
  String c = "" ;
  String d = "" ;
  
  
try {          
    System.out.print("Masukkan Baris A =  ");
   a = matriks.readLine();

   System.out.print("Masukkan Kolom A =  ");
   b = matriks.readLine();
   
   System.out.print("Masukkan Baris B =  ");
   c = matriks.readLine();
   
   System.out.print("Masukkan Kolom B =  ");
   d = matriks.readLine();
   
   }catch(IOException e){
       
       
   System.out.println("Data Yang Anda Masukkan Bukan Angka");
   
   } 
   
  barisA  =     Integer.parseInt(a);
  kolomA  =     Integer.parseInt(b);
  barisB  =     Integer.parseInt(c);
  kolomB  =     Integer.parseInt(d);
         
        int[][] matriksA = new int[barisA][kolomA];
        int[][] matriksB = new int[barisB][kolomB];
        int[][] matriksC = new int[barisA][kolomB];

        for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomA; j++) {
                matriksA[i][j] = (int) (Math.random() * 10 + 1);
            }
        }
        for (int i = 0; i < barisB; i++) {
            for (int j = 0; j < kolomB; j++) {
                matriksB[i][j] = (int) (Math.random()*10 +1);
            }
        }
        for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomB; j++) {
                matriksC[i][j] = 0;
                for (int k = 0; k < barisB; k++) {
                    matriksC[i][j] -= (matriksA[i][k] / matriksB[k][j]);
                }
            }
        }
        System.out.println("Matriks A");
        for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomA; j++) {
                System.out.print(matriksA[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("Matriks B");
        for (int i = 0; i < barisB; i++) {
            for (int j = 0; j < kolomB; j++) {
                System.out.print(matriksB[i][j] + " ");
            }

            System.out.println();
        }
        System.out.println("Hasil Pembagian A : B :");
        for (int i = 0; i < barisA; i++) {
            for (int j = 0; j < kolomB; j++) {
                System.out.print(matriksC[i][j] + " ");
            }    
            System.out.println();
    }             
       }
}

    



0 Komentar Sahabat:

Post a Comment

Jadilah yang pertama...

Facebook

 
Support : Copyright © 2013. Mohammad Ramli - All Rights Reserved
Proudly powered by Blogger