0

I implemented my search and sorting codes a few weeks ago and back when i ran check50, everything turned out good. But today when i run check50 again, i am getting all red with "killed by server".

What does this even mean??

below is my code for search


bool search(int value, int values[], int n)  
{  
//declear variables  
int lower = 0;  
int upper = n - 1;  
int middle = (lower + upper)/2;  
bool search = false;  

//binary search  
while(!search) {  
    if (values[middle] < values[value])  
        {    
        lower = middle + 1;  
        middle = (lower + upper)/2;  
        }  
    //when middle number is bigger than value, cut the right side  
    else if(values[middle] > values[value])  
        {    
        upper = middle - 1;  
        middle = (lower + upper)/2;  
        }  
    //when value is found  
    else if(values[middle] == values[value])  
        {    
        search = true;  
        }  

 }

 //return search  
 return search;  
}
0
0

"Killed by server" means your program was stuck in an infinite loop, so it was forcibly stopped.
Your code doesn't have any way of dealing with a value that doesn't exist in values, so it would get stuck in an infinite loop.

3
  • Oh thanks. I see the problem now. If i don't input a value, the while loop runs forever. Any suggestions how to approach this problem and fix it? – Xihai Luo Mar 3 '15 at 1:36
  • That's not what I meant. I mean if you input a value that isn't in the "haystack" you're searching, your program has no way of exiting the while loop since search is always false. – Quevun Mar 3 '15 at 2:02
  • Yeah, I know what you meant. I think i didnt explain clearly. I'll try to think of a way to fix this problem. Thanks though. I'll ask for help if i get stuck! – Xihai Luo Mar 3 '15 at 2:59
0

I had a similar issue with Problem Set 0 where it would compile on my local machine but when I used check50 there was a problem and it was killed by server.

What fixed this for me (I'm not sure why or if this'll help you) was by putting the include for the CS50 library first.

Check50 wouldn't work when code was:

#include <stdio.h>
#include <cs50.h>
...

But when the above code was replaced with the below and check50 run again it worked fine:

#include <cs50.h>
#include <stdio.h>
...

Hope this helps :)

You must log in to answer this question.

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