ITPEC FE Subject B April 2026 Question 3

From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array indexes start at 1.

The function distance receives integer arrays S and T of size n as arguments and returns the Manhattan distance of the two vectors that can be computed as ∑ni=1|Si − Ti| . Function abs, which is used by the function distance, returns the absolute value of an integer. Assume that n is 1 or greater.

[Program]
○ integer: distance(integer []: S, integer []: T, integer: n)
integer: p ← 0
integer: i
for (increase i from 1 to n by 1)
blank
endfor
return p

Answer group

Explanation

p ← p + abs(S[i] - T[i]) — the Manhattan distance adds up the absolute difference of each coordinate pair.

The formula ∑ni=1|Si − Ti| maps onto the loop piece by piece:

  • •∑ — accumulate into p with + on every pass of the loop
  • •|Si − Ti| — the absolute value of the difference of the two coordinates, abs(S[i] - T[i])

Example: for S = {1, 5} and T = {4, 2}, p becomes 0 + 3 = 3, then 3 + 3 = 6.

Answer (b)

Why not others:

  • •(a) — adds the coordinates instead of subtracting them; in the example it returns 5 + 7 = 12, which says nothing about how far apart the vectors are
  • •(d) — subtracts each term from p, returning the negative of the distance (−6 in the example); a distance cannot be negative
  • •(c) — makes both mistakes, adding the coordinates and subtracting the result from p

Key rule: Translate a formula symbol by symbol: the summation becomes an accumulating + in the loop, the bars become abs, and the sign inside them stays as written.

Array Manipulation · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.