forked from Vishal-Aggarwal0305/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.cpp
More file actions
38 lines (33 loc) · 826 Bytes
/
LinearSearch.cpp
File metadata and controls
38 lines (33 loc) · 826 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
#include <iostream>
using namespace std;
int main()
{
int element, ind, flag = 0;
int num;
cout<<"Enter number of element: ";
cin>>num;
int arr[num];
cout << "\nEnter the elements \n"; //Entering elements of array
for (int i = 0; i < num; i++)
{
cin >> arr[i];
}
cout << "Enter element to search in array "; //Element to be searched
cin >> element;
for (int i = 0; i < num; i++)
{
if (arr[i] == element) //if element found
{
ind = i; //store the index of that element
break;
}
else
int flag = 1; //not found
}
if (flag == 1)
{
cout << "\nElemnt not found in your array";
}
else
cout << "\nElement found at index " << ind << " position " << ind + 1;
}