0

this is my code (find.c) :

#include "helpers.h"

const int HAY_MAX = 65536;

int main(int argc, char * argv[ ])
{
    if (argc != 2)
    {
        printf("Usage: %s need\n", argv[0]);
        return 1;
    }

    int needle = atoi(argv[1]);

    int size;
    int haystack[HAY_MAX];
    for (size = 0; size < HAY_MAX; size++)
    {
        printf("\nhaystack[ % d] = ", size);
        int straw = get_int( );
        if (straw == INT_MAX)
           break;

        haystack[size] = straw;
    }
    printf("\n");

    sort(haystack, size));

    if (search(needle, haystack, size))
    {
        printf("\nFound needle in haystack!\n\n");
        return 0;
    }
    else
    {
        printf("\nDidn't find needle in haystack.\n\n");
        return 1;
    }
}

code (helpers.c):

bool search(int value, int array[ ], int n)
{
    int max = n - 1;
    int min = 0;

    while (max >= min)
    {
        int mid = (min + max)/2;

        if(array[mid] < value)
          min = mid + 1;
        else if (array[mid] > value)
          max = mid - 1;
        else
          return true;
    }
    return false;
}

void sort(int values[ ], int n)
{
    int count[65536] = {0};
    int current = 0;
    for(int i = 0; i < n; i++)
    {
        count[values[i]]++;
    }
    for(int i = 0; i < 65536; i++)
    {
        while(count[i] != 0)
        {
            values[current] = i;
            count[i]--;
            current++;
        }
    }
    return;
}

i'm try alot, but when i'm check50 it's still error like this: https://cs50.me/checks/a17e931852e89daf3a9cdf408972c7b009dc3761

please help me to figure out my problems.

3
  • 1
    I ran the sort and search code, and it appears to be working fine. Is it possible that you failed to recompile (make) your code after changing it? Also, please be sure that you have not changed the find.c file.
    – Cliff B
    Oct 19, 2017 at 5:25
  • idk why, what should i do right now ?
    – Kris Le
    Oct 19, 2017 at 15:36
  • Did you save your file after changing the code? Did you run make again after you saved the changed file?
    – Cliff B
    Nov 5, 2017 at 3:32

0

You must log in to answer this question.

Browse other questions tagged .