0% found this document useful (0 votes)
6 views2 pages

TD 2 Recursion

This document outlines a tutorial for the ADS2 Algorithms and Data Structure 2 module at Kasdi Merbah Ouargla University, focusing on recursion exercises. It includes various programming tasks such as calculating factorials, Fibonacci numbers, power functions, and string manipulations using recursive functions. Additionally, it explores concepts like GCD, octal conversion, and unique permutations while emphasizing recursive problem-solving techniques.

Uploaded by

htm9958
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

TD 2 Recursion

This document outlines a tutorial for the ADS2 Algorithms and Data Structure 2 module at Kasdi Merbah Ouargla University, focusing on recursion exercises. It includes various programming tasks such as calculating factorials, Fibonacci numbers, power functions, and string manipulations using recursive functions. Additionally, it explores concepts like GCD, octal conversion, and unique permutations while emphasizing recursive problem-solving techniques.

Uploaded by

htm9958
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Kasdi Merbah Ouargla University

Faculty of New technologies and information and communication


Computer Science and Information Technology Department

Module: ADS2 Algorithms and Data Structure 2


Teachers: BELHADJ Mourad, MEISSA Marwa & KORICHI Aicha
Academic year: 2024/2025
Class: ING1 Computer engineering
Type: Tutorial

TD 02: Recursion

Exercise 1 3. Write a recursive function to find the nth num-


ber in the Fibonacci sequence. The Fibonacci
1. Write a recursive function Fact(n) to compute sequence is defined as:
the factorial of a number n. The factorial of a
number n is the product of all positive integers 
less than or equal to n: 0
 if n = 0
F (n) = 1 if n = 1
F (n − 1) + F (n − 2) if n > 1


(
1 if n = 0 or n = 1
n! =
n × (n − 1)! if n > 1
Exercise 3
2. Write a recursive function Power(x,n) to calcu-
late xn , where x is a base and n is an exponent. 1. Write a recursive function Power(x, n) that
The function should compute x raised to the calculates the power of a floating-point number
power n. x raised to the power of n (i.e., xn ), where x is
3. Write a recursive function Reverse(s) to re- a float and n is an integer. The function should
verse a given string. The function should return handle both positive and negative exponents.
a char array with the characters of the input
string reversed. For example:
if n = 0

"hello" → "olleh" 1

P ower(x, n) = x × P ower(x, n − 1) if n > 0
1
if n < 0


Exercise 2 P ower(x,−n)

2. Write a recursive function to calculate the sum


1. Write a head-recursive function to print all
of elements in an array of integers. The func-
numbers from 1 to n. In head recursion, the re-
tion should take the array and its size as param-
cursive call is made before performing any other
eters. Provide both a function and a recursive
operations.
procedure.
2. Write a tail-recursive function to calculate the
3. Write a recursive function CountVowels(s, n)
Greatest Common Divisor (GCD) of two given
that counts the number of vowels in a given
numbers using Euclid’s Algorithm. The GCD
string s of length n. The function should return
of two numbers a and b can be calculated using
the count of vowels (a, e, i, o, u, both uppercase
the following property:
and lowercase) present in the string.

(
a if b = 0
gcd(a, b) =
gcd(b, a mod b) if b > 0
Exercise 4 Example:
Input: "aba"
1. Write a recursive function DecimalToOctal(n) The possible unique permutations are:
that converts a decimal number n to its octal
representation. The function should return an
aab, aba, baa
integer representing the octal equivalent of n.
2. Write a recursive function SumOfOctalDigits(n) Hint:
that computes the sum of the digits of the oc-
tal representation of n. Instead of directly • Sort the string before passing it to the recursive
computing the sum, this function should call function using qsort() from stdlib.h.
DecimalToOctal(n) to obtain the octal num-
ber and then recursively sum its digits. • Modify the recursive function to skip duplicate
characters when generating permutations.
3. Write a recursive function IsSumEven(n) that
determines whether the sum of the digits of the
octal representation of n is even or odd. Instead Exercise 6
of computing the sum directly, this function
should call SumOfOctalDigits(n) and return In an e-commerce system, there are two processes
true if the sum is even and false otherwise. that depend on each other and recursively call each
other in an alternating fashion. Consider two func-
Example: For n = (67)10 (decimal) tions, CheckOrder() and UpdateInventory(), as fol-
lows:
• Convert to octal using DecimalToOctal(67):
CheckOrder(orderID): This function checks if an or-
(67)10 = (103)8 der is valid. If the order ID is even, the order is con-
sidered valid. If the order ID is odd, the order is
• Compute the sum of the octal digits using considered invalid.
SumOfOctalDigits(67): UpdateInventory(orderID): This function updates
the inventory by decreasing it by 1 for each valid
1+0+3=4
order. After updating the inventory, it calls the
CheckOrder() function to check the validity of the
• Check if the sum is even using IsSumEven(67):
order again.
4 is even ⇒ true These two functions call each other recursively, alter-
nating between checking the order and updating the
inventory. The recursion continues until the order is
Exercise 5 processed or deemed invalid.
Using the recursive function Permutations(s) de-
fined in the previous exercise, write a new function (
UniquePermutations(s) that generates and displays valid order if order exists
CheckOrder(orderID) =
only the unique permutations of a given string s, even invalid order otherwise
if it contains duplicate characters.
To achieve this, you should use the C standard li-
brary function qsort() to sort the input string be- (
fore generating permutations, ensuring that duplicate U pdateInventory(orderID) = reduce stock if valid
permutations are not printed multiple times. alert if invalid

You might also like