-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12.cpp
More file actions
34 lines (34 loc) · 698 Bytes
/
Copy path12.cpp
File metadata and controls
34 lines (34 loc) · 698 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
// linear search
#include<iostream>
using namespace std;
int main(){
int a[20],n,i,val,found;
cout<<"Enter the no. of array elements: ";
cin>>n;
cout<<"Enter elements:\n";
for(i=0;i<n;i++){
cout<<"Element "<<i+1<<": ";
cin>>a[i];
}
cout<<"Array:\n";
cout<<"[";
for(i=0;i<n;i++){
cout<<" "<<a[i];
}
cout<<"]\n";
// search logic
cout<<"Enter value to search: ";
cin>>val;
found = 0;
for(i=0;i<n;i++){
if(val==a[i]){
cout<<"Element found at index: "<<i<<"\n";
found = 1;
break;
}
}
if(!found){
cout<<"Element not found!\n";
}
return 0;
}