-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_linearsearch.cpp
49 lines (48 loc) · 1.46 KB
/
04_linearsearch.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
#include <iostream>
using namespace std;
//function to fill the empty array with elements
void fillArray(int arr[],int n){
for(int i=0; i<n; i++){
cout<<"arr["<<i<<"]: ";
cin>>arr[i];
}
}
// function to print the given input array
void displayArray(int arr[],int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
// function for linear search
void linearSearch(int arr[],int n){
int item;
cout<<"Enter the element to search in the given array: ";
cin>>item;
int flag = 0;
int i=0;
for(i; i<n; i++){
if(arr[i]==item){
flag=1;
break;
}
}
if(flag==1){
cout<<"Element found at index: "<<i<<endl;
}
else{
cout<<"Sorry!! Element not found"<<endl;
}
}
int main(){
// Here in this, we are going to search an element in the given input array and print the index at which the target or item that is to be searched is found : Linear Search is a easy technique in which we traverses the whole array and we simply compare each element of the array with the item.If it matches, we print the index of the element else return that particular element is not found in the particular array
int n = 10;
int arr[10];
fillArray(arr,n);
cout<<endl;
cout<<"The given input array looks like..."<<endl;
displayArray(arr,n);
cout<<endl;
linearSearch(arr,n);
cout<<endl;
return 0;
}