2017-12-24 01:30:49 +08:00
|
|
|
#include<iostream>
|
|
|
|
using namespace std;
|
|
|
|
|
2017-12-26 18:59:37 +08:00
|
|
|
int LinearSearch(int *array, int size, int key)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
2017-12-26 18:59:37 +08:00
|
|
|
for (int i = 0; i < size; ++i)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
|
|
|
if (array[i]==key)
|
|
|
|
{
|
2017-12-26 18:59:37 +08:00
|
|
|
return i;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2017-12-26 18:59:37 +08:00
|
|
|
int size;
|
|
|
|
cout<<"\nEnter the size of the Array : ";
|
|
|
|
cin >> size;
|
|
|
|
|
|
|
|
int array[size];
|
2017-12-24 01:30:49 +08:00
|
|
|
int key;
|
|
|
|
|
|
|
|
//Input array
|
2017-12-26 18:59:37 +08:00
|
|
|
cout<<"\nEnter the Array of " << size << " numbers : ";
|
|
|
|
for (int i = 0; i < size; i++)
|
2017-12-24 01:30:49 +08:00
|
|
|
{
|
|
|
|
cin>>array[i];
|
|
|
|
}
|
2017-12-26 18:59:37 +08:00
|
|
|
|
2017-12-24 01:30:49 +08:00
|
|
|
cout<<"\nEnter the number to be searched : ";
|
|
|
|
cin>>key;
|
|
|
|
|
2017-12-26 18:59:37 +08:00
|
|
|
int index=LinearSearch(array, size, key);
|
2017-12-24 01:30:49 +08:00
|
|
|
if (index!=-1)
|
|
|
|
{
|
|
|
|
cout<<"\nNumber found at index : "<<index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout<<"\nNot found";
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|