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!!