0

I've been working on this for a while now, and I can't for the life of me figure out why the needle can't be found. In gdb when I run the function the second for loop with 'j' only appears once. Does this mean that it is not repeating?

#include <stdio.h>       
#include <cs50.h>
#include <string.h>
#include "helpers.h"


/**
 * BINARY SEARCH FUNCTION
 **/ 

bool search(int value, int values[], int n)
{
    if (n<1)
    {
        return false;
    }
    // TODO: implement a detecting algorithm, if found end loop
    int max=n-1;
    int min=0;
    while ((max-min)>=0)
    {
        int mid = ((min+max)/2);
        if (values[mid]==value)
            return true;
        else if (values[mid]<value)
            min = mid+1;
        else if (values[mid]>value)
            max = mid-1;           
    }
    return false;
}


/**
 * SELECTION SORT FUNCTION.
 */
void sort(int values[], int n)
{
    for (int i=0; i<(n-1); i++)   
    {
        int min = i;    
        for (int j=i+1; j<n; j++)  
            {    
                if (values[j]<values[min]) 
                {
                    min = j; 
                }
            }
        int temp = values[i];           
        values[i] = values[min];    
        values[min] = temp; 
    }
}
0

Noggy, I ran your code on my system and it ran correctly, with my tests, with check50 and with ./generate 1000 50 | ./find 127. There is a similar question [link] helpers.c passes check50 but cant find 127 in haystack with some really good tips. Make sure you searched for 127 (not 128). Make sure you code is compiled properly.

1
  • Thanks I think I must have forgotten to compile the find program after the last tinkering. Stupid mistake, but thanks for the help! – noggy Nov 1 '15 at 0:54

You must log in to answer this question.

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