1 00:00:00,000 --> 00:00:00,180 2 00:00:00,180 --> 00:00:03,430 BRIAN YU: Let's take a look at how you might have solved the population growth 3 00:00:03,430 --> 00:00:04,210 problem. 4 00:00:04,210 --> 00:00:06,240 Recall that in this problem, your task was 5 00:00:06,240 --> 00:00:10,410 to write a program in C that was able to figure out how many years it would take 6 00:00:10,410 --> 00:00:15,030 to go from some starting population size to some ending population size, 7 00:00:15,030 --> 00:00:17,770 according to some mathematical formula. 8 00:00:17,770 --> 00:00:20,460 So inside of population dot C, you'll see 9 00:00:20,460 --> 00:00:23,490 that the first thing we wanted to do was prompt the user 10 00:00:23,490 --> 00:00:25,890 for a starting population size. 11 00:00:25,890 --> 00:00:29,760 To do so, we could use the get int function from the CS50 Library, 12 00:00:29,760 --> 00:00:31,740 saying get int Start size. 13 00:00:31,740 --> 00:00:34,320 Let's get the starting size for the population, 14 00:00:34,320 --> 00:00:37,510 storing that value inside of Start. 15 00:00:37,510 --> 00:00:41,380 But recall that we couldn't just accept any integer as the starting size. 16 00:00:41,380 --> 00:00:44,620 We definitely didn't want to accept 0 or a negative number. 17 00:00:44,620 --> 00:00:48,520 But we also didn't want to accept numbers that were less than 9. 18 00:00:48,520 --> 00:00:51,640 We needed the starting population to be at least 9 19 00:00:51,640 --> 00:00:55,670 to make sure we could start to actually grow this population. 20 00:00:55,670 --> 00:00:58,960 So in order to continually re-prompt the user for input 21 00:00:58,960 --> 00:01:01,240 until they provide us with a valid input, 22 00:01:01,240 --> 00:01:03,850 we here used a do-while loop, where we're 23 00:01:03,850 --> 00:01:06,970 going to repeatedly prompt the user for a start size 24 00:01:06,970 --> 00:01:09,730 as long as Start is less than 9. 25 00:01:09,730 --> 00:01:13,760 If Start is less than 9, that means the input is not valid, 26 00:01:13,760 --> 00:01:16,310 which means we need to prompt the user again. 27 00:01:16,310 --> 00:01:19,600 And so this loop will continue running until we ultimately 28 00:01:19,600 --> 00:01:23,710 get a value for Start that we consider to be valid. 29 00:01:23,710 --> 00:01:26,740 After we've prompted for the starting population size, 30 00:01:26,740 --> 00:01:30,130 we next need to prompt for the ending population size. 31 00:01:30,130 --> 00:01:32,530 And here, too, there's very similar logic. 32 00:01:32,530 --> 00:01:35,980 We want to use get int to prompt the user for an integer, 33 00:01:35,980 --> 00:01:39,010 but we also want to make sure that integer is valid. 34 00:01:39,010 --> 00:01:41,920 In particular, the ending population size 35 00:01:41,920 --> 00:01:44,960 can't be less than the starting population size. 36 00:01:44,960 --> 00:01:48,850 If the end size is less than the start size, that's not a valid input. 37 00:01:48,850 --> 00:01:51,320 And so we need to re-prompt the user. 38 00:01:51,320 --> 00:01:53,740 So here too, we can use a do-while loop. 39 00:01:53,740 --> 00:01:57,250 We start by declaring an integer called End. 40 00:01:57,250 --> 00:02:00,760 And then we're repeatedly going to prompt the user for input. 41 00:02:00,760 --> 00:02:03,260 End equals get int End size. 42 00:02:03,260 --> 00:02:07,900 And if end is less than start, that means the end value provided 43 00:02:07,900 --> 00:02:10,660 by the user is not a valid integer, which 44 00:02:10,660 --> 00:02:14,830 means we need to re-prompt the user for a new value again and again, 45 00:02:14,830 --> 00:02:18,360 until they provide us with a valid end value. 46 00:02:18,360 --> 00:02:20,690 Once we have a valid start and end value, 47 00:02:20,690 --> 00:02:22,700 we now need to figure out how many years it 48 00:02:22,700 --> 00:02:25,820 would take to get from the starting population size 49 00:02:25,820 --> 00:02:27,980 to the end population size. 50 00:02:27,980 --> 00:02:31,880 In doing so, we're going to want to keep track of how many years have passed. 51 00:02:31,880 --> 00:02:35,270 And any time we want to keep track of information inside of our computer 52 00:02:35,270 --> 00:02:39,870 program, it's often going to be helpful to have a variable to do just that. 53 00:02:39,870 --> 00:02:45,140 So here we define an integer called years and set it equal to 0 at first, 54 00:02:45,140 --> 00:02:48,530 because, initially, no years have passed so far. 55 00:02:48,530 --> 00:02:52,270 But we're going to be updating that variable in due time. 56 00:02:52,270 --> 00:02:55,900 Now we need to keep updating the population size 57 00:02:55,900 --> 00:02:58,870 until we get to the end population size. 58 00:02:58,870 --> 00:03:01,810 And any time we're doing something again and again, it's 59 00:03:01,810 --> 00:03:05,270 often going to be helpful to put that code inside of a loop. 60 00:03:05,270 --> 00:03:08,560 So "while start is less than end" is going 61 00:03:08,560 --> 00:03:13,150 to repeatedly run some code, as long as the starting population hasn't yet 62 00:03:13,150 --> 00:03:15,220 reached the end population. 63 00:03:15,220 --> 00:03:18,460 Once the start population does reach the end population, 64 00:03:18,460 --> 00:03:22,200 then this condition will be false and will exit the loop. 65 00:03:22,200 --> 00:03:23,910 What does the loop do? 66 00:03:23,910 --> 00:03:27,600 Well, here, we have a line of code that updates the population, 67 00:03:27,600 --> 00:03:31,500 updates the value of start, based on the mathematical formula 68 00:03:31,500 --> 00:03:34,290 for how many new members of the population we're gaining 69 00:03:34,290 --> 00:03:36,900 and how many members of the population we're losing. 70 00:03:36,900 --> 00:03:42,420 Recall that per the formula, every year, we gain start divided by 3 llamas. 71 00:03:42,420 --> 00:03:44,070 We started with start llamas. 72 00:03:44,070 --> 00:03:47,310 And we lose start divided by 4 llamas. 73 00:03:47,310 --> 00:03:51,480 So by having start plus start over 3 minus start over 4, 74 00:03:51,480 --> 00:03:55,800 we're able to calculate the updated value for the population size. 75 00:03:55,800 --> 00:03:59,130 Recall, too, that division of integers in C 76 00:03:59,130 --> 00:04:01,500 will automatically truncate for us anything 77 00:04:01,500 --> 00:04:03,520 that shows up after the decimal. 78 00:04:03,520 --> 00:04:07,980 So if we end up with a number like 2.8, for example, we'd cut off the 0.8. 79 00:04:07,980 --> 00:04:11,250 And we'd just be left with the number 2, which is the behavior that we 80 00:04:11,250 --> 00:04:13,240 expect for the program. 81 00:04:13,240 --> 00:04:15,390 The other thing this loop needs to do, though, 82 00:04:15,390 --> 00:04:18,240 in addition to updating the population size, 83 00:04:18,240 --> 00:04:20,910 is to update how many years have gone by. 84 00:04:20,910 --> 00:04:25,680 We've updated the population once, which means one additional year has passed. 85 00:04:25,680 --> 00:04:29,700 And so I've included years plus plus here to mean one additional year 86 00:04:29,700 --> 00:04:30,480 has passed. 87 00:04:30,480 --> 00:04:35,230 Let's increase the value of the variable years by 1. 88 00:04:35,230 --> 00:04:39,220 After this loop is over, we will have reached the end population size. 89 00:04:39,220 --> 00:04:42,820 And the variable years will now store the number of years 90 00:04:42,820 --> 00:04:47,080 that it took to get us from that starting size to the end size. 91 00:04:47,080 --> 00:04:52,750 So ultimately, we can print using print f, Years colon, and then percent i 92 00:04:52,750 --> 00:04:58,210 to stand in for some integer here printing out the value of years. 93 00:04:58,210 --> 00:05:00,880 All in all, once we compile and run that program, 94 00:05:00,880 --> 00:05:04,180 we can provide as input some start and end population size, 95 00:05:04,180 --> 00:05:08,410 and our program will tell us how many years it took to get from the start 96 00:05:08,410 --> 00:05:10,870 to the end size in population. 97 00:05:10,870 --> 00:05:11,890 My name is Brian. 98 00:05:11,890 --> 00:05:14,500 And this was population growth. 99 00:05:14,500 --> 00:05:15,000