REKURZIJA
To je postupak gdje funkcija uzastopno poziva
samu sebe. Tipične primjene rekurzije su u igrama, sortirajućim stablima
i listama.
Razmotrimo računanje 6!( 6 faktorijela )
6! = 6 * 5 * 4 * 3 * 2 * 1 6! = 6 * 5! 6! = 6 * ( 6 - 1 )! n! = n * ( n - 1 )!
/* loš primjer za prikaz rekurzije */ #include <stdio.h> long int faktorijel( long int ); /* prototip funkcije */ long int faktorijel( long int n ) { long int rezultat; if( n == 0L ) rezultat = 1L; else rezultat = n * faktorijel( n - 1L ); return ( rezultat ); } main() { int j; for( j = 0; j < 11; ++j ) printf("%2d! = %ld\n", faktorijel( (long) j) ); }
VJEŽBA C19
Ponovo napiši primjer
c9 tako da upotrijebiš rekurzivnu funkciju.