0

I got stuck with my binary search method and I don't really know what I'm doing.

At this moment I got

1 Error (CS0161) not all code paths return a value

int low = 0;
int high = cities.Length;

static int BinarySearch(City[] cities, int key, int low, int high)
{
    int mid;

    if (low > high)
        return -1; //Sökning EJ hittad

    mid = low + high / 2;

    if (key == cities[mid].temp)
        return mid; //Sökning Hittad!
    else if (key < cities[mid].temp)
        BinarySearch(cities, key, low, mid - 1);
    else
        BinarySearch(cities, key, mid + 1, high);
}

...searching for temperature with input from user.

Console.WriteLine("\n\tBINARY SEARCH\n");
do
{
    Console.Write("Search temp:");
    loop = true;
    str = Console.ReadLine();
    try
    {
        key = Convert.ToInt32(str);
        
         
        index = BinarySearch(cities, key, low, high);
        if (index == -1)
        {
            Console.WriteLine($"Couldn't find any city with temperature: {key}°C");
        }
        else
        {
            Console.WriteLine("Search results: ");
            Console.WriteLine(cities[index].ToString());
            loop= false;
        }
    }
    catch
    {
        Console.WriteLine("Only numbers, please.");
    }
} while (loop);
4
  • 2
    You need to return the value from the calls to BinarySearch(), i.e. return BinarySearch(cities, key, low, mid - 1); etc Commented May 6, 2022 at 14:48
  • First of all but unrelated: Always use curly braces even on single-line ifs etc. Especially in the beginning this will save you headaches. Commented May 6, 2022 at 14:48
  • 1
    The BinarySearch method doesn't return anything in the else if or else blocks. Hence, "not all code paths return a value". The method has a return type, so every logical path needs to return something. Commented May 6, 2022 at 14:49
  • Check out this answer for the simplest implementation of recursive Binary Search using C# 12. Commented Jan 28, 2024 at 21:12

1 Answer 1

0

The error message 'not all code paths return a value' is correct:

if (key == cities[mid].temp)
    return mid; //Sökning Hittad!
else if (key < cities[mid].temp)
    BinarySearch(cities, key, low, mid - 1); // <-- does not return anything
else
    BinarySearch(cities, key, mid + 1, high); // <-- does not return anything

Fix:

if (key == cities[mid].temp)
    return mid; //Sökning Hittad!
else if (key < cities[mid].temp)
    return BinarySearch(cities, key, low, mid - 1); 
else
    return BinarySearch(cities, key, mid + 1, high); 
Sign up to request clarification or add additional context in comments.

5 Comments

This worked! Thank you. But there's still one small problem with this code. When I search for a number that isn't an element in the array, I get "Only numbers, please." instead of "Couldn't find any city with temperature: {key}°C"
In that case, actually catch the exception thrown and look into it. But this should be a different issue, then. If you cannot resolve it, feel free to post a new question with the new issue.
Not 100% sure, but int high = cities.Length; might cause an ArgumentOutOfRangeException. I think it should be int high = cities.Length-1;.
int high = cities.Lenght -1 doesn't work well in this case. But I've seen it before.
my issue is not an issue anymore. I dont know why but sometimes the program fix it self through a simple restart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.