c - Convert function iterative to recursive -
this question has answer here:
- c programming - 2 loops recursion 3 answers
how can pass function of iterative recursive
void triangulo(int max) { int i, j; for(i = 1; <= max; i++) { for(j = 0; j < i; j++) printf("%d", j + 1); printf("\n"); } }
anyone has idea? or impossible this?
update
void triangulo(int n, int i, int j) { if(i <= n) { if(j < i) { printf("%d", j + 1); triangulo(n, i, j + 1); } if (j == i) { printf("\n"); triangulo(n, + 1, 0); } } }
try : here make i
, j
arguments because in each recursion have different values.
void triangulo(int max,int i,int j) //make , j arguments { if(i<max) { if(j<max) { printf("%d",j); triangulo(max, i, ++j);//dont use post increment operation } else { printf("\n"); triangulo(max , ++i, 0); } } }
generally,
- use
if
,else
condition and - send arguments @ end of may happen in iterative method.
Comments
Post a Comment