-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlinearSearch.cpp
More file actions
41 lines (33 loc) · 771 Bytes
/
linearSearch.cpp
File metadata and controls
41 lines (33 loc) · 771 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
41
#include <iostream>
using namespace std;
int linearSearch(int array[], int size, int element){
for (int i = 0; i < size; ++i) {
if (element == array[i]) {
return i;
break;
}
}
return -1;
}
int main(int argc, char const *argv[]) {
int array[20];
int size;
int element;
int index;
cout << "Enter the size of array: ";
cin >> size;
cout << "Enter the elements of array: ";
for (int i = 0; i < size; ++i) {
cin >> array[i];
}
cout << "Enter the element to be found: ";
cin >> element;
index = linearSearch(array,size, element);
if (index != -1) {
cout << "Element " << element << " found at location " << index;
}
if (index == -1) {
cout << "Element " << element << " NOT found.";
}
return 0;
}