Contoh Program C++ Searching

Sumber:

http://sokhi95.blogspot.com/2013/05/contoh-program-c-searching.html

Program : C++
Specific  : Searching

========================================

#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
main()
{
char npm[500][10];
char nm[500][20];
char kls[500][3];
char npm_cari [10];
int ketemu=0, i=0, jd=0, no=0;
char adm=’Y’;

while (adm==’Y’)
{
clrscr();
jd++;
gotoxy (10,5) ; cout <<“Input Data Mahasiswa”;
gotoxy (10,7) ; cout <<“Data Mahasiswa ke-” << (i+1);
gotoxy (10,8) ; cout <<“NPM   = “;
gotoxy (10,9) ; cout <<“Nama  = “;
gotoxy (10,10); cout <<“Kelas = “;
gotoxy (10,12); cout <<“Ada Data Mahasiswa Lainnya [Y/T] = “;
gotoxy (18,8); gets (npm[i]);
gotoxy (18,9); gets (nm[i]);
gotoxy (18,10); gets (kls[i]);
i++;
gotoxy (45,12); cin >> adm;
}
// Menampilkan seluruh data yg diinputkan
clrscr();
gotoxy (10,2); cout << “Daftar Mahasiswa TI Angkatan 2012”;
gotoxy (2,4); cout << “————————————————“;
gotoxy (2,5); cout << “| No. |   NPM    |       Nama          | Kelas |”;
gotoxy (2,6); cout << “————————————————“;
//                    234567890123456789012345678901234567890123456789
//                            1         2         3         4
for (i=0; i<jd; i++)
{
no=no+1;
gotoxy (2,6+no); cout << “|     |          |                     |       |”;
gotoxy (5,6+no); cout << no << “.”;
gotoxy (10,6+no); cout << npm[i];
gotoxy (21,6+no); cout << nm[i];
gotoxy (45,6+no); cout << kls[i];

}
gotoxy (2,7+no); cout << “————————————————“;
getch();

// Proses Searching

i=0;
clrscr();
gotoxy (20,10); cout << “Mencari Data Mahasiswa”;
gotoxy (20,12); cout << “Inputkan NPM = “; cin >> npm_cari;
while (i<jd && ketemu==0)
{
if(strcmp(npm[i],npm_cari)==0)
ketemu=1;
else
i++;
}

if (ketemu)
{
gotoxy(20,14); cout << “Data Mahasiswa tsb berada pada indeks ke-” << i;
gotoxy(20,16); cout << “Nama  = ” << nm[i];
gotoxy(20,17); cout << “Kelas = ” << kls[i];
}
else
{    gotoxy(20,14); cout << “Data Mahasiswa tsb tidak ditemukan”; }
getch();
}

Contoh Program C++ Searching Mencari Angka

Sumber:

http://pemrogramanamikomsi.blogspot.com/2013/11/contoh-program-c-serching-mencari-angka.html

Contoh program c++ Sequential Search mencari posisi / letak angka.

searching . . . .mencari angka data ditemukan

#include <conio.h>
#include <iostream.h>
main(){
int c,i,posisi;
int A[20]={3,2,4,10,20,1,5,8,7,9,6,5,11,12,14,13,16,15,17,19};

cout<<“Data : “;
for(i=0;i<20;i++){
cout<<A[i]<<” “;
}

cout<<“\nData yang ingin dicari : “;
cin>>c;
i=0;
posisi=0;
while(i<19 && A[i]!=c){
i++;
}
if (A[i]!=c){
cout<<“Maaf data yang dicari tidak ada”;
}else if(posisi=i+1)
cout<<“ditemukan pada posisi ke “<<posisi;
getch();
}