Question

PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code.

EXISITNG CODE:

int Exponentiation(int X, int Y) {
    if (Y == 0) // base case
        return 1;
    else    // recursive case
        return X * Exponentiation(X, Y - 1);    //make recursive call
}
int Multiply(int X, int Y){
    if(Y == 0){
        return 0;
    }
    else{
        return X + Multiply(X, Y-1);
    }
}
int MultiplyRange(int X, int Y){
    if(Y == 0){
        return 0; 
    }
    else{
        return X + MultiplyRange(X, Y-1);
    }
}
int Subtraction(int X, int Y) {
    if (Y == 0) // base case
        return X;
    else    // recursive case
        return Subtraction(X, Y - 1) - 1;    //make recursive call
}
int Addition(int X, int Y) {
    if (Y == 0) // base case
        return X;
    else    // recursive case
        return 1 + Addition(X, Y - 1);    //make recursive call
}
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 ( int min, int max)

{

if (min == max){
//base case

return min;
}

else{
//recursive case

return max + sumRange(min,max-1);

}

}

}

int sumDigits(int n)

{

int rightDigit = n%10;

int remainDigits = n / 10;

if(n < 10)

{

// Base case

return n;

}

else

{

return sumDigits(remainDigits) + rightDigit;

}

}

 
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int Division(int X, int Y) {
    if (X < Y) // base case
        return 0;
    else    // recursive case
        return 1 + Division(X-Y, Y);    //make recursive call
}

int main() {
    cout << Division(8, 3) << endl;  // prints 2
    return 0;
}

Please upvote this answer. Let me know if you have doubts..

Add a comment
Know the answer?
Add Answer to:
PROBLEM: Write a recursive method named Division that takes two integers X and Y returns 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
  • In C++, write a recursive function power that takes two positive integers base and n as...

    In C++, write a recursive function power that takes two positive integers base and n as inputs and computes base to the n power. Example power(3, 2) is 9. Do not use any loops in your program. This is what I got so far but I'm not sure what I'm doing wrong: #include <iostream> using namespace std; int calc(int x, int y); int main() {       calc(2, 3);    return 0; } int calc() {          cout...

  • Write a recursive function named multiply that takes two positive integers as parameters and returns the...

    Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...

  • In a recursive solution, the base case terminates the recursive processing. In the below code, which...

    In a recursive solution, the base case terminates the recursive processing. In the below code, which of the following calls to recurse() would stop further recursive calls? #include <iostream> void recurse(int x, int y) { if (y > 0) { x++; y--; std::cout << x << " " << y << " "; recurse(x, y); std::cout << x << " " << y << std::endl; } } 1recurse(1, 2); 2recurse(1, 0); 3recurse(1, 1); 4recurse(0, 1);

  • 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...

  • in JAVA 24. Suppose that a class has an overloaded method named add with the following...

    in JAVA 24. Suppose that a class has an overloaded method named add with the following two implementations: double add (int x, double y) { return x + y; } double add (double x, int y) { return x + y + 1; } What, if anything, will be returned by the following method calls? A. add(3, 3.14) B. (3.14, 3) C. add (3, 3) D. add (3.14, 3.14) 29. Here is the code for a recursive method named mystery....

  • Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...

    Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display “heads.” If the random number is 2, the function should display “tails.” Demonstrate the function in a program that asks the user how many times the coin should be tossed and then simulates the tossing of the coin that...

  • This code is based on creating a recursive funciton. You cannot alter the code besides adding...

    This code is based on creating a recursive funciton. You cannot alter the code besides adding your solution in the particular area. Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120 #include void PrintFactorial(int factCounter, int factValue){    int nextCounter = 0;    int nextValue = 0;    if (factCounter == 0) {            // Base case: 0! = 1       printf("1\n");    }...

  • CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple...

    CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple calculator which performs the following operations: • Addition if the string is "+" • Difference if the string is "-" Multiplication if the string is "*" • Return quotient (obtained by dividing the first number by the second) if the string is "/" • Return greater value if the string is "max" • Return lesser value if the string is "min" solution.cpp 2 Create...

  • a) Make a function which returns x/3 with a given integer x. double third(int x) {...

    a) Make a function which returns x/3 with a given integer x. double third(int x) { //Complete your code below. ______________________________________ return y; } b) Make a program which shows 2 ∗ n with a given user input n. #include<iostream> using namespace std; // Complete the blank. _______________________ int main(){ int n; cout<<"Enter:"; cin>>n; cout<<twice(n); return 0; } int twice(int x){ return 2*x; } c) Make a function which returns true if n is positive, otherwise returns false. ________ positive(int...

  • 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...

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