0

I am not sure what is wrong with the code. It seems to work on some of the criteria when using cs50's check50, but does not work on others. There also does not seem to have a pattern about the wrong ones.

Here is my helpers.c code:

//helpers.c
//Helper functions for Problem Set 3.

#include <cs50.h>

#include "helpers.h"

bool b_search(int value, int array[], int low, int high);

// Returns true if value is in array of n values, else false.
bool search(int value, int values[], int n)
{
    return b_search(value, values, n - n, n - 1);
}

// Binary search with 4 parameters, different from original prototype
bool b_search(int value, int array[], int low, int high)
{
    // Check for non-positive n, retrun false
    if (low > high)
    {
        return false;
    }

    // Check for n == 1, check value for both high and low
    if (low == high)
    {
        if (value == array[low] || value == array[high])
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // Find new mid-point
    int mid = (low + high) / 2;

    // When key < mid-point
    if (value < array[mid])
    {
        return b_search(value, array, low, mid - 1);
    }
    // When key > mid-point
    else if (value > array[mid])
    {
        return b_search(value, array, mid + 1, high);
    }
    return false;
}

//Sorts array of n values.
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm: bubble sort
    // Set swap counter to non-zero value
    int swap = -1;

    for (int i = 0; i < n - 2; i++)
    {
        // Set swap counter to zero
        swap = 0;
        for (int v = 0; v < n - i - 1; i++)
        {
            // Compare adjacent elements
            if (values[v] > values[v + 1])
            {
                // Swap adjacent elements, add 1 to swap counter
                int temp = values[v];
                values[v] = values [v + 1];
                values[v] = temp;
                swap++;
            }
    }

    // If no swaps from 0'th element to n-1'th element, break
    if (swap == 0)
    {
        break;
    }

    }
    return;
}

However, when I carry out check50, here is the output it gives:

~/workspace/pset3a/ $ check50 2016.find.less helpers.c
:) helpers.c exists
:) helpers.c compiles
:) finds 42 in {42,43,44}
:( finds 42 in {41,42,43}
   \ expected an exit code of 0, not 1
:) finds 42 in {40,41,42}
:( finds 42 in {41,42,43,44}
   \ expected an exit code of 0, not 1
:( finds 42 in {40,41,42,43}
   \ expected an exit code of 0, not 1
:) finds 42 in {39,40,41,42}
:) doesn't find 42 in {39,40,41}
:) doesn't find 42 in {39,40,41,43}
:( finds 42 in {42,40,39,41}
   \ expected an exit code of 0, not 1
https://sandbox.cs50.net/checks/acae46e132714d9f9a7a1607a52219a9

Please help!!

0

There are several problems in the code that need attention.

First, simply put, sort is not working. Because of the nature of the issues, I'm only going to provide pointers toward them, giving you the chance to debug them.

  • There is a flaw in the code that swaps elements within the array.
  • There is an issue with the for loop limits in your nested for loops.

You should edit the end of the sort code as follows to print out the list to see if it has successfully sorted, and test with different lists.

for (int i=0;i<n;i++) printf("%i " , values[i]);
   printf("\n");

   return;
}

There's also a serious flaw in the b_search() code. What happens when the target is actually equal to values[mid]?

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

You must log in to answer this question.

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