0

I can't seem to figure this out. here's my code

bool Binarysearch(int key, int array[], int n){

int end = n-1; 
int start = 0; 

while(start<=end){ 
int midpoint = (((end+start)/2));

    if (array[midpoint] == key){
        return true;
    }
    else if (key > array[midpoint]){
        start = midpoint+1;
    }

    else if (key < array[midpoint]){
        end = midpoint-1;
    }
    else {
        break;
    }
    }
    return false;
    }

it compiles and runs, but when I run the command " ./generate 1000 50 | ./find 127" it doesn't find needle in haystack.

http://i.imgur.com/CbWRu5N.png

neither does running "./generate 1000 50 | ./find 128"

I don't really understand what "Seeding is" either when it comes to drand and srand on the generate.c file

I didn't edit anything in the other files besides changing the word "search" to "Binarysearch"


EDIT

_------------------------

void sort(int values[], int n) 
{
    int min = 0;
    // TODO: implement an O(n^2) sorting algorithm search Algorithm SELECTION SORT
    for (int i =0; i<n-1; i++){
        min = i;
        for (int j=i+1; j<n; j++){ 
            if (values[j] <values[min])
                min = j;
        }
        if (min != i)
        values[min] = values[i];
    }
    return;
}
0

Take a look at find.c and see how it's calling your function (if at all).

2
  • i think my problem is in my sorting algorithm , because binarysearch needs a sorted list and I am not sure if my sort() function is working as intended. Can you look into it (I added it into the post), i tried everything else Aug 26 '16 at 13:44
  • As mentioned, find.c is calling a function called search, which it cannot find since you renamed it. As for your sort function, you can call it on a small unsorted list of your own making and see if it works.
    – ronga
    Aug 27 '16 at 6:52
0

You changed search(...) to Binarysearch(...) in find.c You also used Binarysearch(...) in helpers.c

BUT, did you update the function signature from search(...) to Binarysearch(...) in helpers.h????

If this isn't the issue, then I would suggest that you change back to search(...) in helpers.c and actually replace the files find.c and helpers.h with copies from the original distribution. For some reason, I found that I had to do so to get the program to work. I can't explain it, but I observed some unusual behavior. I could get the code to work, but when I tried to pipe from generate to find, it would only show one prompt for input instead of 1000 as expected. Deleting the executables and source files and then replacing the distro files and recompiling everything cured it.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I tried everything you suggested, renamed everything to "search" instead of binary search. I regenerated the file and unzipped the pset3 zip contents again as well and tried it there with the new pasted code. I think my problem is actually in my sort(), because Binary search needs a sorted list and I am not 100% my sort algorithm is working. I added that code into the post for my sorting algrithm Aug 26 '16 at 13:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .