-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLsearch.cpp
More file actions
40 lines (31 loc) · 810 Bytes
/
Lsearch.cpp
File metadata and controls
40 lines (31 loc) · 810 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
#include <iostream>
using namespace std;
// Linearly search x in A[]. If x is present then return its
// location, otherwise return -1
int Lsearch(int A[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int main()
{ int A[100],x,n,i,j;
cout << "Enter number of elements in your array{less than 100):";
cin >> n;
for( i=0;i<n;i++)
{cin >> A[i];}
cout << "Your entered list is :";
for( j=0;j<n;i++)
{cout << A[j]<<endl ;}
cout<<"Enter your search element:"<<endl;
cin>>x;
cout<<"The element that you want to search for is:"<<x<<endl;
int flag = Lsearch(A, n, x);
if (flag == -1)
cout << "Element is not present in the array:";
else
cout << "Element found at position: " << flag;
return 0;
}