Assume the availability of a method named makeLine that can be passed a non-nega
ID: 3815835 • Letter: A
Question
Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identical characters that are all equal to c. Write a method named print Triangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, its value is raised to the next odd number (e.g. 4 rightarrow 5). Then, when k has the value zero, the method prints a SYMMETRIC triangle of O's (the capital letter O) as follows: first a line of n O's, followed by a line of n-2 O's (indented by one space), and then a line of n-4 O's (indented by two spaces), and so on. For example, if the method received 5, 0 (or 4, 0) it would print: OOOOO OOO O The method must not use a loop of any kind (for, while, do-while) to accomplish its job. The method should invoke makeline to accomplish the task of creating Strings of varying lengths.Explanation / Answer
#include <stdio.h>
void makeLine(int n, char c)
{
if (n == 0)
{
return;
}
printf("%c", c);
makeLine(n-1, c);
}
void printSymetricTriangle(int n, int m)
{
if (n < 0)
{
return;
}
makeLine(m, ' ');
makeLine(n, 'O');
makeLine(m, ' ');
printf(" ");
printSymetricTriangle(n-2, m+1);
}
void printTriangle(int n, int k)
{
if (n < 0)
{
return;
}
if (n % 2 == 0)
{
n = n+1;
}
printSymetricTriangle(n, 0);
}
int main()
{
printTriangle(14, 0);
return 0;
}
As for k apart from 0 is our choice. I choose it to also print a symettric triagle.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.