-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_search.cpp
77 lines (62 loc) · 1.81 KB
/
linear_search.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cassert>
#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;
}
static void tests() {
int size = 4;
int *array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = i;
}
assert(LinearSearch(array, size, 0) == 0);
assert(LinearSearch(array, size, 1) == 1);
assert(LinearSearch(array, size, 2) == 2);
size = 6;
for (int i = 0; i < size; i++) {
array[i] = i;
}
assert(LinearSearch(array, size, 3) == 3);
assert(LinearSearch(array, size, 1) == 1);
assert(LinearSearch(array, size, 5) == 5);
std::cout << "All tests have successfully passed!\n";
delete[] array;
}
int main() {
int mode = 0;
std::cout << "Choose mode\n";
std::cout << "Self-test mode (1), interactive mode (2): ";
std::cin >> mode;
if (mode == 2) {
int size = 0;
std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
std::cin >> size;
while (size <= 0 || size > 30) {
std::cout << "Size can only be 1-30. Please choose another value: ";
std::cin >> size;
}
int *array = new int[size];
int key = 0;
std::cout << "Enter 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 << "Number found at index: " << index << "\n";
} else {
std::cout << "Array element not found";
}
delete[] array;
} else {
tests();
}
return 0;
}