Question

C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...

C++ Recursion Practice!

1)Multiplication

#include <iostream.h>

int Multiply(int M, int N)

//Performs multiplication using the + operator.

//Pre : M and N are defined and N > 0.

//Post: Returns M x N

{

int Prod;

if (N == 1)

    Prod = M;                       //base case

else

    Prod = M + Multiply(M, N - 1); //recursive step

return Prod;

}

2) Reverse

#include <iostream.h>

void Reverse(int N)

//Displays string of length N in the reverse order

//Pre : N >= 1.

//Post: Displays N characters.

{

char Next;            //next data character

if (N <= 1)

{                    //base case

    cin >> Next;

    cout << Next;

}

else

{                    //recursive step

    cin >> Next;

    Reverse(N - 1);

    cout << Next;

}

}

3) Complete the following recursive function that calculates the

   value of a number (Base) raised to a power (Power).

   Assume that Power is positive, i.e Power>=1

      int PowerRaiser (int Base, int Power)

      {

        int Prod;

        if (Power == ______)

          Prod = ________;

        else

          Prod = ________ * ________;

        return Prod;

      }

4) What is the output of the following program?

What does function Strange compute?

    

      int Strange (int N)

      {

        int Res;

        if (N == 1) then

          Res = 0;

        else

          Res = 1 + Strange(N % 2);

        return Res;

      }

      void main()

      {

        cout << Strange(8) << "\n"

      }

5) recursive sum

int FindSum(int X[], int N)

//Finds sum of values in array X elements 0..N - 1.

//Pre : Array X is defined and N >= 0.

//Post: Returns sum of first N elements of X,

{

int Sum;

if (N == 0)

    Sum = 0;

else

    Sum = X[N - 1] + FindSum(X, N - 1);

return Sum;

}

void main()

//Tests function FindSum.

{

const MaxIndex = 20;

int Num;

int X[MaxIndex];

Num = 3;

X[0] = 5;

X[1] = 10;

X[2] = -7;

cout << "The array sum is " << FindSum(X, Num) << "\n";

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans 1

#include<iostream>

using namespace std;


int Multiply(int M, int N)

//Performs multiplication using the + operator.

//Pre : M and N are defined and N > 0.

//Post: Returns M x N

{

int Prod;

if (N == 1)

Prod = M; //base case

else

Prod = M + Multiply(M, N - 1); //recursive step

return Prod;

}
  
int main()
{
  
cout<<Multiply(5,6);

return 0;
}

=====================================================================================

Output

* 1 TDM-GCC 4.9.2 64-bit Release v DED: Chegg\C++ Chegg\recursion_multipication.cpp - [Executing] - Dev-C++ 5.11 File Edit Se

=====================================================================================

Ans 2

#include<iostream>

using namespace std;


void Reverse(int N)

//Displays string of length N in the reverse order

//Pre : N >= 1.

//Post: Displays N characters.

{

char Next; //next data character

if (N <= 1)

{ //base case

cin >> Next;

cout << Next;

}

else

{ //recursive step

cin >> Next;

Reverse(N - 1);

cout << Next;

}

}
  
int main()
{
  
Reverse(5);  
return 0;
}

=====================================================================================

Output

:: O : - 0 x DED: Chegg\C++ Chegg\recursion_string_rev1.cpp - [Executing] - Dev-C++ 5.11 File Edit Search View Project Execut

=====================================================================================

Ans 3:

#include<iostream>

using namespace std;

/*
Complete the following recursive function that calculates the

value of a number (Base) raised to a power (Power).

Assume that Power is positive, i.e Power>=1

*/
int PowerRaiser (int Base, int Power)

{

int Prod;

if (Power == 1)

Prod = Base;

else

Prod = Base *PowerRaiser(Base,Power-1);

return Prod;

}
  

  
int main()
{

cout<<PowerRaiser(5,1)<<endl;

cout<<PowerRaiser(2,6)<<endl;

return 0;
}

=====================================================================================

Output

- X v DED: Chegg\C++ Chegg\recursion_power_find.cpp - [Executing] - Dev-C++ 5.11 File Edit Search View Project Execute Tools

=====================================================================================

4) What is the output of the following program?

What does function Strange compute?

    

      int Strange (int N)

      {

        int Res;

        if (N == 1) then

          Res = 0;

        else

          Res = 1 + Strange(N % 2);

        return Res;

      }

      void main()

      {

        cout << Strange(8) << "\n";

      }

=====================================================================================

Output

it is an infinite loop.

because of

Strange(8) = 1+ Strange(0) = 1+1+Strange(0) = 1+1+1+Strange(0)......................

for even number it will go in the infinite loop and nothing will be printed on the console.

=====================================================================================

Ans 5:

#include<iostream>

using namespace std;


int FindSum(int X[], int N)

//Finds sum of values in array X elements 0..N - 1.

//Pre : Array X is defined and N >= 0.

//Post: Returns sum of first N elements of X,

{

int Sum;

if (N == 0)

Sum = 0;

else

Sum = X[N - 1] + FindSum(X, N - 1);

return Sum;

}
  
int main()
{
  
const int MaxIndex = 20;

int Num;

int X[MaxIndex];

Num = 3;

X[0] = 5;

X[1] = 10;

X[2] = -7;

cout << "The array sum is " << FindSum(X, Num) << "\n";

return 0;
}

=====================================================================================

Output

DED: Chegg\C++ Chegg\recursion_array_sum.cpp - [Executing] - Dev-C++ 5.11 File Edit Search View Project Execute Tools Style W

Add a comment
Know the answer?
Add Answer to:
C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT