I'm using the Binary Search algorithm as a function in C++. I'm passing it an int array that I've read from a file, but something is happening in the meantime, since its values never reach the Binary Search function. Instead of four digit customer codes, I'm getting a list of zeros.
This int array works fine in the Bubble Sort function I'm also using.
Function prototype:
Array Definitions:
Here's the function call for Bubble Sort:
Here's the function call for Binary Search:
Here's my Bubble Sort function:
Here's my Binary Search function:
I know this has to be something patently obvious, but for the life of me I can't figure it out. Why does it work for BubbleSort and not BinarySearch?
Any help is appreciated.
This int array works fine in the Bubble Sort function I'm also using.
Function prototype:
Code:
double TotalCharge (int, int);
void BubbleSort (int[], int [], double [], const int, string []);
int BinarySearch (int[], int, int, int);
Array Definitions:
Code:
const int max_num=100;
string Name[max_num];
int Usage[max_num], CustomerNumbers[max_num];
double AmountDue[max_num];
Here's the function call for Bubble Sort:
Code:
BubbleSort (Usage, CustomerNumbers, AmountDue, numcustomers, Name);
Here's the function call for Binary Search:
Code:
result=BinarySearch(CustomerNumbers, key, 0, max_num-1);
Here's my Bubble Sort function:
Code:
void BubbleSort (int a[], int b[], double c[], const int arraySize, string d[])
{
int swap=1, Index, hold1, hold2;
double hold3;
string hold4;
for (int pass = 0; swap==1; pass++) //passes
{
swap=0;
for (Index = 0; Index < arraySize - 1 - pass; Index++) //one pass
if (b [Index] > b[Index + 1])
{
hold1=b[Index];
b[Index]=b[Index + 1];
b[Index + 1]=hold1;
swap=1;
hold2=a[Index];
a[Index]=a[Index + 1];
a[Index + 1]=hold2;
swap=1;
hold3=c[Index];
c[Index]=c[Index + 1];
c[Index + 1]=hold3;
swap=1;
hold4=d[Index];
d[Index]=d[Index + 1];
d[Index + 1]=hold4;
swap=1;
}
}
}
Here's my Binary Search function:
Code:
int BinarySearch (int a[], int search_key, int low, int high)
{
int middle;
while (low <= high)
{
middle = (low+high)/2;
if (search_key == a[middle]) //match
return middle;
else if (search_key < a[middle])
high = middle-1; //search low end of array
else
low = middle+1; //search high end of array
}
cout<<search_key <<endl;
for (int b=0; b<=100; b++)
{cout<<a[b]<<endl;}
return -1; //key not found
}
I know this has to be something patently obvious, but for the life of me I can't figure it out. Why does it work for BubbleSort and not BinarySearch?
Any help is appreciated.