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
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
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 ()
{
int jum_hari[12]=
{
31,28,31,30,31,30,31,31,30,31,30,31
};
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();
while (1)
{
while (1)
{
cout << "Tahun (1992 - 1995): ";
cin >> tahun;
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;
#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, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 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 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;
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>
void main()
{
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
HASIL
PROGRAM 7.9
// * --------------------------------------------------- *
// * argarr2.cpp *
// * *
// * Contoh untuk melewatkan array berdimensi dua *
// * pada fungsi.
// * --------------------------------------------------- *
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
float matriks[BARIS][KOLOM];
int jum_baris, jum_kolom;
int i, j;
isi_matriks(matriks,jum_baris,jum_kolom);
// Definisi fungsi
{
int i, j;
#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;
randomize ();
for (int i = 0; i < MAKS; i++)
data [i] = rand ();
}
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;
int data[10] = {5, 100, 20, 31, 77, 88, 99, 20, 55, 1 };
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 };
// Menampilkan data
cout << "Data semula : " << endl;
for (int i = 0; i < jumdata; i++)
cout << setw (4) << data [i];
cout << endl; // Pindah baris
// Mengurutkan data
// Menampilkan data
HASIL