I have no idea what the problems are with this code, so any help would be appreciated. The code compiles, but the output is skewed. It only sorts after a certain amount of characters, and then sorts badly. Suggested test for linear sort : ./generate 1000 50 | ./find 127 Output: Didn't find needle in haystack. Check50 : Checking....................................................................................................................... Unexpected end of input. Trying to find 3 :jharvard@appliance (~/Dropbox/pset3/find): ./find 3
haystack[0] = 1
haystack[1] = 2
haystack[2] = 3
haystack[3] = Segmentation fault (core dumped).
Thanks!
-mmoore
Here is my code:
#include <cs50.h> #include <math.h> #include <stdio.h> #include "helpers.h" /** * Returns true if value is in array of n values, else false. */ //Recursive Search int k = 0; bool binsearch(int key, int array[], int min, int max) { int j = 0; int mid1 = 0; int mid = 0; if(max < min) { printf("%i", j); return false; } else { mid1 = max / 2; mid = round(mid1); if(array[mid] < key) { return binsearch(key, array, mid + 1, max); } else if(array[mid] < key) { return binsearch(key, array, min, mid - 1); } else { k = k + 1; j = j + 1; printf("%i", j); return true; } } } /** * Returns true if value is in array of n values, else false. */ bool search(int value, int values[], int n) { binsearch(value, values, values[0], n); return k; } /** * Sorts array of n values. */ void sort(int values[], int n) { int j = 0; int k = n; for(int i = 0; i < n; i++) { if(values[i] == 0) { } else if(values[i] < j) { j = values[i]; values[k] = j; k = k - 1; } } return; }