binary search not returning value

I am writing a code to find a user input value in a randomly generated array. I am not getting any values to be found. As far as i can tell the rest of my code is working ok. I inserted a line to show the array elements before the user input. I've been looking at this for two days now and am just as stumped now as i was before. Help is appreciated!

Thanks!


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void CreateArray(int [], int);
void BubbleSort(int [], int);
int BinarySearch(const int [], int, int);
void Swap(int &, int &);

int main ()
{
const int SIZE = 20;
int RandArray[SIZE];
int input, results, index = 0;
char choice;
int *ptr = nullptr;


CreateArray(RandArray, SIZE); // create random number array

BubbleSort(RandArray, SIZE); // sort random array

while (index < SIZE)
{
cout << RandArray[index] << endl;
index++;

}

do
{
cout << "Please enter a number between 1 and 100" << endl;
cin >> input;
while (input < 1 || input > 100)
{
cout << "please enter a different number that meets the requirements." << endl;
cin >> input;
}

results = BinarySearch(RandArray, SIZE, input); // search for user input

if (results == -1)
cout << "Your number is not in the array" << endl;
else
{
ptr = &results;
cout << "Your number is in the array at position " << ptr << endl;
}

cout << results << endl;

cout << "Do you want to try another number? Enter Y or N" << endl;
cin >> choice;

}
while (toupper(choice) == 'Y');

cout << "End of line" << endl;

return 0;

}


void CreateArray(int array[], int size)
{
unsigned seed = time(0); // get system time
srand(seed); // seed random generator
const int MIN_VALUE = 1, MAX_VALUE = 100, ARRAY_SIZE = 20;



for (int index = 0; ARRAY_SIZE > index; index++) // create array
array[index] = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

for(int i = 0; i < size; i++) //find and change duplicates
{
for(int j = i; j < size; j++)
{
if (array[i] == array[j])
{
array[j] = array[j] - 1;
}
}
}
}


void BubbleSort(int array[], int size)
{
int maxElement;
int index;

for (maxElement = size - 1; maxElement > 0; maxElement--)
{
for (index = 0; index < maxElement; index++)
{
if (array[index] < array[index + 1])
{
swap(array[index], array[index+1]);
}
}
}
}


int BinarySearch(const int array[], int size, int value)
{
int first = 0, last = size - 1, middle, position = -1;
bool present = false;


while (!present && first <= last)
{
middle = (first + last) / 2; // find middle number
if (array[middle] == value) // if value is middle element
{
present = true;
position = middle;
}
else if (array[middle] > value) // if value is in lower half
last = middle - 1;
else
first = middle + 1; // if value is in upper half
}

return position;

}




void Swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
If I understand correctly, you want to see if user can guess one of the randomly generated numbers?

To find a match you must compare the user input to each element of the array.

Maybe like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int BinarySearch(const int array[], int size, int value)
 {
 int  position = -1;



 for (int i = 0;  i < size; i++) {
     if(array[i] == value) {
         position = i;
         break;
     }
 }

 return position;

 }
Last edited on
yes, that works! Im not sure what my code was trying to do then. Thanks!
Hi everyone,I go through all the iteration that you have been mentioned about.I recommended that you must focus on the loop iteration with all int values.Thanks
Topic archived. No new replies allowed.