You are on page 1of 5

Searching

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
const int x = 10;
int bil[x] = {6,7,7,11,42,55,64,72,86,99};
int main();
void head(), menu(), menuSequens(int i), menuBin(int i);
void tampil(int t[]);
void sequence (int c[], int key);
void binary(int c[], int key);
void ulang(int a);

int main()
{
int f,g;
head();
getch();
return 0;
}
void head()
{
char c[1];
cout << endl<< endl;
cout << " SSSSSSSS EEEEEEEE LL AAAAAAAA MM MM AAAAAAA
A TTTTTTTT" << endl;
cout << " SS EE LL AA AA MMMM MMMM AA A
A TT" << endl;
cout << " SS EE LL AA AA MM MM MM MM AA A
A TT" << endl;
cout << " SSSSSSS EEEEEEEE LL AAAAAAAA MM MMM MM AAAAAAA
A TT" << endl;
cout << " SS EE LL AA AA MM MM AA A
A TT" << endl;
cout << " SS EE LL AA AA MM MM AA A
A TT" << endl;
cout << " SSSSSSSS EEEEEEEE LLLLLLLL AA AA MM MM AA A
A TT" << endl;
cout << endl<< endl;
cout << " DDDDDDD AAAAAAAA TTTTTTTT AAAAAAAA NNNN NN GGGGGGG
G !!" <<endl;
cout << " DD DD AA AA TT AA AA NN NN NN GG
!!"<<endl;
cout << " DD DD AA AA TT AA AA NN NN NN GG
!!"<<endl;
cout << " DD DD AAAAAAAA TT AAAAAAAA NN NN NN GG GGGG
G !!"<<endl;
cout << " DD DD AA AA TT AA AA NN NN NN GG G
G !!"<<endl;
cout << " DD DD AA AA TT AA AA NN NN NN GG G
G 00"<<endl;
cout << " DDDDDDD AA AA TT AA AA NN NNNN GGGGGGG
G 00"<<endl;
cout << endl<< endl;
cout << "Ketik Apa Saja Untuk Melanjutkan ! ";
cin >> c;
if(c){
menu();
}
}
void menu()
{
char pilih;
home:
system("cls");
cout << endl;
cout <<"#################################"<<endl;
cout <<"### Nama : Agus Sunarko ###"<<endl;
cout <<"### NIM : 20160801133 ###"<<endl;
cout <<"#################################"<<endl;
cout <<endl;
cout <<" Program Searching "<<endl;
cout <<"*********************************"<<endl;
cout << endl << endl;
cout <<"Menu Program yang Tersedia : "<<endl;
cout <<"1. Sequential Search"<<endl;
cout <<"2. Binary Search" << endl;
cout << endl << endl;
cout <<"Masukkan Pilihan Anda : ";
cin >> pilih;
if (pilih=='1'){
system("cls");
menuSequens(1);
} else if (pilih=='2'){
system("cls");
menuBin(2);
} else
{
cout << "Pilihan Anda Tidak Tersedia";
goto home;
}
}
void menuSequens(int i)
{
int f;
system("cls");
cout << endl;
cout <<"#################################"<<endl;
cout <<"### Nama : Agus Sunarko ###"<<endl;
cout <<"### NIM : 20160801133 ###"<<endl;
cout <<"#################################"<<endl;
cout <<endl;
cout <<" Sequential Search "<<endl;
cout <<"*********************************"<<endl;
cout << endl << endl;
if(i==1)
{
tampil(bil);
cout << "Masukkan bilangan yang ingin dicari -> ";
cin >> f;
cout << endl;
sequence(bil,f);
ulang(1);
}
}
void menuBin(int i)
{
int f;
system("cls");
cout << endl;
cout <<"#################################"<<endl;
cout <<"### Nama : Agus Sunarko ###"<<endl;
cout <<"### NIM : 20160801133 ###"<<endl;
cout <<"#################################"<<endl;
cout <<endl;
cout <<" Binnary Search "<<endl;
cout <<"*********************************"<<endl;
cout << endl << endl;
if(i==2)
{
tampil(bil);
cout << "Masukkan bilangan yang ingin dicari -> ";
cin >> f;
cout << endl;
binary(bil,f);
ulang(2);
}
}
void tampil(int t[])
{
cout << "Bilangan Yang Tersedia = ";
for(int i=0;i<x;i++)
cout << setw(3) << t[i]<<" ";
cout << endl;
}
void sequence (int c[], int key)
{
int index;
bool ketemu = false;
for(int k=0;k<x;k++)
{
if(c[k]==key)
{
index = k;
ketemu = true;
break;
}
}
if(ketemu)
cout << "Bilangan " << key << " ditemukan pada data ke -> " << i
ndex+1 << endl;
else
cout << "Tidak Ada";
}
void binary(int c[], int key)
{
int awal = 0, akhir = x, tengah,index;
bool flag = false;
while(!flag && awal <= akhir)
{
tengah = (awal+akhir)/2;
if(c[tengah] == key)
{
index = tengah;
flag = true;
break;
}
else if(c[tengah] < key)
awal = tengah+1;
else
akhir = tengah-1;
}
if(flag)
cout << "Bilangan " << key << " ditemukan pada data ke -> " <<
index+1 << endl;
else
cout << "Tidak Ada";

}
void ulang(int a)
{
int b=3;
char pilih;
cout << endl << endl;
cout <<"*********************************"<<endl;
cout <<"Menu Home Pilih 0";
cout <<"\nAnda mau mencoba kembali [Y/N] ?"<<endl;
cout << "Pilihan Anda : ";
cin >> pilih;
if (pilih=='0')
{
system("cls");
main();
} else if (pilih=='Y' || pilih=='y')
{
system("cls");
if(a==1)
menuSequens(1);
else if(a==2)
menuBin(2);
getch();
} else if (pilih=='N' || pilih=='n' || pilih=='T' || pilih=='t')
{
cout << endl;
cout <<"#################################"<<endl;
cout <<"### TERIMA KASIH ###"<<endl;
cout <<"### @ahlanlans ###"<<endl;
cout <<"#################################"<<endl;
}
else
{
cout <<"Pilihan Anda Tidak Tersedia"<<endl;
ulang (a);
}
}

You might also like