-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlinearSearch.cpp
More file actions
40 lines (32 loc) · 828 Bytes
/
Copy pathlinearSearch.cpp
File metadata and controls
40 lines (32 loc) · 828 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>
int LinearSearch(int *array, int size, int key) {
for (int i = 0; i < size; ++i) {
if (array[i] == key) {
return i;
}
}
return -1;
}
/** main function */
int main() {
int size;
std::cout << "\nEnter the size of the Array : ";
std::cin >> size;
int *array = new int[size];
int key;
// Input array
std::cout << "\nEnter the Array of " << size << " numbers : ";
for (int i = 0; i < size; i++) {
std::cin >> array[i];
}
std::cout << "\nEnter the number to be searched : ";
std::cin >> key;
int index = LinearSearch(array, size, key);
if (index != -1) {
std::cout << "\nNumber found at index : " << index;
} else {
std::cout << "\nNot found";
}
delete[] array;
return 0;
}