# EOF NOT WORKING IN PSET 3 LESS FIND

CURRENTLY I AM TRYING TO SOLVE THE PSET3 LESS FIND BELOW IS MY CODE.PROBLEM IS THAT WHEN I WANT TO USE CTRL-D FUNCTION TO EXECUTE EOF FUNCTION IT DID NOT WORK.I THINK CODE ENTER IN A INFINITE LOOP BUT I CAN'T FIGURE THIS OUT,PLEASE HELP ME.

/**
* 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)
{
// TODO: implement a searching algorithm
//ensure element is positive

if (value < 0)
{
return false;
}
//declare the variable for binaray search
int min = 0;
int middle;
int max = n-1;

while(value > 0)
{
middle = min + max/2;
if(values[middle] == value)
{
return true;
}
else if (values[middle] > value)
{
max = middle-1;
}
else if (values[middle] < value)
{
max = middle + 1;
}
}
return false;
}

/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// TODO: implement a sorting algorithm
for (int i = 0; i < n-1; i++)
{
int min = i;

for (int j = i + 1; j < n-1; j++)
{
if (values[j+1] < values[i])
{
min = j+1;

}
}
//swap the min with max
int temp = values[i];
values[i] = values[min];
values[min] = temp;
}

return;
}

THIS IS THE OUTPUT.

~/workspace/pset3/find/ \$ make find
clang -ggdb3 -O0 -std=c11 -Wall -Werror -o find find.c helpers.c -lcs50 -lm
~/workspace/pset3/find/ \$ ./find 42

haystack[0] = 50

haystack[1] = 42

haystack[2] =

The code has an infinite loop. In search, the while loop runs until value is no longer greater than 0. Since value is never changed inside the loop, it will never end unless it hits the return. But why doesn't it?

More importantly, look at the following:

else if (values[middle] < value)
{
max = middle + 1;
}

Shouldn't that be changing min?

There are other issues, but they're for a new question. ;-)

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

• thanks Cliff its help me a lot – mjahanzaib6 Dec 21 '17 at 2:28