Figure 11 shows a binary search algorithm that has been programmed in C# - AQA - GCSE Computer Science - Question 12 - 2023 - Paper 1
Question 12
Figure 11 shows a binary search algorithm that has been programmed in C#.
The CompareTo method is used to compare two strings. It returns:
- -1 if the first string ... show full transcript
Worked Solution & Example Answer:Figure 11 shows a binary search algorithm that has been programmed in C# - AQA - GCSE Computer Science - Question 12 - 2023 - Paper 1
Step 1
Complete the table below for the program in Figure 11 if the user input is wolf.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
animalToFind
validAnimal
start
finish
mid
wolf
False
0
7
3
True
0
7
6
True
7
7
7
Step 2
Extend the program in Figure 12. Your answer must be written in C#.
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
string inputWord;
bool found = false;
Console.Write("What word would you like to find? ");
inputWord = Console.ReadLine();
for (int i = 0; i < fruits.Length; i++) {
if (fruits[i] == inputWord) {
found = true;
break;
}
}
Console.WriteLine(found ? "True" : "False");
Step 3
State why a binary search cannot be used on the array fruits.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
A binary search cannot be used on the array fruits because the array is not ordered or sorted. The elements in the array must be in a sorted sequence for a binary search to work correctly.
Step 4
Rewrite line 1 and line 6 from Figure 13 to make the algorithm work as intended.
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!