I've tried using "./generate 1000 50 | ./find 127" and it doesn't find a needle so I guess my code is wrong. I'm not sure if it is my search function or sort function or both that are wrong. Here's a link to the code as well: https://gist.github.com/9121ea9a4e8ce516b0e7fee9614e3ca1
bool search(int value, int values[], int n)
{
// TODO: implement a searching algorithm
int i = 0;
while(n>0)
{
//look at the middle of list
int m=round(n+i)/2;
//if number found, return true
if(value==values[m])
{
return true;
}
//if number higher, search right
if(value>values[m] && value<values[n])
{
i=m+1;
}
//if number lower, search left
if(value>values[i] && value<values[m])
{
n=m-1;
}
}
return false;
}
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n+1; j++)
{
if(values[j]<values[i])
{
values[i]=values[j];
}
}
}
}
n
is passed as a parameter.search()
isint n
.