Question

1)List four differences between recursion and iteration. (consider: definition, termination, implementation difficulty and speed) 2)Write a...

1)List four differences between recursion and iteration. (consider: definition, termination, implementation difficulty and speed)

2)Write a recursive function/method to perform exponentiation

             return xm, assuming m >= 0. [3 marks]

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

Definition: Iteration in simple words means repeating the same process again and again. In programming terms, an iteration is a set of operation which is to be carried out until a breaking condition is met whereas Recursion means a repeated application of a given procedure, In terms of programming language it a recursive function is a function that calls itself.

Termination: An iteration gets terminated when the breaking condition is triggered. i,e, a variable reaches the breaking condition whereas a recursion gets terminated when a statement called the base case is executed. It makes the function return before the control reaching to the recursive call.

implementation: An iteration needs three conditions to start. Initialization, Condition, increment or decrement whereas A recursive call needs only a base case and recursive call to the function.

Speed: Iteration has faster execution as compared to recursion

Ans2. Since no particular language is mentioned I am writing this code in Java

public static exponentiate (int x, int m)

{

if(m == 0)

return 1;

else

return x* exponentiate(x,m-1)

}

Add a comment
Know the answer?
Add Answer to:
1)List four differences between recursion and iteration. (consider: definition, termination, implementation difficulty and speed) 2)Write a...
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++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • in c++ only. second part of the first question. M is a linked list. this question as well as 3 and 4 are built off of question 1. 2. Write a function definition to create a new list. sub, that i...

    in c++ only. second part of the first question. M is a linked list. this question as well as 3 and 4 are built off of question 1. 2. Write a function definition to create a new list. sub, that is a subset of M. Return the new list to the calling function. 2. Write a function definition to create a new list. sub, that is a subset of M. Return the new list to the calling function.

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops....

    Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops. Instructions This assignment will be different than previous assignments (and most assignments which come after it). In this assignment, you will be crafting four solutions to four different problems. This assignment will also have special requirements regarding how you may code. You are not allowed to use assignment statements. This includes using preincement, postincrement, predecrement, and postdecrement. You are allowed to use assignment to...

  • 1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark...

    1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark 1729 is O(1) b) 3 marks 2n2-4n -3 is O(n2) 2. [3 marks] Using the definition of Big-Oh, prove that 2n2(n 1) is not O(n2) 3. 6 marks Let f(n),g(n), h(n) be complexity functions. Using the definition of Big-Oh, prove the following two claims a) 3 marks Let k be a positive real constant and f(n) is O(g(n)), then k f(n) is O(g(n)) b)...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

  • What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that...

    What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...

  • 1-1. List five differences between SI engines and CI engines. 1-2. A four-stroke cycle engine mayor...

    1-1. List five differences between SI engines and CI engines. 1-2. A four-stroke cycle engine mayor may not have a pressure boost (supercharger, tur in the intake system. Why must a two-stroke cycle engine always have an intake 1-3. List two advantages of a two-stroke cycle engine over a four-stroke cycle engine. List two advantages of a four-stroke cycle engine over a two-stroke cycle engine. 1-4. (a) Why do most very small engines operate on a two-stroke cycle? (b) Why...

  • 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels contai...

    Please design the function in version 3 of python 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that represents the portion of the original image that is specified by the other four parameters. The extracted portion of the image should consist of the pixels that fall in the intersection of the rows of pixels that begin with row rmin...

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