Recursive Definition

134 Views Asked by At

Consider the following informal definition for a function calc(x,y) = (0*y) + (1*y) + … + (x*y) For example, we have that calc(2,5) = (0*5) + (1*5) + (2*5) Give a recursive definition for the function calc. Give a trace to show each step involved in calculating calc(3,4) using your definition.

I've come up with:

Base Case:

Calc(0,y) = 0

Recursive Case:

Calc(x,y) = n + Calc(x-1,y) * y

Some how I have a feeling my recursive case isn't going to work as expected.

Can somebody help me with this?

1

There are 1 best solutions below

0
On

The $n$ in your recursive case is not defined, so will cause failure. Your approach is correct, so $n$ should be replaced by $Calc(x,y)-Calc(x-1,y)$. What is the value of that? The recursive call will supply $Calc(x-1,y)$