Alg Ch1 Part3
Alg Ch1 Part3
Assignment Rules:
• Tip: Before diving into your assignment, take the time to thoroughly read and understand the rules and
instructions. This ensures that your submission is comprehensive and accurate.
cout << "Number of bits for unsigned long long: " << sizeof(unsigned long long) * 8 << " bits" << endl;
Cont. Recursion
Exponentiation (xy) with Recursive Method
Exponentiation, represented as xy, is a fundamental mathematical operation where x is the base and y is the
exponent. When dealing with exponentiation through a recursive method, we break down the problem into
smaller subproblems. Assume x can be of type double and y is of type integer. The recursive approach involves
two main cases:
1) Base case: If the exponent (y) is 0, the result is always 1. [x0 is always 1]
2) Recursive Case: If y is greater than 0, we recursively calculate x^(y-1) and multiply the result by x.
function recursive_power(x, y)
// Base case
if y==0
return 1
// Recursive case: x^y = x * x^(y-1)
return x * recursive_power(x, y - 1)
Example
Given the following function:
int fun ( int x )
{
if ( x )
return 2 + fun (x – 1);
else
return 1;
}
What is the output for the following code (show your work):
int x = 3 , result;
result = fun ( x );
cout << result;
Example
For each call below, indicate what value is returned: [show your work]
mystery ( 6 , 13 )
mystery (14 , 10)
Example
• What is the result of the following function when a=12 and b=9? Show your work.
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding
ones, usually starting with 0 and 1. The sequence goes:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ….. and so on.
1. Setup the recurrence relation F(n) that define the sequence of Fibonacci numbers.
2. Write a pseudocode for the the recursive function fib(n) to calculate the nth Fibonacci number.
3. Implement the recursive function fib(n) ion c++ . Observe the function's behavior for larger values
Recall: gcd (a , b), the greatest common divisor of two nonnegative, not both zero integers a and b
Exercise
Write pseudocode for a recursive function to find the sum of digits of a given
number.
Example: n = 135 sum of digits = 9
Exercise
In the lecture, we discussed a method to raise a double to an integer power. In this question, write a
recursive function that allows raising to a negative integer power as well.
Exercise
Example:
Input String: "algorithm"
Output (Reversed): "mhtirogla"
Exercise
Design a recursive function named displayVertically(n), that displays the digits
of the given number nonnegative integer n vertically on the screen.
Advanced topics:
• Any task that can be accomplished using recursion can also be done in some other
way without using recursion (nonrecursive version).
• The recursive version may use more storage and run somewhat slower, because the
computer must do a good deal of work manipulating the stack in order to keep track
of the recursion.
• However, since the system does all this for you automatically, using recursion can
sometimes make your job as a programmer easier and can sometimes produce code
that is easier to understand
Interactive tools and simulations
some websites that provide interactive tools and simulations to help students
understand recursion and the use of the call stack:
• Website: http://pythontutor.com
2. Visualgo.net: offers visualizations for various data structures and algorithms, including
recursion. It allows you to see how the call stack works for different
programming languages.
• Website: https://visualgo.net/en/recursion