Get rid of userUnsortedList and return to your code from your post on Jul 25, 2018 at 2:33pm. Here are some comments referring to that code:
You haven't declared binarySearch right. Just like bubbleSort, it needs a pointer to the array and the size of the array. It also needs the value that you're searching for. So change line 8 to
int binarysearch(int *ptr1, int size, int value);
and change line 96 to
int binarysearch(int *userlist, int numElems, int value)
Change searchVariable() to return the value (you fixed this in your most recent code). Also, it doesn't need to take any parameters. So change line 9 to:
int searchvariable();
and change lines 63-67 to:
1 2 3 4 5 6 7
|
int searchvariable()
{
int value;
cout << "Integer to search array for: " << endl;
cin >> value;
return value;
}
|
Note that
value
inside
searchvariable
is a local variable, so it is a different variable from the
value
declared in main.
Right now bubbleSort() is declared as returning an int. It doesn't need to return anything so change the return type to void at lines 7 and 70.
In bubbleSort() at line 80, index goes through all the possible values in the array. But lines 82 and 85 access the value
after index. So line 80 should really be:
for (int index = 0; index < SIZE-1; index++) {
It looks like you got lost in the code that gets the searchvariable and does the binary search. You need the value that you're going to search for, and you need to remember the index of the value returned by binarysearch():
int value, index;
Getting the value to search for is pretty easy:
value = searchvariable();
Then you call binarysearch() to find the value. Binarysearch() returns an index:
index = binarysearch(ptr1, inArraySize, value);
But after calling binarysearch(), you can't just print out
ptr1[index]
because binarysearch might not have found it. binarysearch() returns -1 if it doesn't find the value so you need to check for that case. Maybe something like:
1 2 3 4 5
|
if (index == -1) {
cout << value << " isn't in the array\n";
} else {
cout << value << " is at location " << index+1 << '\n';
}
|
The code in binarysearch isn't quite right:
- remove subptr (delete line 100 & 106)
- change subptr to middle at lines 107 and 109.
- change < to > at line 109
After these changes, the program should work.