Solution

ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/imperative-programming/1/a/solution

Allocate a fresh array and copy every digit:
function CopyNum(n: number): number
    new(result, len(n))
    for i := 0 to len(n)-1 do
        result[i] := n[i]
    return result
Every coefficient in the digit-array integer representation is unchanged, so the value is unchanged. The new array shares no storage with n; assigning one pointer to the other would instead create reference aliasing. Since the entries are integers, copying the entries also gives the required deep copy of this representation.

New to topics? Read the docs here!