Exercise

Alternative cash flow vector code

In the above example, you may have coded the cash flow vector by writing cf <- c(3, 3, 3, 3, 3, 3, 3, 103).

However, this may seem a bit tedious. An alternative is to code the cash flow vector using the rep() command. Typing rep(x, y) will automatically repeat x y times. For example, rep(1, 4) is equivalent to typing c(1, 1, 1, 1).

In this exercise, you'll construct a more general function for computing a cash flow vector based on a series of inputs: r for coupon rate, p for par value, and ttm for time to maturity. To do so, you'll take advantage of the rep() command.

Instructions

100 XP
  • Code an alternative cash flow function using rep() and input variables r, p, and ttm. Save this new function as alt_cf.
  • Use alt_cf to generate a cash flow vector with coupon rate (r) of 3% (0.03), par value (p) of $100, and time to maturity (ttm) of 8 years.