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
  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • c++ #include using namespace std; int main() {    int n, x, num, j = 0;...

    c++ #include using namespace std; int main() {    int n, x, num, j = 0;    cin >> n >> x;    int*arr = new int[n];    for (int i = 0; i < n; i++)    {        cin >> num;        if (num < x)        {            arr[j] = num;            j++;        }    }    for (int i = 0; i < j; i++)    {...

  • Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int...

    Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • QUESTION 26 Given the following function: int secret(int num, int m) inti, prod=1; if (m=0) return...

    QUESTION 26 Given the following function: int secret(int num, int m) inti, prod=1; if (m=0) return 1: - for (i=0; i<m; i++) { prod = prod * num; } return prod; What is the output for this function call? cout << secret(10,6);

  • PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their...

    PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their sum. Please comment on every line of code. EXISTING CODE: int AddToN(int n) { if (n == 0) { // base case return 0; } else { // recursive case return n + AddToN(n-1); //make recursive call } } void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange (...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return...

    C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return next character (in ASCII order) // which is a vowel, starting with parameter // If there are no more vowels, return null character ('\0') char nextVowel(char start) { // recursive call return nextVowel(start + 1); // base case if (start > 'z') return '\0'; // incorrect base case if ('a' == start || 'i' == start || 'o' == start || 'u' == start)...

  • How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int...

    How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please...

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