Question: Give a recursive implement to the following functions: a. void printTriangle(int n) This function is given a positive integer n, and (aligned to the left)
Give a recursive implement to the following functions:
a. void printTriangle(int n)
This function is given a positive integer n, and
(aligned to the left) made of n lines with asterisks.
prints a textual image of a right triangle
For example,
printTriangle
, should print:
*
**
***
****
b. void printOpositeTriangles(int n)
This function is given a positive integer n, and
prints a textual image of a two opposite right
triangles (aligned to the left) with asterisks, each containing n lines.
For example,
printOpositeTriangles (4) should print:
****
***
**
*
*
**
***
****
(c) void printRuler(int n)
This function is given a positive integer n, and prints a vertical ruler of 2" 1 lines. Each line
contains - marks as follows:
The line in the middle (1/2) of the ruler contains n - marks
The lines at the middle of each half (1/4) and (3/4) of the ruler contains (n-1) - marks ''
The lines at the 1/8,3/8,5/8 and 7/8 of the ruler contains (n-2) '-' marks
And so on ...
The lines at the 1/2k,3/2k ,5/2k......,2k-1/2k of the ruler contains 1 '-' mark
For example,
printRuler(4), should print (only the blue marks):
-
- -
-
- - -
-
- -
-
- - - -
-
- -
-
- - -
-
- -
-
Hints:
Take for n=4: when finding print_ruler(4), try to think first what
print_ruler(3) does, and how you can use it to print a ruler of size 4.
Then, generally identify what print_ruler(n-1) is supposed to print, and use that
in order to define how to print the ruler of size n.
You may want to have more than one recursive call
It looks much scarier than it actually is
1.
2. 3
In C++ please
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
