When I check my code for helpers.c using check50, it appears that the code is technically working, ie it finds values in the array, and doesn't find them if there not. However, when it finds a number in array, it returns exit code 1 instead of 0, as seen below:
:) helpers.c exists
:) helpers.c compiles
:( finds 42 in {42,43,44}
\ expected an exit code of 0, not 1
:( finds 42 in {41,42,43}
\ expected an exit code of 0, not 1
:( finds 42 in {40,41,42}
\ expected an exit code of 0, not 1
:( 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}
\ 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}
:( finds 42 in {42,40,39,41}
\ expected an exit code of 0, not 1
can someone explain this to me? I hav copied my code for helpers.c below:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include "helpers.h"
void swap(int* pa, int* pb);
bool search(int value, int values[], int n)
{
int first = 0;// declare first value in sorted array
int last = n; // declare last value in sorted array
while(last<=first)
{
int middle = (first + last)/2;
if(value==values[middle])
{
return true;
}
else if(value>values[middle])
{
first = middle + 1;
}
else if(value<values[middle])
{
last = middle - 1;
}
}
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
int swap_count = 2;
do
{
for(int i = 0; i <= n; i++)
{
for (int j = 0; j <= n-1-i; j++)
{
swap_count = 0;
if(values[j] > values[j+1])
{
int* pa = &values[j];
int* pb = &values[j+1];
swap(pa, pb);
swap_count++;
}
}
}
}
while (swap_count != 0);
return;
}
void swap(int* pa, int* pb)
{
int temp = *pb;
*pb = *pa;
*pa = temp;
}
thankyou for your advice, I have amended my code as you suggested. It now passes more of the checks in check 50, but is still returning the wrong exit code in 3 cases:
:) 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} :( finds 42 in {42,40,39,41} \ expected an exit code of 0, not 1
do you have any further suggestions - my edited code is below?
#include <stdio.h>
include
include
include "helpers.h"
void swap(int* pa, int* pb);
bool search(int value, int values[], int n)
{
if(n<=0)
{
return false;
}
int first = 0;// declare first value in sorted array
int last = n-1; // declare last value in sorted array
while(first<=last)
{
int middle = (first + last)/2;
if(value==values[middle])
{
return true;
}
else if(value
{
last = middle - 1;
}
else if(value>values[middle])
{
first = middle + 1;
}
}
return false;
}
/**
* Sorts array of n values.
/
void sort(int values[], int n)
{
int swap_count = 2;
do
{
for(int i = 0; i <= n; i++)
{
for (int j = 0; j <= n-1-i; j++)
{
swap_count = 0;
if(values[j] > values[j+1])
{
int pa = &values[j];
int* pb = &values[j+1];
swap(pa, pb);
swap_count++;
}
}
}
}
while (swap_count != 0);
return;
} void swap(int* pa, int* pb) { int temp = *pb; *pb = *pa; *pa = temp; }