-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_search.cpp
More file actions
46 lines (45 loc) · 1.01 KB
/
Copy pathlinear_search.cpp
File metadata and controls
46 lines (45 loc) · 1.01 KB
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
#include<iostream>
using namespace std;
bool linearSearch(int list[] , int last ,int target ,int& locn){
int looker =0;
bool found;
while (looker <= last && list[looker] != target)
{
looker++;
}
locn = looker;
if(looker<=last){
found =true;
}
else{
found = false;
}
return found;
}
int main(){
int n,t,locn;
bool found;
int arr[10];
cout<<"enter no.of elements: ";
cin>>n;
for (int i = 0; i < n; i++)
{
arr[i] = rand()%100 ;
}
cout<<"array elements are: "<<endl;
for (int i = 0; i < n; i++)
{
cout<<arr[i]<<" " ;
}
cout<<endl;
cout<<"Enter the number to be searched: ";
cin>>t;
found = linearSearch(arr,n-1,t,locn);
if(found){
cout<<"Element "<<t<<" found at position "<<locn+1<<endl;
}
else{
cout<<"Element "<<t<<" not found in the list "<<endl;
}
return 0;
}