-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearsearch2.cpp
More file actions
47 lines (42 loc) · 1.07 KB
/
Copy pathlinearsearch2.cpp
File metadata and controls
47 lines (42 loc) · 1.07 KB
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
#include <iostream>
using namespace std;
int linearsearch(int first, int last, int search);
int loadrandomdata();
int id[500];
int total; // number of ID numbers in the array
int search; // the ID number that we are looking for
int index; // index of the ID number we are looking for
int main() {
total = loadrandomdata();
cout << "Enter an id to search for ";
cin >> search;
index = linearsearch(0, total - 1, search);
if (index == -1) {
cout << "The id " << search << " is not in the array.\n";
} else {
cout << "The id " << search << " is at index " << index << endl;
}
}
int linearsearch(int first, int last, int search) {
// first = first index, last = last index
int i;
index = -1;
for (i = first; i <= last; i++) {
if (search == id[i]) {
index = i;
}
}
return index;
}
int loadrandomdata() {
int num;
// load the data randomly - feel free to use more numbers
num = 6;
id[0] = 846;
id[1] = 109;
id[2] = 528;
id[3] = 761;
id[4] = 391;
id[5] = 220;
return num;
}