rec.c 967 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int recfact(int x){
  4. int y = 1;
  5. if (x==1){
  6. return y;
  7. } else {
  8. return y *= x *recfact(x-1);
  9. }
  10. }
  11. int recfor(int x){
  12. int gaga = 1;
  13. for (int i = 1; i < x;) {
  14. gaga *= i++;
  15. }
  16. return gaga;
  17. }
  18. /*
  19. * i = 1, gaga = 1*2
  20. * i = 2, gaga = 1*2*3 = 6
  21. * i = 3, 1*2*3*4 = 24
  22. *
  23. * wrong loop time condition
  24. * wrong calculation
  25. *
  26. * ---
  27. *
  28. * recursion
  29. *
  30. * forgot to assign, forgot a number (multiplied by 0)
  31. * misunderstood calculation
  32. wel exploren, maar wel naar een doel.
  33. kijken naar auxililiaries.
  34. curiosity wss, leren vragen. leren wat ik niet heb, weten wat ik niet weet.
  35. zoektocht heeft wat sturing nodig.
  36. hoe ik mijn probeer stel/vind
  37. het probleem onderzoeken
  38. limiteer de pogingen
  39. hardware / networking
  40. * * */
  41. int main () {
  42. int i = 6; // we can not handle negative numbers
  43. printf("%d\n", recfor(i));
  44. printf("%d\n", recfact(i));
  45. return 0;
  46. }