Question

If you understand how the above recursive algorithm to compute yz works, you can turn it...

If you understand how the above recursive algorithm to compute yz works, you can turn it into a more efficient iterative algorithm that basically uses the same strategy (though it is not a tail recursive algorithm). Some parts of this iterative algorithm is given below. Fill in the blanks:

Power-iterative(y: number; z: non-negative integer)

1. answer=1

2. while z > 0

3.    if z is odd then answer=__________

4.    z = ________

5.    y = _________

6. return answer

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

Power-iterative(y: number; z: non-negative integer)

1. answer=1

2. while z > 0

3.    if z is odd then answer = answer * y

4.    z = z/2 // now number is even

5.    y = y*y // change y to y^2

6. return answer

/* C Program */

#include <stdio.h>

/* Iterative Function to calculate (x^y) in O(log y) */
int power(int x, unsigned int y)
{
   int res = 1;   // Initialize result

  
   while (y > 0)
   {
       // If y is odd, multiply x with result
       if (y % 2)
           res = res*x;

       // y must be even now
       y = y/2;
       x = x*x;
   }
   return res;
}

// Driver program to test above functions
int main()
{
int x = 2;
int y = 5;
int p = 13;
printf("Power is %u", power(x, y));
return 0;
}
/* Output */

Power is 32

Add a comment
Know the answer?
Add Answer to:
If you understand how the above recursive algorithm to compute yz works, you can turn it...
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
  • Could u plz teach me how to sketch the graph?i still can not understand it

    could u plz teach me how to sketch the graph?i still can not understand it EXAMINER: various 6. Use the function /(x), the first derivative f(z) and the second derivative /"(r) as defined 9.5 here to gather information and fill in the blanks below. (f a feature doesn't apply, urite None. 5-4r 2r-3 7-4x x-1) 2a-I u (o) Domain otDvL o) u () Symmetry o f (e) z-intercepts. X-t ()Equation(s) of any vertical asymptotes: Equation(o) of any horizontal asymptotes: ndy-coordinates...

  • The answer below is correct, but can you guys show me how to get it? I...

    The answer below is correct, but can you guys show me how to get it? I need conplete solution. thanks Consider a 16-bit floating-point representation based on the IEEE floating-point format, with one sign bit, seven exponent bits (k=7), and eight fraction bits (n:8). The exponent bias is 27-1-1-63. Fill in the table that follows for each of the numbers given, with the following instructions for each column: Hex: the four hexadecimal digits describing the encoded form. M the value...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we...

    Undecimal to decimal&decimal to undecimal #Your code here Thank you! Binary-to-Decimal In a previous lab, we considered converting a byte string to decimal. What about converting a binary string of arbitrary length to decimal? Given a binary string of an arbitrarily length k, bk-1....bi .box the decimal number can be computed by the formula 20 .bo +21.b, + ... + 2k-1. bx-1- In mathematics, we use the summation notation to write the above formula: k- 2.b; i=0) In a program,...

  • Could you please show me how I can drw those circuits. please using (NI Multisim 14)...

    Could you please show me how I can drw those circuits. please using (NI Multisim 14) Optocoupler Objectives Use an ohmmeter to determine the condition of the optoisolator. Observe the operation of an optocoupler. Determine the maximum frequency response of the optocoupler. Required Materials (1) Dual DC power supply (1) Function generator (1) Oscilloscope (2) Multimeters (1) Optocoupler (ECG3040) (1) 3.9ΚΩ resistor (1) 220 resistor Introduction An optoisolator is a hybrid integrated circuit that contains an LED on one side...

  • Can Dogs Understand Human Cues? EXPLORATION Dogs have been domesticated for about 14,000 years. In that...

    Can Dogs Understand Human Cues? EXPLORATION Dogs have been domesticated for about 14,000 years. In that time, have they been able to develop an understanding of human gestures such as pointing or glancing? How about simi lar nonhuman cues? Researchers Udell, Giglio, and Wynne tested a small number of dogs in order to answer these questions. In this exploration, we wll first see whether dogs can understand human gestures as well as nonhuman gestures. To test this, the researchers positioned...

  • This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named...

    This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named Budgeter.java. To use a Scanner for console input, you must import java.util.*; in your code. This program prompts a person for income and expense amounts, then calculates their net monthly income. Below are two example logs of execution from the program. This program’s behavior is dependent on the user input (user input is bold and underlined below to make it stand out and differentiate...

  • Can you piea se Show how 4a 2 got the answer F helps me. understand H better Thant you So much Problem 1 REGIONA...

    Can you piea se Show how 4a 2 got the answer F helps me. understand H better Thant you So much Problem 1 REGIONAL AIRLINES Regional Airlines is establishing a new telephone system for handling flight reservations. During the 10:00 A.M. to 11:00 A.M. time period, calls to the reservation agent occur ran- domly at an average of one call every 3.75 minutes. Historical service time data show that a reservation agent spends an average of 3 minutes with each...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

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