Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAMMING QUESTION The goal of this program is to practice using recursion

ID: 3835560 • Letter: C

Question

C++ PROGRAMMING QUESTION

The goal of this program is to practice using recursion. The Multiplier class performs the mathematical operation a * b in two ways: one is iterative (using loops or control flow) and one is recursive (gets to call itself over and over again). You might not think that addition can be implemented using recursion, but if you follow the cases shown below, it can be done recursively:

BASE CASE: if ( a == 0 || b == 0 ) return( 0 );
BASE CASE: if ( b == 1 ) return( a );
BASE CASE: if ( a == 1 ) return( b );
RECURSIVE CASE: return( a + RecursiveATimesB( a, b - 1 ) );

HINT: You'll probably need to create some additional methods...

Multiplier Class

Sample Driver Code

Multiplier

Multiplier( int a, int b );

int getA() const;
int getB() const;

int RecursiveATimesB() const;
int IterativeATimesB() const;

int myValueofA;
int myValueofB;

// All these calls should produce the
// exact same answer...
// namely, the number 10!

cout << ten.RecursiveATimesB() << endl;
cout << ten.IterativeATimesB() << endl;
cout << ten.RecursiveATimesB() << endl;

Remainder twenty( 4, 5 );
cout << twenty.RecursiveATimesB() << endl;
cout << twenty.IterativeATimesB() << endl;
cout << twenty.RecursiveATimesB() << endl;

Multiplier Class

Sample Driver Code

Multiplier

Multiplier( int a, int b );

int getA() const;
int getB() const;

int RecursiveATimesB() const;
int IterativeATimesB() const;

int myValueofA;
int myValueofB;

Multiplier ten( 5, 2 );

// All these calls should produce the
// exact same answer...
// namely, the number 10!

cout << ten.RecursiveATimesB() << endl;
cout << ten.IterativeATimesB() << endl;
cout << ten.RecursiveATimesB() << endl;

Remainder twenty( 4, 5 );
cout << twenty.RecursiveATimesB() << endl;
cout << twenty.IterativeATimesB() << endl;
cout << twenty.RecursiveATimesB() << endl;

Explanation / Answer

#include <iostream>

using namespace std;
class Multiplier
{
public:
Multiplier( int a, int b )
{
myValueofA = a;
myValueofB = b;
}
int getA() const
{
return myValueofA;
}
int getB() const
{
return myValueofB;
}

int RecursiveATimesB() const
{
return RecursiveATimesB(getA(), getB());
}
int IterativeATimesB() const
{
int b = getB();
int a = getA();
if (b < 0)
{
a = -1*a;
b = -1*a;
}
int result = 0;
for(int i = 0; i < b; i++)
{
result += a;
}
return result;
}
private:
int myValueofA;
int myValueofB;
int RecursiveATimesB(int a, int b) const
{
if ( a == 0 || b == 0 ) return( 0 );
if ( b == 1 ) return( a );
if ( a == 1 ) return( b );
return( a + RecursiveATimesB( a, b - 1 ) );
}
};

int main()
{
Multiplier ten( 5, 2 );
cout << ten.RecursiveATimesB() << endl;
cout << ten.IterativeATimesB() << endl;
cout << ten.RecursiveATimesB() << endl;
Multiplier twenty( 4, 5 );
cout << twenty.RecursiveATimesB() << endl;
cout << twenty.IterativeATimesB() << endl;
cout << twenty.RecursiveATimesB() << endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote