Contoh coding Array 2 dimensi (Penjumlahan matrik)

import javax.swing.JOptionPane;
public class Contoh1 {
public static void main(String[] args) {
int baris = 0;
int kolom = 0;
baris = Integer.parseInt
(JOptionPane.showInputDialog(“baris:”)
);
kolom = Integer.parseInt
(JOptionPane.showInputDialog
(“ kolom:”));
int[][] matriksA = new int[baris][kolom];
int[][] matriksB = new int[baris][kolom];
int[][] matriksC = new int[baris][kolom];
for(int x=0; x for(int y=0; y matriksA[x][y] = (int)(Math.random()*100)
;
matriksB[x][y] = (int)(Math.random()*100)
;
matriksC[x][y] = matriksA[x][y] + matriksB
[x][y];
}
}
System.out.println(“matriks A”);
System.out.println(“———”);
for(int x=0; x for(int y=0; y System.out.print(matriksA[x][y] + “\t”);
}
System.out.println();
}
System.out.println();
System.out.println (“matriks B”);
System.out.println(“———”);
for(int x=0; x for(int y=0; y System.out.print(matriksB[x][y] + “\t”);
}
System.out.println(“”);
}
System.out.println();
System.out.println (“matriks A + B”);
System.out.println(“————-”);
for(int x=0; x for(int y=0; y System.out.print(matriksC[x][y] + “\t”);
}
System.out.println();
}
}
}

0 komentar:

Posting Komentar