Advertisement
milanmetal

Golubova Rekurzija

Dec 18th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. float population_growth(int years, float growth, float *population) {
  4.   if (years == 0) {
  5.     return *population;
  6.   } else {
  7.     *population += growth * population_growth(years-1, growth, population);
  8.     printf("Year: %d | Population += %.2f * %.2f\n", years, growth, *population);
  9.  
  10.     return *population;
  11.   }
  12. }
  13.  
  14. int main(){
  15.   float population = 1000000.0;
  16.  
  17.   // annual growth
  18.   float growth = 0.05;
  19.   int years = 70;
  20.  
  21.   population_growth(years, growth, &population);
  22.   printf("Final population: %.2f", population);
  23.  
  24. }
  25.  
  26. /*
  27. milan@milan:/home/milan/algorithms$ gcc -o golub golub.c
  28. milan@milan:/home/milan/algorithms$ ./golub
  29. Year: 1 | Population += 0.05 * 1050000.00
  30. Year: 2 | Population += 0.05 * 1102500.00
  31. Year: 3 | Population += 0.05 * 1157625.00
  32. Year: 4 | Population += 0.05 * 1215506.25
  33. Year: 5 | Population += 0.05 * 1276281.50
  34. Year: 6 | Population += 0.05 * 1340095.62
  35. Year: 7 | Population += 0.05 * 1407100.38
  36. Year: 8 | Population += 0.05 * 1477455.38
  37. Year: 9 | Population += 0.05 * 1551328.12
  38. Year: 10 | Population += 0.05 * 1628894.50
  39. Year: 11 | Population += 0.05 * 1710339.25
  40. Year: 12 | Population += 0.05 * 1795856.25
  41. Year: 13 | Population += 0.05 * 1885649.00
  42. Year: 14 | Population += 0.05 * 1979931.50
  43. Year: 15 | Population += 0.05 * 2078928.12
  44. Year: 16 | Population += 0.05 * 2182874.50
  45. Year: 17 | Population += 0.05 * 2292018.25
  46. Year: 18 | Population += 0.05 * 2406619.25
  47. Year: 19 | Population += 0.05 * 2526950.25
  48. Year: 20 | Population += 0.05 * 2653297.75
  49. Year: 21 | Population += 0.05 * 2785962.75
  50. Year: 22 | Population += 0.05 * 2925261.00
  51. Year: 23 | Population += 0.05 * 3071524.00
  52. Year: 24 | Population += 0.05 * 3225100.25
  53. Year: 25 | Population += 0.05 * 3386355.25
  54. Year: 26 | Population += 0.05 * 3555673.00
  55. Year: 27 | Population += 0.05 * 3733456.75
  56. Year: 28 | Population += 0.05 * 3920129.50
  57. Year: 29 | Population += 0.05 * 4116136.00
  58. Year: 30 | Population += 0.05 * 4321943.00
  59. Year: 31 | Population += 0.05 * 4538040.00
  60. Year: 32 | Population += 0.05 * 4764942.00
  61. Year: 33 | Population += 0.05 * 5003189.00
  62. Year: 34 | Population += 0.05 * 5253348.50
  63. Year: 35 | Population += 0.05 * 5516016.00
  64. Year: 36 | Population += 0.05 * 5791817.00
  65. Year: 37 | Population += 0.05 * 6081408.00
  66. Year: 38 | Population += 0.05 * 6385478.50
  67. Year: 39 | Population += 0.05 * 6704752.50
  68. Year: 40 | Population += 0.05 * 7039990.00
  69. Year: 41 | Population += 0.05 * 7391989.50
  70. Year: 42 | Population += 0.05 * 7761589.00
  71. Year: 43 | Population += 0.05 * 8149668.50
  72. Year: 44 | Population += 0.05 * 8557152.00
  73. Year: 45 | Population += 0.05 * 8985010.00
  74. Year: 46 | Population += 0.05 * 9434260.00
  75. Year: 47 | Population += 0.05 * 9905973.00
  76. Year: 48 | Population += 0.05 * 10401272.00
  77. Year: 49 | Population += 0.05 * 10921336.00
  78. Year: 50 | Population += 0.05 * 11467403.00
  79. Year: 51 | Population += 0.05 * 12040773.00
  80. Year: 52 | Population += 0.05 * 12642812.00
  81. Year: 53 | Population += 0.05 * 13274953.00
  82. Year: 54 | Population += 0.05 * 13938701.00
  83. Year: 55 | Population += 0.05 * 14635636.00
  84. Year: 56 | Population += 0.05 * 15367418.00
  85. Year: 57 | Population += 0.05 * 16135789.00
  86. Year: 58 | Population += 0.05 * 16942578.00
  87. Year: 59 | Population += 0.05 * 17789706.00
  88. Year: 60 | Population += 0.05 * 18679192.00
  89. Year: 61 | Population += 0.05 * 19613152.00
  90. Year: 62 | Population += 0.05 * 20593810.00
  91. Year: 63 | Population += 0.05 * 21623500.00
  92. Year: 64 | Population += 0.05 * 22704676.00
  93. Year: 65 | Population += 0.05 * 23839910.00
  94. Year: 66 | Population += 0.05 * 25031906.00
  95. Year: 67 | Population += 0.05 * 26283502.00
  96. Year: 68 | Population += 0.05 * 27597678.00
  97. Year: 69 | Population += 0.05 * 28977562.00
  98. Year: 70 | Population += 0.05 * 30426440.00
  99. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement