-
Notifications
You must be signed in to change notification settings - Fork 97
/
linearSearch.cpp
51 lines (50 loc) · 1.18 KB
/
linearSearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
using namespace std;
template<class t>
void linearSearch(vector<t> input_array,int key)
{
register int i=0;
bool flag = false;
for(i=0;i<input_array.size();i++) //iterating the input array
{
if(key == input_array[i]) //If the value is found then return true
{
flag = true;
break;
}
else //if value not found then return false
{
flag=false;
}
}
if(flag==false)
{
cout << "Element not found"<<endl;
}
else if(flag==true)
{
cout << "Element found!!!"<<endl;
}
}
int main()
{
int array_size; //size of the array
cout << "Enter the number of elements you want in the array"<<endl;
cin >> array_size;
int i=0;
vector<int> array;
int num;
//Taking input for the array
cout<<"Enter the Elements of the Array"<<endl;
for(i=0;i<array_size;i++)
{
cin >> num;
array.push_back(num);
}
int key; //element to find
cout << "enter the element to find"<<endl;
cin >> key;
//function called for searching
linearSearch(array,key);
}