Complete the trace table for the program in Figure 1 if the user input is Spain.
Part of the table has already been filled in.
You may not need to use all the rows in the table.
Figure 1
string[] countries = {"Brazil", "Canada", "Egypt", "France",
"India", "Japan", "Mexico", "Spain"};
Console.Write("Enter a country to find: ");
string countryToFind = Console.ReadLine();
bool validCountry = false;
int start = 0;
int finish = countries.Length - 1;
while (validCountry == false && start <= finish) {
int mid = (start + finish) / 2;
if (countries[mid] == countryToFind) {
validCountry = true;
}
else if (countryToFind.CompareTo(countries[mid]) == 1)
{
start = mid + 1;
} else {
finish = mid - 1;
}
}
Console.WriteLine(validCountry);
| countryToFind | validCountry | start | finish | mid |
|---|---|---|---|---|
| Spain | False | 0 | 7 | 3 |
33 exam-style questions on AQA GCSE Computer Science Searching algorithms. Each one has a worked solution and a mark scheme showing where the marks go.