Friday, March 15, 2013

Sequential Search C++


/*
* Sequential Search
*/

#include <cstdlib>
#include <iostream>

using namespace std;


int main(){
int jml;
int i;
int arr[100];
int cari;
int tanda=-1;
int j=0;
int index[100];

cout <<"Enter how much data : ";
cin >> jml;

for(i=0; i<jml; i++){
cout <<"Enter data "<< i+1 <<" : ";
cin >> arr[i];
}

cout <<"The array : "<< endl;

for(i=0; i<jml; i++)
cout <<" "<< arr[i];

cout <<"\n\nEnter the data you are looking : ";
cin >> cari;

for(int i=0; i<jml; i++){
if(cari==arr[i]){
tanda=i;
break;
}
}

for(int i=0; i<=jml; i++){
if(arr[i]==cari){
index[j]=i;
j++;
}
}

if(tanda!=-1){
cout <<"\n\nThe data found as many as "<< j <<" data."<< endl;
cout <<"The data contained in the array : ";
for(i=0; i<j; i++){
cout <<"["<< index[i]<<"],";
}
cout <<" each.";
cout << endl;
cout <<"The data contained in the row : ";
for(i=0; i<j; i++){
cout <<index[i]+1<<",";
}
cout <<" each.";
cout << endl;
cout << endl;
} else {
cout <<"\nNot found!";
}

system("PAUSE");
return 0;
}

0 comments:

Post a Comment