Anda di halaman 1dari 18

BAB 7

ARRAY

PROGRAM 7.1
// * --------------------------------------------------- *
// * suhu.cpp *
// * *
// * Contoh program yang menggunkan array. *
// * --------------------------------------------------- *
#include <iostream.h>
#include <conio.h>
void main ()
{
float suhu[5]; // Array dengan 5 elemen bertipe float
clrscr () ; // Hapus layar

// Membaca data dari keyboard dan meletakkan ke array

cout << "Masukkan 5 buah data suhu" << endl;


for(int i= 0;i<5;i++)
{
cout <<i<< " : " ;
cin >> suhu[i];
}
// Menampilkan isi array ke layar

cout << "Data suhu yang anda masukkan : " << endl;
for ( int i = 0;i < 5;i++)
cout << suhu[i] << endl;
getche ();
}

HASIL:
PROGRAM 7.2
// * --------------------------------------------------- *
// * suhu2.cpp *
// * *
// * Contoh pemrosesan pada array, yaitu untuk *
// * menghitung nilai rata-rata suhu. *
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>
const int JUM_DATA = 5;

void main ()
{
float suhu[JUM_DATA]; // Array suhu
float total; // Untuk menampung total suhu

clrscr (); // Hapus layer

// Membaca data dari keyboard dan meletakkan ke array

cout << "Masukkan data suhu" << endl;


for (int i = 0;i < JUM_DATA; i++)
{

cout << i << " : ";


cin >> suhu[i];
}

// Menghitung nilai rata-rata

total = 0; // Mula-mula diisi dengan nol


for (int i = 0;i < JUM_DATA; i++)
total = suhu[i]; // Tambahkan isi suhu (i) ke total

cout << "Suhu rata-rata = " << total / JUM_DATA << endl;
getche ();
}
HASIL :

PROGRAM 7.3
// * --------------------------------------------------- *
// * jhari.cpp *
// * *
// * Contoh pemberian nilai awal terhadap array *
// * --------------------------------------------------- *
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

void main ()
{

// Pendefinisian array jum_hari dan


// pemberian nilai awal

int jum_hari[12]=
{
31,28,31,30,31,30,31,31,30,31,30,31
};

clrscr (); // Hapus layar

// Tampilkan isi jum_hari


for (int i = 0; i < 12;i++)
cout << "jum_hari[" <<i<< "] = "<< jum_hari[i]<<endl;
getche () ;
}
HASIL :

PROGRAM 7.4
// * --------------------------------------------------- *
// * initnol.cpp *
// * *
// * Contoh memberi nilai awal sama dengan nol *
// * terhadap seluruh elemen array *
// * --------------------------------------------------- *

#include<iostream.h>
#include<conio.h>

void main ()
{
int tmp[5] = {0}; // Beri nilai nol seluruhnya
clrscr(); // Hapus layer
for (int i = 0; i < 5;i++)
cout << "tmp["<< i << "] = " << tmp [i] << endl;
getche () ;
}
HASIL :

PROGRAM 7.5
// * --------------------------------------------------- *
// * lulus.cpp *
// * *
// * Contoh pemakaian array berdimensi dua *
// * untuk menampung data kelulusan *
// * dari tiga buah jurusan studi untuk *
// * 4 tahun *
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>

void main()
{
int data_lulus[3][4]; //Array berdimensi dua
int tahun,jurusan;

clrscr();

// Memberikan data ke elemen array data_lulus

data_lulus [0][0] = 35; // data TI - 1992


data_lulus [0][1] = 45; // data TI - 1993
data_lulus [0][2] = 90; // data TI - 1994
data_lulus [0][3] = 120; // data TI - 1995
data_lulus [1][0] = 100; // data TI - 1992
data_lulus [1][1] = 110; // data TI - 1993
data_lulus [1][2] = 70; // data TI - 1994
data_lulus [1][3] = 101; // data TI - 1995
data_lulus [2][0] = 10; // data TI - 1992
data_lulus [2][1] = 15; // data TI - 1993
data_lulus [2][2] = 21; // data TI - 1994
data_lulus [2][3] = 17; // data TI - 1995

// Proses untuk memperoleh informasi kelulusan

while (1)
{

cout << "Jurusan (0 = TI, 1 = MI, 2 = TK): ";


cin >> jurusan;

if ((jurusan ==0) || (jurusan ==1) || (jurusan ==2))

while (1)
{
cout << "Tahun (1992 - 1995): ";
cin >> tahun;

if ((tahun >= 1992) && (tahun <= 1995))

cout << "Jumlah yang lulus = " << endl;


cout << data_lulus[jurusan][tahun]<<endl;
}
}
}
getche ();

