Back to Challenge Set 19

Debug Challenge: Search

Difficulty  

divider

Objective

Four searches — Activities 2.9 and 2.10. Not one of them crashes, and only one gets so much as a warning.

Three give a confident wrong answer. One never gives an answer at all.

A search that returns -1 looks exactly the same whether the item is really missing or the code is broken. So you cannot debug these by reading the output alone. You have to check the output against an array you can see.


The Four Programs


Bug 1

This should find any name in the crew. It finds exactly one of them, and it builds with a warning on line 9: Unreachable code detected.

Bug 1
string[] crew = { "Vega", "Orion", "Lyra", "Draco" };
Console.WriteLine(Find("Vega"));
Console.WriteLine(Find("Lyra"));
Console.WriteLine(Find("Nope"));
int Find(string name)
{
for (int i = 0; i < crew.Length; i++)
{
if (crew[i] == name)
{
return i;
}
else
{
return -1;
}
}
return -1;
}
Output
0
-1
-1

Also: "Lyra" is in the crew and the program says it is not. Say which line decided that, and on which pass of the loop. Then say which part of line 9 the warning is about, and why it can never run.


Bug 2

This one never stops. 35 is not in the array, and the program never reaches the last line.

Read it before you run it. If you do run it, press Ctrl+C to stop it.

Bug 2
int[] ids = { 10, 20, 30, 40, 50 };
int target = 35;
int low = 0;
int high = ids.Length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (ids[mid] == target)
{
Console.WriteLine($"found at {mid}");
break;
}
if (ids[mid] < target)
{
low = mid;
}
else
{
high = mid - 1;
}
}
Console.WriteLine("done");

Also: trace low, mid, and high until they stop changing, and write down the three values they get stuck on. The fix is a + 1.


Bug 3

Find is a correct binary search — check it against Activity 2.10 if you like. It reports that 10 and 20 are missing. Both are in the array. It finds 50.

Bug 3
int[] ids = { 40, 10, 50, 20, 30 };
Console.WriteLine($"looking for 10 -> {Find(10)}");
Console.WriteLine($"looking for 50 -> {Find(50)}");
Console.WriteLine($"looking for 20 -> {Find(20)}");
int Find(int target)
{
int low = 0;
int high = ids.Length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (ids[mid] == target)
{
return mid;
}
if (ids[mid] < target)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
Output
looking for 10 -> -1
looking for 50 -> 2
looking for 20 -> -1

Also: the fix is not in the method. Say what it is, and then explain why 50 was found anyway.


Bug 4

This should find all four scores. It finds three.

Bug 4
int[] scores = { 10, 20, 30, 40 };
Console.WriteLine($"looking for 10 -> {Find(10)}");
Console.WriteLine($"looking for 30 -> {Find(30)}");
Console.WriteLine($"looking for 40 -> {Find(40)}");
int Find(int target)
{
for (int i = 0; i < scores.Length - 1; i++)
{
if (scores[i] == target)
{
return i;
}
}
return -1;
}
Output
looking for 10 -> 0
looking for 30 -> 2
looking for 40 -> -1

Also: name the one element this search can never find, in an array of any length, and say which test value you would always include to catch this bug.


Submit

For each program, give:

  • One sentence on what was wrong
  • The corrected line, or lines, of code
  • Your answer to its Also question

And one more, for the set as a whole: three of these four gave a wrong answer with no error. Name one thing you could add to a search program so that a wrong answer would be noticed.

Commence Debugging