Program Mencari posisi Index Input Dengan C++

#include<iostream>
using namespace std;

int main () {
int n, angka[12], kiri, kanan, tengah, temp, key;
bool ketemu = false;

cout << “Masukan jumlah data? “;
cin >> n;

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

cout << “Angka ke – [” << i << “] : “;
cin >> angka[i];
}

for (int i=0; i<n; i++)
{
for(int j=0; j< n-1; j++)
{

if(angka [j] > angka [j+1])
{
temp = angka[j];
angka[j] = angka[j+1];
angka[j+1] = temp;
}
}
}
cout << “=========================================================================\n”;
cout << “Data yang telah diurutkan adalah:\n”;
for(int i=0; i<n; i++)
{
cout << angka[i] << ” “;
}
cout << “\n=========================================================================\n”;
cout << “Masukan angka yang dicari: “;
cin >> key;

kiri = 0;
kanan = n-1;
while(kiri<=kanan)
{
tengah=(kiri + kanan) /2;
if(key == angka[tengah])
{

ketemu=true;
break;
}
else if (key < angka [tengah])
{
kanan = tengah -1;
}
else
{
kiri = tengah +1;
}
}
if (ketemu == true)
{

cout << “Angka ditemukan! “;
cout << “Data berada pada indek ke-” << tengah;
}
else
cout << “Angka tidak ditemukan!”;
return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s