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 |
Practise AQA GCSE Computer Science Searching algorithms with exam-style questions for GCSE Computer Science. 33 questions, matched to the AQA GCSE Computer Science (8525) specification and written in Paper 1 and Paper 2 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.