### Returning a Comparison value for Linear and Binary search problem

Ok, so my current issue with my code is that I cannot figure out how to properly output a comparison value, for how long it takes my program to find the number entered with linear and binary search, I've looked everywhere and just can't seem to figure it out. A friend of mine told me to use count++, so I figured I would at least throw that in for the ole' college try. The areas that need help are lines 27 and 38, with the current way I have it coded, it just returns 1 and 2 for linear and binary search respectively, no matter what number in the array is entered.

Any and all help is appreciated, thanks!!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````#include using namespace std; int searchList(const int list[], int numElems, int value); int binarySearch(const int array[], int numElems, int value); int main () { const int arraySize = 20; int a[arraySize] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; int number; int count = 0; cout << "Please pick a number between 1 and 20 to search the index for." << endl; cin >> number; //Linear Search int index = searchList(a, arraySize, number); if(index !=-1) { cout << "The number's element found by linear search is " << index << endl; count++; cout << "The amount of comparison's used to find the element\nWith linear search is " << count << endl; } else cout << "That is not a valid number within the linear search index" << endl; //Binary Search index = binarySearch(a, arraySize, number); if(index != -1) { cout << "The number's element found by binary search is " << index << endl; count++; cout << "The amount of comparison's used to find the element\nWith binary search is " << count << endl; } else cout << "That is not a valid number within the binary search index." << endl; return 0; } //Linear Search Function int searchList(const int list[], int numElems, int value) { int index = 0; int position = -1; bool found = false; while (index < numElems && !found) { if (list[index] == value) { found = true; position = index; } index++; } return position; } //Binary Search Function int binarySearch(const int array[], 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 (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle - 1; else first = middle + 1; } return position; }``````
The values you are interested in comparing are the number of probes made by each search function in attempting to locate the the element in the data structure. So, you need to count the probes from within the search routines. You could add a counter argument to your functions. For example:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` `````` int searchList(const int list[], int numElems, int value, int &numProbes) { int index = 0; int position = -1; bool found = false; numProbes = 0; while (index < numElems && !found) { numProbes++; if (list[index] == value) { found = true; position = index; } index++; } return position; } int binarySearch(const int array[], int size, int value, int &numProbes) { int first = 0, last = size - 1, middle, position = -1; bool found = false; numProbes = 0; while (!found && first <= last) { numProbes++; middle = (first + last) / 2; if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle - 1; else first = middle + 1; } return position; }``````
Last edited on
Thank you so much! This works perfectly.
Topic archived. No new replies allowed.