Hello.
i wanted to generate the following series(pattern).
The captain of TITANIC is a mathematics freak. He has recently been given a problem that he is unable to solve. And he has asked your help to solve it.
There are n numbers in a given expression.
X1 X2 X3 .... Xn
What is the number of ways to put parenthesis in the expression.
For n=4, the different ways are
(((x1.x2).x3).x4)
((x1.x2).(x3.x4))
(x1.((x2.x3).x4))
(x1.(x2.(x3.x4)))
((x1.(x2.x3)).x4)
The problem asks for the count(which is a Catalan series).But i wanted to generate the pattern. Can anyone please help? This is what i tried:
Well, im not good at recursion and im finding this hard to solve.im not able to obtain a solution.
Can anyone please help?
#include <stdio.h>
#define high 4
char arr[3] = {'X', 'Y', 'Z'};
void Generate(int num)
{
int i;
if (num >= high) {
return;
} else if (num == 2) {
printf("(%c, %c)", arr[0], arr[1]);
} else if ((high - num) == 3) {
printf("(%c, %c)", arr[high - 3], arr[high - 2]);
} else {
for (i = 1; i < high; i++) {
printf("(");
Generate(num + 1);
printf(")\n");
}
}
}
int main(void)
{
Generate(0);
return 0;
}