i am trying to write a program that gets input step
and prints the following in LATEX format .(https://i.sstatic.net/cF4Kc9gY.png)](https://i.sstatic.net/cF4Kc9gY.png)
for example for step = 3
, the output is 1+\frac{2+\frac{4}{5}}{3+\frac{6}{7}}
this is the code i have write now but i get *** stack smashing detected ***: terminated
error for inputs more than 6
. any idea how to manage this ? (at least for inputs lower than 10)
#include <stdio.h>
#include <stdlib.h>
void frac(int start, int step, char *result) {
if (step == 1) {
sprintf(result, "%d", start);
} else {
char left[256];
char right[256];
frac(2 * start, step - 1, left);
frac(2 * start + 1, step - 1, right);
sprintf(result, "%d+\\frac{%s}{%s}", start, left, right);
}
}
int main() {
int step;
scanf("%d", &step);
char result[1024];
int start = 1;
frac(start, step, result);
printf("%s\n", result);
return 0;
}
You need to sign in to view this answers
Leave feedback about this