Programming Basics (Pseudocode Tracing)

When the following function c is called as c(4, 2), how many times is c called, including the first call?

○integer: c(integer: n, integer: k)
  if (k = 0 or k = n)
    return 1
  endif
  return c(n − 1, k − 1) + c(n − 1, k)
  1. A5
  2. B6
  3. C11
Show answer and explanation

Answer: C. 11

c(n, k) computes the number of ways to choose k items from n (the binomial coefficient) using the Pascal's triangle relation. c(4, 2) calls c(3, 1) and c(3, 2); these call c(2, 0) and c(2, 1), and c(2, 1) and c(2, 2); each of the two c(2, 1) calls makes calls to c(1, 0) and c(1, 1). There are 6 calls that return 1 and 5 calls that make two further calls, 11 in total. The return value is 6, equal to the number of calls that return 1.

  • A. This counts only the calls that make two further calls. The 6 calls that return 1 also count.
  • B. This is the return value c(4, 2) = 6, not the number of calls.
  • C. Correct. The 6 calls that return 1 plus the 5 that make two further calls.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. ⊕ is bitwise exclusive OR. What are the values of x and y after the following program runs?
  2. x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. What is the value of y after the following program runs?
  3. ∧ is bitwise AND. What is the value of c after the following program runs?
  4. x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. With x = 75, what is the value of y after the following assignment?
  5. The following procedure bubble is a bubble sort that sorts the integer array data in ascending order. swap(data, j, k) is a procedure that swaps the values of data[j] and data[k]. Array indices start at 1. What should replace 【a】 in the program?