ITPEC FE Subject B April 2025 Question 15

From the answer group below, select the correct combination of answers to be inserted into A and B in the description. Here, the array index starts at 1.

The calcDistance returns a value based on array p1, p2 and positive integer n. Assuming that the number of elements in arrays p1 and p2 are the same. The function abs(x) returns the absolute value of x, and pow(a, b) returns a raised to the power of b.
In the case of p1 is {3, 1, 5, 2} and p2 is {4, 6, 2, 3}, the calcDistance(p1, p2, 1) returns A, whereas calcDistance(p1, p2, 2) returns B. As the value of n increases, those of calcDistance(p1, p2, n) converges to 5.

[Program]
○ real: calcDistance(real []: p1, real []: p2, integer: n)
integer: i
real: distance ← 0
real: ex
for (increase i from 1 to number of elements in p1 by 1)
distance ← distance + pow(abs(p1[i] - p2[i]), n)
endfor
ex ← 1 ÷ n /* Division is performed in data type real */
distance ← pow(distance, ex)
return distance

Answer group

OptionAB

Explanation

A: 10 and B: 6 — the function sums the n-th powers of the element differences and takes the n-th root: the Minkowski distance, Manhattan for n = 1 and Euclidean for n = 2.

Element by element, the absolute differences between {3, 1, 5, 2} and {4, 6, 2, 3} are 1, 5, 3 and 1.

  • •n = 1: 1 + 5 + 3 + 1 = 10, and ex = 1, so pow(10, 1) returns 10
  • •n = 2: 1 + 25 + 9 + 1 = 36, and ex = 0.5, so 360.5 = 6

As n grows the largest difference dominates, so the result falls toward that maximum, 5, as the description says. No value of calcDistance can be below 5.

Answer (f)

Why not others:

  • •(a), (b), (c) and (d) — A = 4 is the gap between the element totals, 15 - 11 = 4: taking the absolute value after summing lets the signed differences −1, −5, 3 and −1 cancel, while the program applies abs to each pair
  • •(a) and (e) — B = 4 is below 5, the largest single difference, which the distance can never drop under; the sum of squares is 36, not 16
  • •(c) and (g) — B = 10 repeats the n = 1 result, as if squaring and the square root cancelled; they do not across a sum, since 360.5 = 6
  • •(d) and (h) — B = 18 is 36 ÷ 2: dividing by n instead of raising to the power 1 ÷ n

Key rule: pow(x, 1 ÷ n) is the n-th root, not a division by n, and an absolute value inside a loop applies to each term before the terms are added.

Math & Data Science · 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.