fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. //Maciej Januszewski, III semestr, PS2
  5.  
  6. //Wyznacza wartość symbolu Newtona z definicji(iteracyjnie).
  7. //Najpierw trzeba zdefiniować własność silni.
  8. long silnia(int a)
  9. {
  10. long s;
  11.  
  12. //Z własności wiemy, że 1!=1 oraz 0! = 1
  13. if (a == 0 || a == 1)
  14. {
  15. return 1;
  16. }
  17. else
  18. {
  19. s = 1;
  20. for (int i = 1; i <= a; i++)
  21. {
  22. s *= i;
  23. }
  24. }
  25. return s;
  26. }
  27.  
  28. //Mając daną już własność silni, korzystam ze wzoru.
  29. long newton(int n, int k)
  30. {
  31. return silnia(n)/(silnia(k)*silnia(n-k));
  32. }
  33.  
  34. //Wyznacza wartość symbolu Newtona rekurencyjnie ze wzoru.
  35. unsigned long int newton_rek(long int n ,long int k)
  36. {
  37. if ( n == k || k == 0 )
  38. {
  39. return 1;
  40. }
  41.  
  42. if (k > n)
  43. {
  44. return 0;
  45. }
  46.  
  47. else return newton_rek(n-1,k-1) + newton_rek(n-1,k);
  48. }
  49. //Program główny.
  50. int main()
  51. {
  52. int n = 0;
  53. int k = 0;
  54. long funkcja1 = 0;
  55. long funkcja2 = 0;
  56.  
  57. FILE *f = fopen("In0101.txt", "r+");
  58. if (f == NULL)
  59. {
  60. printf("Nie udalo sie otworzyc pliku In0101.txt\n");
  61. return 1;
  62. }
  63. fread(n, sizeof(long), 1 , f);
  64. fread(k, sizeof(long), 1 , f);
  65. fclose(f);
  66.  
  67. FILE *ff = fopen("Out0101.txt", "w+");
  68.  
  69. if (ff == NULL)
  70. {
  71. printf("Nie udalo sie otworzyc pliku Out0101.txt\n");
  72. return 1;
  73. }
  74.  
  75. funkcja1 = newton(n,k);
  76. funkcja2 = newton_rek(n,k);
  77. fprintf( ff, "%d %d\n", funkcja1, funkcja2 );
  78. fclose(f);
  79.  
  80. return 0;
  81. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘silnia’:
prog.c:20: error: ‘for’ loop initial declaration used outside C99 mode
prog.c: In function ‘main’:
prog.c:63: warning: passing argument 1 of ‘fread’ makes pointer from integer without a cast
prog.c:64: warning: passing argument 1 of ‘fread’ makes pointer from integer without a cast
prog.c:77: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’
prog.c:77: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
prog.c:63: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result
prog.c:64: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result
stdout
Standard output is empty