-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinearSearch.cpp
72 lines (67 loc) · 1.45 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
using namespace std;
//THIS FUNCTION PRINTS THE ARRAY OF INDEXES FOUND
template <class t>
void lsearch(vector<t> array,t f)
{
vector<t> ans;
int flag=0;
register int i=0;
for(i=0;i<array.size();i++)
{
if(f == array[i])
{
ans.push_back(i);
}
else
{
flag++;
}
}
if(flag==array.size())
{
cout << "ELEMENT NOT FOUND" << endl;
}
else
{
cout << "INDEX ARRAY: ";
for(i=0;i<ans.size();i++)
{
cout << ans[i] << " ";
}
cout << endl;
}
}
int main()
{
int n; //size of the array
register int i=0;
//for int array
cout << "Enter the number of elements you want to enter the int array" << endl;
cin >> n;
int find; //element to find
int num; //sample element to enter
vector<int> arr; //declared a int vector
for(i=0;i<n;i++)
{
cin >> num;
arr.push_back(num);
}
cout << "Enter the element to find in int array" << endl;
cin >> find;
lsearch(arr,find);
cout << "Enter the number of elements you want to enter the float array" << endl;
cin >> n;
float ffind;
float sample;
vector<float> ary;
for(i=0;i<n;i++)
{
cin >> sample;
ary.push_back(sample);
}
cout << "Enter the element to find in float array" << endl;
cin >> ffind;
lsearch(ary,ffind);
}