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