Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Searching/Linear Search/linear_searh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

int search(vector<int> arr, int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}

// Driver code
int main()
{
vector<int> arr;
int n;
int x;
cout<<"Enter the number of elements in input : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter the input value of element "<<i+1<<" : ";
int p;
cin>>p;
arr.push_back(p);
}
cout<<"Enter the value to be searched: ";
cin>>x;
int index = search(arr, n, x);
if (index == -1)
cout << "Element is not present in the array";
else
cout << "Element found at position " << index;

return 0;
}