0

i wrote the helpers.c which seems correct to me but every time I check with cs50, it tells me server was killed, help is appreciated.

include

include "helpers.h"

/** * Returns true if value is in array of n values, else false. */ bool search(int value, int values[], int n) {

int lower = 0;
int upper = n -1;

// checking whether n less than zero 
if (n < 0)
{
return false;
}
// implementing the binary search tree
while(lower <= upper)
{
    // getting the midpoint of the array index
    int midpoint = (upper - lower) / 2;

    // checking whether the array's midpoint value is greater than user value.
    if (values[midpoint] > value)
    {
        // our upperbound is now updated to half of the original array
        upper = midpoint - 1;
    }
    // same logic as above
    else if (values[midpoint] < value)
    {
        // our lowerbound is now updated to half of original array
        lower = midpoint + 1;
    }
    else if (values[midpoint] == value)
    {
        return true;
    }
    else
    {
        return false;
    }


}
return 0;

}

/** * Sorts array of n values. */ void sort(int values[], int n) { // Bubble sort: implement an O(n^2) sorting algorithm

int counter;
do
{
    counter = 0;
    // iterate through the array
    for(int i = 0; i < n; i++)
    {
       // checking whether the current value greater than next value
        if (values[i] > values[i+1])
        {
            // swapping the neighbour values
            int temp = values[i+1];
            values[i + 1] = values[i];
            values[i] = temp;
            counter++;
        }
    }
// once counter stays at zero we know the list is sorted :)    
}while(counter > 0);

return;

}

0

Your code goes into an infinite loop under certain conditions, such as when the needle is in the upper part of the list: needle = 42, list = 40, 41, 42, 43.

Try inserting the following into your search function just before lower=midpoint+1;

        printf("low=%i, mid=%i high=%i \n", lower, midpoint, upper);

This will give you an idea what needs to be fixed. Look carefully at the logic, including the test condition of the while loop.

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

| improve this answer | |
  • cool thanks, I just changed the midpoint = lower+upper/2 in stead of upper-lower/2, but i still have some problem::) helpers.c exists :) helpers.c compiles :) finds 42 in {42,43,44} :) finds 42 in {41,42,43} :( finds 42 in {40,41,42} \ expected an exit code of 0, not 1 :) finds 42 in {41,42,43,44} :) finds 42 in {40,41,42,43} :( finds 42 in {39,40,41,42} \ expected an exit code of 0, not 1 :) doesn't find 42 in {39,40,41} :) doesn't find 42 in {39,40,41,43} – Arsenal Vakili Dec 21 '15 at 20:02
  • id you notice that the only fails occur when 42 is the last element in the list? – Cliff B Dec 21 '15 at 20:13
  • so i am assuming search would never find the value at the end of the list, I am assuming it has to do with my while condition: (lower <= upper), am i on right track? – Arsenal Vakili Dec 21 '15 at 21:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .