Array Multdimensi

· OOP1
Penulis

//variable
byte jmlBaris = 0;
byte jmlKolom = 0;

//Deklarasi array
byte[,] matrixA;

//input jml baris dan kolom
Console.Write(“jumlah elemen baris = “);
jmlBaris = Convert.ToByte(Console.ReadLine());
Console.Write(“jumlah elemen kolom = “);
jmlKolom = Convert.ToByte(Console.ReadLine());

//inisialisasi array
matrixA = new byte[jmlBaris, jmlKolom];

//mengisi array
for (byte baris = 0; baris < jmlBaris; baris++)
{
for (byte kolom = 0; kolom < jmlKolom; kolom++)
{
//isi array per elemen
Console.Write(“matrixA[{0},{1}]) = “, baris.ToString(), kolom.ToString());
matrixA[baris, kolom] = Convert.ToByte(Console.ReadLine());
}
}
//tampilkan elemen array
Console.WriteLine(“menampilkan isi array”);
for (byte baris = 0; baris < jmlBaris; baris++)
{
for (byte kolom = 0; kolom < jmlKolom; kolom++)
{
Console.WriteLine(“isi elemen ke [{0},{1}] = {2}”, baris, kolom, matrixA[baris, kolom]);
}
}

Console.ReadLine();

Image

Program C++
#include <iostream>
using namespace std;
class arrayMulti
{
private: int Baris;
private: int Kolom;
public: void isiarray()
{
cout<<“Banyak Baris = “;
cin>>Baris;
cout<<“Banyak Kolom = “;
cin>>Kolom;
int array[Baris][Kolom];
for(int i=0;i<Baris;i++)
{
for(int j=0;j<Kolom;j++)
{
cout<<“Masukkan Data Array:”;
cin>>array[i][j];
}
}
for(int i=0;i<Baris;i++)
{
for (int j = 0; j <Kolom; j++)
{
cout<<array[i][j]<< ” “;
}
cout<<endl;
}
}
};
int main()
{
arrayMulti a;
a.isiarray();
return 0;
}

Hasilnya:

Program Java;

package arraymulti;
import java.util.Scanner;
class arraymulti
{
private int [][] arr;
private int Baris;
private int Kolom;
public void isiarray()
{
Scanner masuk = new Scanner(System.in);
System.out.print(“Banyak Baris = “);
Baris = masuk.nextInt();
System.out.print(“Banyak Kolom = “);
Kolom = masuk.nextInt();
arr = new int[Baris][Kolom];
for(int i=0;i<Baris;i++)
{
for(int j=0;j<Kolom;j++)
{
System.out.print(“Masukkan Data Array:”);
arr[i][j]=masuk.nextInt();
}
}
}
public void tampil()
{
for(int i=0;i<Baris;i++)
{
for (int j = 0; j <Kolom; j++)
{
System.out.print( arr[i][j]+ ” “);
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
arraymulti a=new arraymulti();
a.isiarray();
a.tampil();
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: