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])
            int one = values[i];
            int two = values[s];
            i = two;
            s = one;
        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!

1 Answer 1


I believe the infinite loop issue is due to the swap variable in the sorting algorithm. I will not fully disclose the errors but i'll just point it out so you can lookup for yourself and understand it.

  1. The swap variable increases after every loop (provided swap is not neg or zero initially, the loop condition will never meet , therefore the infinite loop). Also swap algo is not proper. Look up in the internet for proper sorting algo and understand it before implementing it.

  2. The second error i.e. \ expected an exit code of 0, not 1 , is due to the fact that a return of 0 is expected for successful find. Your statement
    if (values[middle] == value) { return true; return 0; }
    has two return statement which is not valid as only the first return statement is executed and everything after it will not even be seen by the compiler. A function has to return only one return statement. (except if there is a conditional statement to return either of the return statements, which also eventually returns just a single statement).

  3. Your search algo is also logically incorrect in the following sections.

if (values[middle] < value) { min = middle + 1; max = list-1; } //if less
if (values[middle] > value) { max = middle - 1; min = 0; }

So look up for the search algo as well.

Hope that helps. Good Luck!

You must log in to answer this question.

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