-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.cpp
More file actions
38 lines (38 loc) · 791 Bytes
/
LinearSearch.cpp
File metadata and controls
38 lines (38 loc) · 791 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
// Program for performing linear search in array
#include <iostream>
using namespace std;
int linearSearch(int *arr, int size, int element)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == element)
{
return 1;
}
}
return 0;
}
int main()
{
int size;
cout << "Enter the size of the array: ";
cin >> size;
int *arr = new int[size];
cout << "Enter the elements: ";
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
int elementToSearch;
cout << "Enter the element to search: ";
cin >> elementToSearch;
if (linearSearch(arr, size, elementToSearch))
{
cout << "Element is found" << endl;
}
else
{
cout << "Element is not found" << endl;
}
return 0;
}