Complete the trace table for the program in Figure 1 if the user input is sodium.
Part of the table has already been filled in.
You may not need to use all the rows in the table.
Figure 1
elements = ["argon", "boron", "copper", "helium", "neon", "oxygen", "sodium", "zinc"]
elementToFind = input("What element would you like to find? ")
found = False
low = 0
high = len(elements) - 1
while found == False and low <= high:
mid = (low + high) // 2
if elements[mid] == elementToFind:
found = True
elif elementToFind > elements[mid]:
low = mid + 1
else:
high = mid - 1
print(found)
| elementToFind | found | low | high | mid |
|---|---|---|---|---|
| sodium | 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.