You are on page 1of 2

Chap9

Sequential Search
#include <iostream>
using namespace std;
void initialize(int list[], int size);
void filling(int list[], int size);
void display(const int list[], int size);
int seqSearch(const int list[], int size, int x);
const int s = 10;
int main()
{
int lab[s];
int Item;
initialize(lab, s);
filling(lab, s);
display(lab, s);
cout << "Enter searched item: ";
cin >> Item;
int result=seqSearch(lab, s, Item);
if (result == -1) {
cout <<"The Item "<< Item << " is not found" << endl;
}
else
cout <<"The Item "<< Item << " is found in the position " << result <<
endl;
return 0;
}

void initialize(int list[], int size) {


for (int i = 0; i < size; i++)
list[i] = 0;
}
void filling(int list[], int size) {
cout << "Enter elements of Array: ";
for (int i = 0; i < size; i++)
cin >> list[i];
}
void display(const int list[], int size) {
cout << "Display of Array: ";
for (int i = 0; i < size; i++)
cout<< list[i]<<" ";
cout << endl;
}
int seqSearch(const int list[], int size, int x)
{
bool found = false;
int i = 0;
while ((i < size) && (!found))
{
if (list[i] == x) found = true;
else i++;
}
if (found) return i;
else return -1;
}

You might also like