-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathlinear_search.cpp
More file actions
41 lines (35 loc) · 842 Bytes
/
linear_search.cpp
File metadata and controls
41 lines (35 loc) · 842 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
#include <bits/stdc++.h>
using namespace std;
/*
<----------------------------------PROBLEM------------------------------------>
Given an array arr[] of n elements, write a function to search a given element el in arr[].
Variables used:
n = total number of elements in array
v = vector (dynamic array initialized with the input size)
el = element to be searched in the array.
*/
int main()
{
int n;
cin >> n;
vector<int> v(n);
for(int i=0; i<n; i++){
cin >> v[i];
}
int el;
cin >> el;
// Flag variable to keep a check if the element is found till then.
bool flag=false;
for(int i=0; i<n; i++){
if(v[i]==el){
flag=true;
}
}
if(flag){
cout << "Element found:)";
}
else{
cout << "Element not found:(\n";
}
return 0;
}