A Nasa robot (bottom right) is trying to rendezvous back with the spaceship (top left). However, it cannot enter the marked yellow squares, which represent unstable areas due to tectonic activity.
Which of these algorithms would guide the robot safely back to the spaceship?
Algorithm 1: Walk 1 square up and 1 square left. Repeat.
Algorithm 2: Walk 2 squares up and 2 squares left. Repeat.
Algorithm 3: Walk 1 square left and 1 square up. Repeat.
Algorithm 4: Walk 2 squares left and 2 squares up. Repeat.
Peter wants to create an algorithm to ensure that he buys exactly N items.
Which of these algorithms could he use?
Algorithm 1: Start with .
Buy one item. Increase by 1.
Repeat. Stop when .
Algorithm 2: Start with .
Buy one item. Multiply by 2.
Repeat. Stop when .
Algorithm 3: Start with .
Buy one item. Increase by 1.
Repeat. Stop when .
Helen wants to create an algorithm that would help her find the largest number in a set. Assume that the set is denoted by a[0], a[1], ..., a[N-1]
.
Which of these algorithms could she use?
Algorithm 1:
i = 0
.i < N - 1
:
large = a[i]
. i
by 1
.Algorithm 2:
i = 0
and large = a[0]
.i < N
:
a[i] > large
, replace large
with a[i]
. i
by 1
.Algorithm 3:
i = 0
. Set large = a[0]
. i < N - 1
:
a[i] > large
, replace large
with a[i]
. i
by 1
.Jordan wants to create an algorithm to count the number of positive numbers out of an array of real numbers. Assume that the array is denoted by {a[0], a[1], ... , a[N - 1]}
. Clearly, N
is the number of elements in the array a
Which of these algorithms could he use? (They only differ by the first line.)
Algorithm 1:
i = 0
and total = 0
. i < N
:
a[i] > 0
, increase total
by 1. i
by 1. Algorithm 2:
i = 0
and total = 1
. i < N
:
a[i] > 0
, increase total
by 1. i
by 1. Algorithm 3:
i = 1
and total = 0
. i < N
:
a[i] > 0
, increase total
by 1. i
by 1.Susan wants to create an algorithm that finds the Nth Fibonacci number, which follows the rules that
Which of the following algorithms could she use to find ?
Algorithm 1: Set .
1. If , print and end the program.
2. Set and increase by 1.
3. Go back to step one.
Algorithm 2: Set .
1. If , print and end the program.
2. Set and increase by 1.
3. Go back to step one.
Algorithm 3: Set .
1. If , print and end the program.
2. Set and increase by 1.
3. Go back to step one.