I wanted to ask why the first code functions and the second is not? Both are recursions and should calculate the binomial coefficient when going up the pascal triangel. Thanks.
#include <stdio.h>
#include <stdlib.h>
int rekurs(int n, int k)
{ if((k==0)||(n==0)||(n==k))
{
return 1;
}
else{
return rekurs(n-1,k-1)+ rekurs(n-1,k);
}
}
int main(int argc, const char *argv[])
{
int spalte = atoi(argv[1]);
int zeile= atoi(argv[2]);
printf("%d,%d",zahlen,rekurs(spalte,zeile));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int rekurs(int n, int k)
{ int ergebnis;
if((k==0)||(n==0)||(n==k))
{
ergebnis = 1;
}
else{
return ergebnis = rekurs(n-1,k-1)+ rekurs(n-1,k);
}
}
int main(int argc, const char *argv[])
{
int spalte = atoi(argv[1]);
int zeile= atoi(argv[2]);
printf("%d,%d",zahlen,rekurs(spalte,zeile));
return 0;
}
returnif theifcondition is true. Assigningergebnisin theelsecase is also pointless.rekursa brand newergebnisvariable will created for that call and for that call only. When the function returns the variable reaches the end of its life-time and it ceases to exist and the value in it is lost.-Wall -Wextra -pedantic -Werror.) It would have caught your error.