-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearSearch.cpp
More file actions
42 lines (42 loc) · 843 Bytes
/
Copy pathlinearSearch.cpp
File metadata and controls
42 lines (42 loc) · 843 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int linear_search(int arr[], int el)
{
int found = 0;
int pos;
for (int i = 0; i < sizeof(arr) - 1; i++)
{
if (arr[i] == el)
{
found = 1;
pos = i;
break;
}
}
return found ? pos : 0;
}
int main()
{
int n;
cout << "How many elements?" << endl;
cin >> n;
int arr[n];
cout << "Give the elements one by one" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int search;
cout << "How element do you want to search?" << endl;
cin >> search;
int result = linear_search(arr, search);
if (!result)
{
cout << "not found" << endl;
}
else
{
cout << search << " is found in postion: " << result + 1 << endl;
}
return 0;
}