forked from NJACKWinterOfCode/nwoc_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_Search.cpp
57 lines (46 loc) · 963 Bytes
/
Linear_Search.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
// C++ program to implement Linear Search
// Output:-
/* Enter the size of array
5
Enter the elements of array
1 7 9 8 17
Enter the element to be searched
9
Key found at position 2
Enter the size of array
5
Enter the elements of array
1 7 9 8 17
Enter the element to be searched
20
Key not found */
// Source Code:-
#include<iostream>
using namespace std;
int main()
{
int total_size;
cout<<"Enter the size of array\n";
cin>>total_size;
int arr[total_size],iter,key,flag=0,pos;
cout<<"Enter the elements of array\n";
for(iter = 0; iter < total_size; iter++)
{
cin>>arr[iter];
}
cout<<"Enter the element to be searched\n";
cin>>key;
for(iter = 0; iter < total_size; iter++)
{
if(arr[iter] == key)
{
flag = 1;
pos = iter;
}
}
if(flag == 1)
cout<<"Key found at position "<<pos;
else
cout<<"Key not found";
return 0;
}