Binary Search function problem in C++

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.

Always post a data file, i'm going to try to generate one myself... but an initial comment on style:

Code:

for (Index = 0; Index < max_num; Index++)
{AmountDue[Index]=0;}

That makes my eyes bleed. Again, since this is a style issue, there are options that are more readable. Here are two:

Code:

for (Index = 0; Index < max_num; Index++) {
AmountDue[Index]=0;
}

Code:

for (Index = 0; Index < max_num; Index++)
{
AmountDue[Index]=0;
}

or even (though you'd have to change it if you needed more in the loop):

Code:

for (Index = 0; Index < max_num; Index++) AmountDue[Index]=0;

I far prefer the first example. If there's no brace on the line with your control structure, you don't know if a block follows, a single expression, etc. If you see the brace right there, you know what's coming, and that there will be a matching brace (hopefully on its own line) that ends the control structure.

Again, this is a stylistic point, but it's never too early to learn to write readable code.

Indentation, in most cases, is used to indicate a new scope, i.e. a function, control block, etc. This waterfall of if-then-else is not terribly difficult to understand, but is an eyesore. Most people would be used to seeing something like that more like this:

The error wasn't that serious. My major issues with the code is style at this point, but the error was in how you called your binary search function.

How you call it now would work if your array CustomerNumbers was FULL. The example file i wrote only had 4 entries. That meant there were a lot of 0s in the CustomerNumbers array. 0 is less than any 4 digit number. This breaks binary search because it depends on a sorted array. The array isn't completely sorted. You pass the number of records to BubbleSort, so it doesn't sort 1001 after all of the empty 0 entries. Your BinarySearch function already accepts a high value.

-Lee

P.S. This was the data i used, if someone else was to want to look at it:

Always post a data file, i'm going to try to generate one myself... but an initial comment on style:

Code:

for (Index = 0; Index < max_num; Index++)
{AmountDue[Index]=0;}

That makes my eyes bleed. Again, since this is a style issue, there are options that are more readable. Here are two:

Code:

for (Index = 0; Index < max_num; Index++) {
AmountDue[Index]=0;
}

Code:

for (Index = 0; Index < max_num; Index++)
{
AmountDue[Index]=0;
}

or even (though you'd have to change it if you needed more in the loop):

Code:

for (Index = 0; Index < max_num; Index++) AmountDue[Index]=0;

I far prefer the first example. If there's no brace on the line with your control structure, you don't know if a block follows, a single expression, etc. If you see the brace right there, you know what's coming, and that there will be a matching brace (hopefully on its own line) that ends the control structure.

Again, this is a stylistic point, but it's never too early to learn to write readable code.

Indentation, in most cases, is used to indicate a new scope, i.e. a function, control block, etc. This waterfall of if-then-else is not terribly difficult to understand, but is an eyesore. Most people would be used to seeing something like that more like this:

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.