# Check50 is claiming that it's recieving an exit code of 0 rather than 1 (different scenario)

I know this question has been answered before (pset 3 "search": expected an exit code of 0, not 1), but I took a look at it, and it seems that the scenario in which the person asked the question is different from mine, while it gets the same results, namely in that the position and way the program runs is different. Is there a reason why this is occurring? Code below:

``````bool search(int value, int values[], int n)
{
//uses binary search to complete the problem
//sets up running midpoint value and other values to control program
int minimum = 0;
int maximum = n - 1;
int midpoint = minimum + ((minimum + maximum)/2);

for (int i = 0; i <= n; i++)
{

if (n <= 0)
{
return false;
}
else
{
//sets up how the program acts in the events of each case
//NOTE: If this happens, either you're lucky, or you deliberately set it up this way. Either way, good job! Partial credit to CS50's binary search video
if (values[midpoint] == value)
{
return true;
}
//NOTE: If the rest happens, you're not so lucky, but ah well
if (values[midpoint] > value)
{
maximum = maximum - 1;
midpoint = minimum + ((minimum + maximum)/2);
}
if (values[midpoint] < value)
{
minimum = minimum + 1;
midpoint = minimum + ((minimum + maximum)/2);
}

}

}

return false;
}
``````

UPDATE: I'm now only receiving this error now.

``````:( finds 42 in {40,41,42}
\ expected an exit code of 0, not 1
``````

This code will never find anything unless value == midpoint. On each pass through the loop, it updates the min or the max, but it never updates midpoint. Since midpoint never changes, the loop will either find the value on the first pass at the midpoint, or the loop will just run to completion without ever finding value.

Side note: it would be more efficient to check if n<=0 just once, before the loop. Checking it on every pass through the loop is a waste after the first check.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Okay, I went ahead and added some code that should update the program, but for some reason, it still doesn't work. I'm going to update the code above. Jul 23 '16 at 5:02
• The updated code is mixing usage of min, mid and max. Sometimes, these vars are being used for the array indexes, other times, they are being used for the actual contents of array elements. You can't mix usage like that. They should strictly be used as indexes, not as array element values. Jul 23 '16 at 5:27
• Okay, based off the information you presented, I changed the following above. I'll show the code I changed. It still doesn't work but I did follow the instructions to the point where I'm only using something as an index. Jul 23 '16 at 5:57
• Worse. `int minimum = values[0];` sets what should be an array index to the content of an array element. `if (values > midpoint)` tries to compare the entire array to what should be the index for the middle of the section of array being tested. I could go on, but you need to take some time and really look at the code and think about what the code is really doing and what it should do. Jul 23 '16 at 6:06
• I'll take a look at the code and come back to you tomorrow. Jul 23 '16 at 6:28