We read your photo as:

Differentiate between call by reference and call by value, construct a trace table for calc(8, 37) and state the operation performed by this subroutine.

Computer Science

ScanSolve AI Answer

a) Differentiate between call by reference and call by value

  • In call by value, a copy of the argument's value is passed to the subroutine. Any changes made to the parameter inside the subroutine do not affect the original argument.
  • In call by reference, a reference (memory address) to the original argument is passed. Changes made to the parameter inside the subroutine directly modify the original argument.

b) Construct the trace table for the subroutine call calc(8, 37)
Given d = 8 and q = 37.
Initial values: r = 0, p = 0.

Stepq >= d * p (37 >= 8 * p)pr
Initial00
Loop 137 >= 8 * 0 (True)10
Loop 237 >= 8 * 1 (True)20
Loop 337 >= 8 * 2 (True)30
Loop 437 >= 8 * 3 (True)40
Loop 537 >= 8 * 4 (True)50
Loop 637 >= 8 * 5 (False, 37 < 40)50
p = p - 140
r = q - (d * p)437 - (8 * 4) = 5
Print45

The subroutine will print p = 4 and r = 5.

c) Assuming that the arguments are both positive, state the operation performed by this subroutine
This subroutine performs integer division and calculates the remainder. It finds the quotient (p) and the remainder (r) when q is divided by d.

Integer division and remainder calculation\boxed{\text{Integer division and remainder calculation}}

d) Give a formula that relates p, r, q & d
The relationship between the quotient (pp), remainder (rr), dividend (qq), and divisor (dd) is:

q=d⋅p+r\boxed{q = d \cdot p + r}

where 0≤r<d0 \le r < d.

Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question
Handwritten step-by-step solution preview

Handwritten Step-by-Step Solution

Get this as a handwritten image on WhatsApp — like a tutor wrote it for you.

Try on WhatsApp