#include <iostream>
#include <conio.h>
using namespace std;
int main(){
const int Ar[7] = {2,4,6,8,9,14,30};
int tar;
cout<<"masukan data yang dicari : ";
cin>>tar;
int awal=0, akhir=7, tengah;
while (awal <= akhir)
{ tengah = (awal + akhir)/2;
if (tar > Ar[tengah] )
{ awal = tengah + 1; }
else if (tar < Ar[tengah])
{akhir= tengah - 1;}
else {awal = akhir +1;
}
}
if (tar == Ar[tengah])
{cout<<" Index, Ke- "<<tengah+1<<endl;
}
else {
cout<<"target tidak ditemukan "<<endl;
}
getch();
}
Tidak ada komentar:
Posting Komentar