So im running into a strange problem. This is on the "Find:More" PSET3 problem. When I run the program such as:
./generate 1000 50 | ./find 127
or
./generate 1000 50 | ./find 128
the program returns the correct "Did or didn't find the needle in the haystack", although it does print off a ton of numbers like so:
haystack[435] =
haystack[436] =
haystack[437] =
haystack[438] =
haystack[439] =
haystack[440] =
haystack[441] =
haystack[442] =
haystack[443] =
However if I try to do it manually ie:
./find 42
3
33
44
42
2
I receive a seg fault. Whats even weirder is when running the "cs50 check test" I receive:
~/workspace/pset3/find/ $ check50 2016.find.less helpers.c
:) helpers.c exists
:( helpers.c compiles
\ expected an exit code of 0, not standard error of "helpers.c:18:8: error: implicit declara..."
yet my program compiles just fine with make? Any ideas?
helpers.c
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
int min = 0;
int max = n-1;
if(binary(value,values,min,max) == true)
{
return true;
}
else
{
return false;
}
}
bool binary(int value, int values[], int min, int max)
{
int current_index = ((min+max)/2);
if(min > max)
{
return false;
}
else if(values[current_index] == value)
{
return true;
}
else if(values[current_index] < value)
{
min = current_index + 1;
return binary(value,values,min,max);
}
else if(values[current_index] > value)
{
max = current_index - 1;
return binary(value,values,min,max);
}
else
{
return false;
}
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
int temp;
int max_value = 0;
int s = 0;
//Find Max Value
for(int j = 0;j < n;j++)
{
if(values[j] > max_value){
max_value = values[j];
}
}
//Fill Arrays with proper counts
int array_holder[max_value];
for(int c = 0;c < max_value;c++)
{
array_holder[c]=0;
}
for(int i = 0;i < n;i++)
{
temp = values[i];
array_holder[temp] = array_holder[temp]+1;
}
for(int k = 0; k <= max_value; k++)
{
if(array_holder[k] == 0)
{
k++;
}
while(array_holder[k] > 0)
{
values[s] = k;
array_holder[k] = array_holder[k] - 1;
s++;
}
}
return;
}
helpers.h
/**
* helpers.h
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include <stdio.h>
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n);
/**
* Sorts array of n values.
*/
void sort(int values[], int n);
//Recursive function
bool binary(int value, int values[], int min, int max);
Code posted here:
helpers.c: http://pastebin.com/gmhXnryz
helpers.h: http://pastebin.com/KTWd8iQ4