Learn to pass parameters and write a simple recursive


Objective

Learn to pass parameters and write a simple recursive subroutine.

Background

In the previous lab, you were required to write a program to accept a positive integer value N, use a subroutine to compute Y where

676_positive integer value.jpg

and print the result on the screen. Let's rewrite this as

Y (M, N) = Y(M) + Y(N)

where

2144_program to accept a positive integer value.jpg

The same problem can be solved recursively. To do that, let's define this function Y(P) as follows:

1. Y(1) = 1. This is the base case.

2. Y(P) = P + Y(P-1).

Notice that Y(P) is defined in terms of a simpler version of itself.

For example, let's say we wish to compute Y(4). The steps would then be

Y(4) = 4 + Y(3) we need to find Y(3) before we can finish computing Y(4)
Y(4) = 4 + (3 + Y(2)) now we need to find Y(2)
Y(4) = (3 + (2 + Y(1))) and now we need Y(1)
Y(4) = 4 + (3 + (2 + 1)) but Y(1) is 1 from our base case
Y(4) = 4 + (3 + 3) evaluate and finish
Y(4) = 4 + 6
Y(4) = 10

Solution Preview :

Prepared by a verified Expert
Engineering Mathematics: Learn to pass parameters and write a simple recursive
Reference No:- TGS01150301

Now Priced at $30 (50% Discount)

Recommended (99%)

Rated (4.3/5)