Dalam matematika, matriks adalah kumpulan bilangan, simbol, atau ekspresi, berbentuk persegi panjang yang disusun menurut baris dan kolom. Bilangan-bilangan yang terdapat di suatu matriks yang menyususun matriks tersebut disebut elemen-elemen
atau komponen-komponen matriks. Nama sebuah matriks dinyatakan dengan
huruf kapital. Banyak baris x banyak suatu kolom dari suatu matriks
disebut ordo matriks Berikut merupakan program perkalian matriks [2][3] dan matriks [3][2] menggunakan bahasa java
package matriksjava;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Matriksjava {
public static void main(String[] args) {
String input;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try{
int x= 3, y=4;
int matrix_1[][] = new int[x][y];
int matrix_2[][] = new int[y][x];
int matrix_3[][] = new int[x][x];
System.out.println("Matrix ke 1 : [2]x[3]");
for(int i = 1; i < x; i++){
for(int j = 1; j < y; j++){
System.out.print("matrix 1 ["+i+"]["+j+"] : ");
input = reader.readLine();
matrix_1[i][j] = Integer.parseInt(input);
}
System.out.println("");
}
System.out.println("Matrix ke 2 : [3]x[2]");
for(int i = 1; i < y; i++){
for(int j = 1; j < x; j++){
System.out.print("matrix 2 ["+i+"]["+j+"] : ");
input = reader.readLine();
matrix_2[i][j] = Integer.parseInt(input);
}
System.out.println("");
}
for(int a = 1; a < x; a++){
for(int c = 1; c < x; c++){
for(int b = 1; b < y; b++){
matrix_3[a][c] = matrix_3[a][c] + (matrix_1[a][b] * matrix_2[b][c]);
}
}
}
for(int i = 1; i < x; i++){
for(int j = 1; j < x; j++){
System.out.print("["+matrix_3[i][j]+"]");
}
System.out.println("");
}
}catch(IOException e){
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Matriksjava {
public static void main(String[] args) {
String input;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try{
int x= 3, y=4;
int matrix_1[][] = new int[x][y];
int matrix_2[][] = new int[y][x];
int matrix_3[][] = new int[x][x];
System.out.println("Matrix ke 1 : [2]x[3]");
for(int i = 1; i < x; i++){
for(int j = 1; j < y; j++){
System.out.print("matrix 1 ["+i+"]["+j+"] : ");
input = reader.readLine();
matrix_1[i][j] = Integer.parseInt(input);
}
System.out.println("");
}
System.out.println("Matrix ke 2 : [3]x[2]");
for(int i = 1; i < y; i++){
for(int j = 1; j < x; j++){
System.out.print("matrix 2 ["+i+"]["+j+"] : ");
input = reader.readLine();
matrix_2[i][j] = Integer.parseInt(input);
}
System.out.println("");
}
for(int a = 1; a < x; a++){
for(int c = 1; c < x; c++){
for(int b = 1; b < y; b++){
matrix_3[a][c] = matrix_3[a][c] + (matrix_1[a][b] * matrix_2[b][c]);
}
}
}
for(int i = 1; i < x; i++){
for(int j = 1; j < x; j++){
System.out.print("["+matrix_3[i][j]+"]");
}
System.out.println("");
}
}catch(IOException e){
}
}
}
Setelah program diatas di run makan akan muncul output seperti berikut.
0 comments:
Post a Comment