Question

In: Computer Science

Now we need to develop a recursive algorithm for multiplication operation with two positive integers without...

  1. Now we need to develop a recursive algorithm for multiplication operation with two positive integers without using the multiplication operator. Your answer should be in pseudocode or Java code format.
  2. Then use the Three-Question Approach to verify your recursive algorithm. No coding is required for this assignment
  3. 1. The Base-Case Question Is   there   a   nonrecursive   way   out   of   the   algorithm,   and   does  
    the   algorithm   work   correctly   for   this   base   case?
    2. The Smaller-Caller Question Does   each   recursive   call   to   the   algorithm   involve   a  
    smaller   case   of   the   original   problem,   leading   inescapably   to   the   base   case?
    3. The General-Case Question Assuming   the   recursive   call(s)   to   the   smaller   case(s)  
    works   correctly,   does   the   algorithm   work   correctly   for   the   general   case?  
  4. Recursive method header given as: int multiplication (int a, int b)

Solutions

Expert Solution

GIven
int multiplication(int a, int b)
The algorithm has to be written without multiplication operator
so, Let's understand the basics of multilplication
Example:
3 * 5 means adding 3 for 5 times which is 3 + 3 + 3 + 3 + 3 = 15
So a * b will be adding a for b times a + a + a +...... b times
Recursive pseudocode for given algorithm
int multiplication(int a, int b)
{
   if(b == 1)
   {
       return a;
   }
   else
   {
       return a + multiplication(a, b - 1);
   }
}
1. The Base-Case Question Is there a nonrecursive way out of the algorithm, and does the algorithm work correctly for this base case?
Yes, The algorithm has a nonrecursive solution. Iterate from 1 to b at each iteration add a to the output then finally it will produce a * b.
In recursive solution The changes in variables like increment and decrement will be called as arguments. So In this algorithm the argument b will start from b, it will be called until b = 1, So the base case is b = 1 where the recursion will end.

2. The Smaller-Caller Question Does each recursive call to the algorithm involve a smaller case of the original problem, leading inescapably to the base case?
The smaller case for the current algorithm is adding a to the previous solution at each recursive call and decrementing the argument b, so that it will reach the base case.

3.3. The General-Case Question Assuming the recursive call(s) to the smaller case(s) works correctly, does the algorithm work correctly for the general case?
Yes, Since the algorithm is working for smaller cases, at each iterataion adding previously returned output and finally at the base case it is returning a, So the algorithm will produce product of any given arguments a and b.


Related Solutions

2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
Implement and complement the recursive code to compute the product of two positive integers, x and...
Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product. Hint: consider the following: 6 x 1 = 6 6 x 2 = 6 + (6 x 1) 6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] =...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based on the fact that xy = 2(x · (y/2)) when y is even and xy = 2(x · ⌊y/2⌋) + x when y is odd, together with the initial condition xy = 0 when y = 0.
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 *...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an...
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an m-bit number and y is an n-bit number (10 points), and analyze the time complexity of this algorithm (10 points).
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD ** Type a positive integer for...
Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD ** Type a positive integer for A: -2 Sorry, you must enter a positive integer (>0). Please try again. ** Recursive Euclidean Algorithm to calculate GCD ** Type a positive integer for A: 35 Type a positive integer for B: 63 The GCD is 7 Write a MIPS assembly program that prompts the user for 2 positive integers (>0). Then it uses the Recursive Euclidean Algorithm to calculate GCD (the...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT