Long time, no post. I guess that's a good thing. Anyway, I have all of my code written, I'm only stuck on this one part. After calling my binarySearch function, I have to display the results. If the result is found, I need to indicate which element it was found in, otherwise indicate that it wasn't found.
I thought using the array would be the way to do it, but it isn't. Here is what I did.
1 2 3 4
if(results != array[ACC_NUMBERS])
cout << "That number does not exist in the array.\n";
else
cout << "That number is found at element " << results << " in the array.\n";
The examples I have only have if(results == -1) to show that the number doesn't exist, but it doesn't account for a possible incorrectly inputted number. It'll show that the number exists using garbage numbers. How do I correctly write my if statement to account for this?
Any insight? I've been trying to think of what to do, but nothing is coming to mind. I guess if it's just this one little thing wrong, I won't get too many points knocked off. Everything else is fine. I'll keep thinking.
What does your binary search return? Does it return an index into some sorted collection, or the element itself? Just a boolean? Something else? What if the element isn't found?
How you do this depends on the results of the search.
if(results != array[ACC_NUMBERS])
If results is supposed to be an index into an array, then testing it against an element doesn't make much sense.
int main()
{
int number, results;
constint ACC_NUMBERS = 10;
int array[ACC_NUMBERS] = {2345981,9879221,8309212,2098732,5420993,
9221987,5092344,2099354,2128309,9873202};
///calls to functions
insertionSort(array, ACC_NUMBERS); ///sorts array in ascending order
print(array, ACC_NUMBERS); ///prints the values of the array
cout << "Enter an account number: ";
cin >> number;
results = binarySearch(array, ACC_NUMBERS, number);
if(results == -1)
cout << "That number does not exist in the array.\n";
else
cout << "That number is found at element " << results << " in the array.\n";
return 0;
}
What does your binary search return? Does it return an index into some sorted collection, or the element itself? Just a boolean? Something else? What if the element isn't found?
It returns the position of the search value. In the if statement, I'm saying which element the number is found at, but I use subscripts in the binarySearch function. I tried testing against subscripts, but I guess I coded it wrong. I'll try again.