I am not sure why my code is causing the check50 to be 'killed by server' in 2 checks (below) all other checks work out correctly. :)
:( doesn't find 42 in {39,40,41,43}
\ killed by server
:( finds 42 in {42,40,39,41}
\ expected an exit code of 0, not 1
Here is my code:
#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
bool search(int value, int values[], int n)
{
int list = n;
int middle = list / 2;
int min = 0;
int max = n-1;
//while array length is greater than 0
while (min <= max)
{
// fine m
middle = (min + max) / 2;
//find m = n
if (values[middle] == value)
{
return true;
return 0;
}
//if greater
if (values[middle] < value)
{
min = middle + 1;
max = list-1;
}
//if less
if (values[middle] > value)
{
max = middle - 1;
min = 0;
}
}
if (min > max)
{
return false;
}
return 1; } /** * Sorts array of n values. */ // TODO: implement an O(n^2) sorting algorithm
void sort(int values[], int n) { int swap = 0;
for (int i = 0; swap != 0; i++ )
{
for (int s = i+1; s < n; s++)
{
if (values[i] > values[s])
{
//switch
int one = values[i];
int two = values[s];
i = two;
s = one;
swap++;
}
if (values[i] < values[s])
{
i = values[i];
s = values[s];
}
}
}
}
I would love if someone could help me find out what logic I am missing or what section of my code is wrong. I think my code is somehow causing a infinite loop, which causes the program to be killed by the server but I do not know why.
I know that there are many questions similar to this one, but I cannot figure out what is going wrong no matter how I look at my code. All help is greatly appreciated!