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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
// book question:
// Write a program that has an array of at least 20 integers in a array
// in ascending order.
// It should call a function that uses the linear search algorithm to locate
// one of the values.
// The function should keep a count of the number of comparisons it makes
// until it finds the value.
// The program then should call a function that uses the binary search algorithm
// to locate the same value.
// It should also keep count of the number of comparisons it makes.
// Display these values on the screen.
// my problem is:
// i think i pretty much got what the book asked, only problem is teacher wants
// the user to be able to enter as many numbers as he or she wants.
// max of 20 obviously. which i'm not sure how to add.
// C.I.S.P. 1010
// Lab:9 searching benchmarks
// 4/15/17
#include <iostream>
using namespace std;
int searchList(const int[], int, int);
int binarySearch(const int[], int, int);
int main()
{
int const SIZE = 20;
int access[SIZE] = { 1, 5, 10, 13, 17, 24, 28, 33, 44, 49,
56, 61, 66, 78, 82, 85, 90, 94, 99, 100 };
int values = 0, countcycles = 0;
do {
countcycles++;
cout << "\nRequest n. " << countcycles << ". Requests left: "
<< 20-countcycles << '\n';
cout << "Please enter a valid value between 1 and 100: ";
cin >> values;
if(values < 1 || 100 < values)
{
cout << '\n' << values << " is out of range. Try again.\n";
values = 1;
continue;
}
cout << "I will search for the value using searchList function\n";
int resultSearch = searchList(access, SIZE, values);
if (resultSearch == -1)
{
cout << "You did not enter a valid value.\n";
}
else
{
cout << "You entered a valid value\n";
cout << (resultSearch + 1) << "\n";
}
cout << "I will now search for the value using the Binary seach "
"function.\n";
int resultsBinary = binarySearch(access, SIZE, values);
if (resultsBinary == -1)
{
cout << "You have entered an invalid value.\n";
}
else
{
cout << "You have entered a valid value.\n";
cout << resultsBinary << endl;
}
// When countcycles gets here, the request has already been fullfilled,
// therefore we need to consider a lower boundary (i.e. 19 instead of 20).
} while(0 < values && values < 101 && countcycles < 19);
return 0;
}
int searchList(const int list[], int size, int value)
{
int index = 0;
while (index < size)
{
if (list[index] == value)
return index;
index++;
}
return -1; // Indicates value not found
}
int binarySearch(const int list[], int size, int value)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (list[middle] == value)
{
found = true;
position = middle;
}
else if (list[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
|