After you determine what the program of Exercise 5.45 does, modify the program to func- tion properly after removing the restriction that the second argument be nonnegative.

What will be an ideal response?

```
// Multiply integers (positive or negative) using recursion.
#include
using namespace std;

int mystery( int, int ); // function prototype

int main()
{
int x; // first integer
int y; // second integer

cout << "Enter two integers: ";
cin >> x >> y;
cout << "The result is " << mystery( x, y ) << endl;
} // end main

// mystery multiplies a * b using recursion
int mystery( int a, int b )
{
if ( b < 0 ) // if b is negative
{
// multiply both a and b by -1, so b is positive
// note this multiplies answer by (-1)*(-1) = 1
a *= -1;
b *= -1;
} // end if

if ( b == 1 ) // base case
return a;
else // recursion step
return a + mystery( a, b - 1 );
} // end function mystery
```

Computer Science & Information Technology

You might also like to view...

Where is the track lock button found, and what action is it used for?

What will be an ideal response?

Computer Science & Information Technology

A runnable thread enters the ________ state (sometimes called the dead state) when it successfully completes its task or otherwise terminates (perhaps due to an error).

a. extinct b. defunct c. terminated d. aborted

Computer Science & Information Technology