HASIL
PROGRAM 7.6
// * --------------------------------------------------- *
// * hurufa.cpp *
// * *
// * Contoh inisialisasi array berdimensi dua *
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>

void main()
{
// Pendefinisian array berdimensi dua
// dan pemberian nilai awal

int huruf_A[8][8] =
{
{ 0, 1, 1, 1, 1, 1, 0, 0 } ,
{ 0, 1, 0, 0, 0, 1, 0, 0 } ,
{ 0, 1, 0, 0, 0, 1, 0, 0 } ,
{ 1, 1, 1, 1, 1, 1, 1, 0 } ,
{ 1, 1, 0, 0, 0, 0, 1, 0 } ,
{ 1, 1, 0, 0, 0, 0, 1, 0 } ,
{ 1, 1, 0, 0, 0, 0, 1, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};

int i, j;

clrscr(); // Hapus layar

int (i = 0; i < 8; i++);


{
for int (j =0; j < 8; j++)
if int (j = 0; j < 8; j++)
cout << '\xDB';
else
cout << '\x20'; // Spasi
cout << endl; // Pindah baris
}
getche () ;
}
PROGRAM 7.7
// * --------------------------------------------------- *
// * hurufab.cpp *
// * *
// * Contoh inisialisasi array berdimensi tiga *
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>

void main()
{
// Pendefinisian array berdimensi tiga
// dan pemberian nilai awal

int huruf[8][26][26] =
{
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1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0, 0, 0 } ,
{ 0, 0, 0, 1, 1, 0, 0, 0 } ,
{ 0, 0, 0, 1, 1, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
{ { 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 1, 1, 1, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 1, 1, 1, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 1, 1, 1, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
{ { 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 1, 1, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0 }
},
};
int i, j, k;

clrscr (); // Hapus layer

for (int i=0;i <8;i++)


{
for (int j=0;j<26;j++)
{
for (int k = 0; k < 26; k++)
if (huruf[i][j][k]== 1)
cout << '\xDB';
else
cout << '\x20'; // Spasi
cout << endl; // Pindah Baris
}

cout << endl; // Baris kosong


}
getche ();
}

HASIL
PROGRAM 7.8
// * --------------------------------------------------- *
// * argarr1.cpp *
// * *
// * Contoh array berdimensi satu kedudukan sebagai *
// * argument fungsi
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

const int MAKS = 100;


void inisialisasi_data(int data[MAKS], int &jumlah);

void main()
{

int data_acak [MAKS]; // Array berdimensi satu


int jumlah;

clrscr (); // Hapus layer

inisialisasi_data(data_acak, jumlah);
//tampilan elemen-elemen array
cout<<"\n isi array: "<<endl;
for( int i=0;i<jumlah;i++)
cout<<data_acak[i]<<endl;
getche ();
}
// Definisi fungsi

void inisialisasi_data(int data[], int & jumlah)


{
while (1)
{
cout << "Berapa jumlah data yang ingin" << endl;
cout << "dibangkitkan secara acak (5-100)?";

cin >> jumlah;

if ( (jumlah >= 5) && (jumlah <= 100) )


break;
}
randomize(); // Menyetel pembangkit bilangan acak
for (int i = 0; i < jumlah; i++)
data[i]=random(100);

HASIL

PROGRAM 7.9

// * --------------------------------------------------- *
// * argarr2.cpp *
// * *
// * Contoh untuk melewatkan array berdimensi dua *
// * pada fungsi.
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

const int BARIS = 5;


const int KOLOM = 5;

void isi_matriks(float mat[][KOLOM], int &brs, int &kol);

void main()
{
float matriks[BARIS][KOLOM];
int jum_baris, jum_kolom;
int i, j;

clrscr(); // Hapus layer

isi_matriks(matriks,jum_baris,jum_kolom);

cout << "\n Matriks yang terbentuk : " << endl;


cout << setiosflags(ios::fixed);
for (i = 0; i < jum_baris; i++)
{
for (j = 0; j < jum_kolom; j++)
cout << setw(12) << setprecision(5) << matriks[i][j];
cout << endl;
}
getche ();

// Definisi fungsi

void isi_matriks(float mat[BARIS][KOLOM], int &brs, int &kol);

{
int i, j;

cout << "Pastikan jumlah baris dan kolom" << endl;


cout << "tidak melebihi 5" << endl;

cout << "Jumlah baris = ";


cin >> brs;
cout << "Jumlah kolom = ";
cin >> kol;
cout<<endl;

for ( i = 0; i < brs; i++)


for ( j = 0; j < kol; j++)
{
cout << "Elemen " <<i<< "," << j << " = ";
cin >> mat [i][j];
cout <<endl;
}
}
PROGRAM 7.10
// * --------------------------------------------------- *
// * bilmaks.cpp *
// * *
// * Memperoleh bilangan terbesar yang ada *
// * pada array *
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

void main()
{
clrscr (); // Hapus layer
const MAKS = 10;
int data [MAKS];
int maks;

// Memperoleh data secara acak

randomize ();
for (int i = 0; i < MAKS; i++)
data [i] = rand ();

// Menampilkan data dan mencari


// data terbesar

cout << "DATA terbentuk : " << endl;


cout << data[0] << endl;
maks = data [0]; // Isi dengan data pertama

for (int i = 1; i < MAKS; i++)


{

cout << data[i] << endl;

if (data[i] > maks)


maks = data [i] ;

}
cout <<"Data Terbesar="<<maks <<endl;
getche ();
}
HASIL

PROGRAM 7.11
// * --------------------------------------------------- *
// * caridata.cpp *
// * *
// * Contoh penanganan untuk mencari suatu data *
// * di dalam array *
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>

void main()
{

int i, x, ketemu;

clrscr (); // Hapus layar

int data[10] = {5, 100, 20, 31, 77, 88, 99, 20, 55, 1 };

cout << "Data yang Anda cari : ";


cin >> x;

ketemu = 0;
for (int i = 0 ; i < sizeof (data) / sizeof(int) ; i++)
{
if (data[i] == x)
{
ketemu = ! ketemu; // Ubah menjadi benar
break; // Keluar dari for
}
}

if (ketemu)
cout << "Data tersebut ada pada posisi ke-2" ;

else
cout << "Data tersebut tidak ada !"<<endl ;
getche ();
}

HASIL

PROGRAM 7.12
// * --------------------------------------------------- *
// * urutdata.cpp *
// * *
// * Contoh untuk mengurutkan data dengan *
// * menggunakan metoda bubble sort *
// * --------------------------------------------------- *

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

void main()
{
int i, j, tmp, jumdata;
clrscr (); // Hapus layar

int data [] = { 5, 100, 20, 31, 77, 88, 99, 20, 55, 1 };

jumdata = sizeof (data) / sizeof(int);

// Menampilkan data
cout << "Data semula : " << endl;
for (int i = 0; i < jumdata; i++)
cout << setw (4) << data [i];
cout << endl; // Pindah baris

// Mengurutkan data

for ( i = 0+i; i < jumdata; i++)


for (j = i + 1; j < jumdata; j++)
if (data[i] > data [j])
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}

// Menampilkan data

cout << "Data setelah diurutkan : " << endl;


for (i = 0; i < jumdata; i++)
cout << setw(4) << data[i];
cout << endl; // Pindah baris
getche();

HASIL

Anda mungkin juga menyukai