You are on page 1of 2

Data Structures and Algorithms CMP210

LAB-6 (Recursions)
Total Marks (8*5 = 40 + bonus(10))
Note:
Try to solve as many problems as you can.
Discussions with your fellows are not allowed.

1. Write a recursive function int TOH_Moves(int n, int start, int temp, int goal) that takes the
same arguments as TOH but instead of printing out the solution, returns the number of disc moves
in solving the problem.
2. Write a recursive function int count_character(char *str, char c) that counts the number of
charcter c in string str.
3. Write a recursive function void replace(char *s, char from, char to);that changes all occurrences
of from in s to to. For example, if s were "steve", and from == 'e' and to == 'a', s would
become "stava".
4. Write a function to recursively print out an integer in any base from base 2 to base 9.
5. A Pattern of Letters
Write a recursive function: void letters (char c) where c is one of the characters 'A' through 'Z'.
The method has printed a pattern of letters as follows:
1. If the parameter c is 'A', then the output is 'A'.
2. For other values of c, the output consists of three parts:
-- the output for the previous letter (c-1);
-- followed by the letter c itself;
-- followed by a second copy of the output for the previous letter.
Example output:
letters('C') will print ABACABA
letters('D') will print ABACABADABACABA
6. Consider the following innite sequence of positive integers, with the rst ten terms shown: 0, 1,
3, 6, 10, 15, 21, 28, 36, 45, . . . Write down a recursive denition of this sequence. And then write
a recursive function to find nth number of the series.
7. Write a function char* reverseString(char* str) that takes in a string str and returns a string
reverse of it. You can use function strlen to find length of a string, so you dont need to pass it
along parameter.

8. Write a recursive function void printHourGlass(int n, int b) where n is an odd number. It prints
pattern of asterisks that looks like an hour glass. It prints n asterisks at first line, n-2 asterisks at
second line and so on till it reaches the line with 1 asterisk and then starts printing the pattern
backwards. The parameter b is the number of blank spaces that should be printed before the first
and the last line of the hour glass.
For example: printHourGlass(5, 0) should print following pattern.
* * * * *
* * *
*
*
* * *
* * * * *

9. Fractal Design (Bonus: 10 points)


Fractals are typically self-similar patterns, where self-similar means they are "the same from near
as from far".
Examine this pattern of asterisks and blanks, and write a recursive function that can generate
patterns such as this:
*
* *
*
* * * *
*
* *
*
* * * * * * * *
*
* *
*
* * * *
*
* *
*

With recursive thinking, the function needs only seven or eight lines of code (including two
recursive calls). Your function prototype should look like this void pattern(int n, int b), where n
is power of 2 e.g. 1,2,4,8, or 16 etc. You may assume n is always a power of 2 and do not need to
check it. The longest line of pattern has n asterisks. The parameter b is the number of blank
spaces that should appear before printing n asterisks in a line.
The above pattern is produced by the call: pattern (8, 0)

You might also like