Vous êtes sur la page 1sur 3

Data Structures and Algorithms CMP210

(Practice of Recursions)
Note:
Try to solve as many problems as you can on visual studio.
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 base 2
5. Write a function to recursively print out an integer in any base ranging from, provided in parameter
base 2 to base 9.

6. 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
7. 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.
8. 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.

9. 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.
* * * * *
* * *
*
*
* * *
* * * * *

10. Write void printTriangle(int n, int m), where pre condition is m<=n. The first line contains m
asterisks, the next line contains m+1 asterisks, and so on up to a line with n asterisks. Then the
pattern is repeated backwards, going n back down to m.
For Example output of call printTriangle (5, 3) should be
***
****
*****
*****
****
***

11. Write void printDiamond(int n, int m, int b), where pre condition is m<=n. and both m and n are
odd. Similar in logic to previous problem it prints a diamond shape this time. Parameter b is the
number of black spaces that should be printed before a line.
For Example output of call printDiamond (7, 1, 0) should be
*
***
*****
*******
*****
***
*

And call printDiamond(7, 5, 0) should output


*****
*******
*****

12.Fractal Design (Youre good in recursions if you can solve this problem)

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)

Vous aimerez peut-être aussi