-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.cpp
executable file
·77 lines (61 loc) · 1.93 KB
/
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 <iostream>
#include <fstream>
using namespace std;
// This is to keep track of the number of comparisons
int comp = 0;
// A function that check if two numbers are equal
bool equal (int lhs, int rhs){
// We increment the number of comparisons
// each time this function is called
comp++;
return lhs == rhs;
}
// The search algorithm seen in lectures
// It takes the list of numbers, the value we are
// looking for, and the size of the list
int search(int list[], int value, int size){
for (int i = 0; i < size; i++) {
// If we find a match, we immediately return
// the position of the element that matched
if (equal(list[i], value)){
return i;
}
}
// If the loop completes, and we have not returned
// it means that the number we are looking for
// is not in the list, so we return -1
return -1;
}
int main(int argc, const char * argv[]) {
// Instead of reading from the keyboard,
// all user input will be read from a file
// called input.txt, found in the same folder
// as our executable
ifstream in("search_input.txt");
if (!in.is_open()){
cout << "Can't find input file" << endl;
return 1;
}
// We need to create a const because
// the size of arrays in C++ is not
// allowed to change
const int size = 5;
// Declare an array of the appropriate size
int numbers[size];
// Read in as many elements as specified
// and fill up the array
for (int i = 0; i < size; i++){
int num;
in >> num;
numbers[i] = num;
}
// Now read in the value we are looking for
int val;
in >> val;
// Call our search algorithm
int pos = search(numbers, val, size);
// Print out the number of comparison operations
// that were performed
cout << comp << " comparisons performed" << endl;
return 0;
}