-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulation.c
57 lines (51 loc) · 1.37 KB
/
population.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// TODO: Prompt for start size
int start = 0;
while (start < 9)
{
start = get_int("Population start size: ");
if (start < 9)
{
printf("Sorry, population too small!\n");
}
}
// TODO: Prompt for end size
int end;
bool valid = false;
while (valid == false)
{
end = get_int("Population end size: ");
if (end > 2147483647) // greater than max INT
{
printf("Sorry, population too large!\n");
valid = false;
}
else if (end < start)
{
printf("Sorry, population can't decrease!\n");
valid = false;
}
else
{
valid = true;
}
}
// TODO: Calculate number of years until we reach threshold
float pop = (float) start;
int generation = 0;
while (end > (int) pop)
{
float born = pop / 3.0;
float died = pop / 4.0;
pop = pop + (int) born - (int) died;
generation += 1;
// printf("generation: %d, population %d.\n", generation, (int) pop); // Test
};
// TODO: Print number of years
// printf("The final population was %d.\n", (int) pop);
// printf("It took %d years to hit our population end size.\n", generation);
printf("Years: %d\n", generation);
}