1 00:00:00,000 --> 00:00:00,330 2 00:00:00,330 --> 00:00:02,130 SPEAKER 1: In this lab, your task is going 3 00:00:02,130 --> 00:00:05,980 to be to write a program to simulate population growth. 4 00:00:05,980 --> 00:00:09,540 Let's imagine that we have a population of animals, a population of llamas, 5 00:00:09,540 --> 00:00:10,830 for example. 6 00:00:10,830 --> 00:00:15,570 Every year, we lose some animals from that population as older llamas die. 7 00:00:15,570 --> 00:00:20,050 But we also gain some animals to that population as new llamas are born. 8 00:00:20,050 --> 00:00:24,090 We can model this kind of population growth using a mathematical formula. 9 00:00:24,090 --> 00:00:26,370 Let's assume, for example, that every year, 10 00:00:26,370 --> 00:00:31,800 if we have a population of n llamas, we gain n divided by 3 llamas 11 00:00:31,800 --> 00:00:34,470 and we lose n divided by 4 llamas. 12 00:00:34,470 --> 00:00:38,340 So in a population of 12 llamas, for example, in the next year, 13 00:00:38,340 --> 00:00:41,940 we would gain 12 divided by 3, or four new llamas. 14 00:00:41,940 --> 00:00:47,320 And we would lose at 12 divided by 4, or losing three llamas, in that case. 15 00:00:47,320 --> 00:00:50,070 Given this setup, we could ask ourselves a question. 16 00:00:50,070 --> 00:00:52,440 For example, we could ask, how many years would it 17 00:00:52,440 --> 00:00:55,350 take to go from 20 llamas in a population 18 00:00:55,350 --> 00:00:58,120 to 30 llamas in a population? 19 00:00:58,120 --> 00:01:01,540 Right now for example, we have 20 llamas in this population. 20 00:01:01,540 --> 00:01:06,310 But after one year we would gain 20 divided by 3 llamas. 21 00:01:06,310 --> 00:01:10,000 Now notice here that 20 doesn't divide evenly into three. 22 00:01:10,000 --> 00:01:12,220 And as a result, we need to just truncate 23 00:01:12,220 --> 00:01:14,080 whatever would come after the decimal. 24 00:01:14,080 --> 00:01:17,180 The actual answer is going to be six point something. 25 00:01:17,180 --> 00:01:19,433 But because we can't have a fraction of an animal, 26 00:01:19,433 --> 00:01:21,850 we're just going to truncate whatever is after the decimal 27 00:01:21,850 --> 00:01:24,100 and say that we're gaining six llamas. 28 00:01:24,100 --> 00:01:28,810 And we're going to lose 20 divided by 4, which nicely equals five llamas. 29 00:01:28,810 --> 00:01:33,070 So after one year, we will now have 21 llamas. 30 00:01:33,070 --> 00:01:34,960 We can then repeat the process and consider 31 00:01:34,960 --> 00:01:36,610 what might happen in the next year. 32 00:01:36,610 --> 00:01:40,870 In the next year, we would gain 21 divided by 3, or seven llamas. 33 00:01:40,870 --> 00:01:44,530 And we would lose 21 divided by 4, truncating whatever is 34 00:01:44,530 --> 00:01:47,290 after the decimal, losing five llamas. 35 00:01:47,290 --> 00:01:49,750 Meaning we have a net change of two llamas. 36 00:01:49,750 --> 00:01:53,050 Which means that after two years, we now have 23 llamas. 37 00:01:53,050 --> 00:01:55,450 After the next year, we would gain seven llamas, 38 00:01:55,450 --> 00:01:59,800 lose five, for a new total of 25 llamas after three years. 39 00:01:59,800 --> 00:02:02,020 In the next year, we would gain eight llamas 40 00:02:02,020 --> 00:02:06,130 and lose six, for a new total of 27 llamas after four years. 41 00:02:06,130 --> 00:02:09,430 And then in the fifth year, we would gain at nine llamas 42 00:02:09,430 --> 00:02:14,570 and lose six for a total now of 30 llamas after five years. 43 00:02:14,570 --> 00:02:16,960 So the answer to the question, how many years would it 44 00:02:16,960 --> 00:02:21,670 take to go from a population of 20 llamas to a population of 30 llamas 45 00:02:21,670 --> 00:02:23,590 is five years. 46 00:02:23,590 --> 00:02:25,780 Now we did all of that math manually. 47 00:02:25,780 --> 00:02:29,320 But your task now is going to be to write a program in C that 48 00:02:29,320 --> 00:02:31,600 can do that calculation for us. 49 00:02:31,600 --> 00:02:33,700 The program will work like this. 50 00:02:33,700 --> 00:02:37,830 At the command line, you'll run a program, ./population. 51 00:02:37,830 --> 00:02:40,510 And at that point, your program should prompt the user 52 00:02:40,510 --> 00:02:43,000 for a starting population size. 53 00:02:43,000 --> 00:02:46,990 You might type of starting population size of 100, for example. 54 00:02:46,990 --> 00:02:51,130 Then your program should prompt the user for an ending population size. 55 00:02:51,130 --> 00:02:55,720 And the user will type in another population size, say 200, for example. 56 00:02:55,720 --> 00:02:58,390 Then your program should figure out how many years 57 00:02:58,390 --> 00:03:02,470 it would take to bring the population of llamas from the starting size 58 00:03:02,470 --> 00:03:06,320 to the ending size, and then print out that number of years. 59 00:03:06,320 --> 00:03:08,560 So in this case, our program would print years 60 00:03:08,560 --> 00:03:11,620 colon 9 because it would take nine years to bring 61 00:03:11,620 --> 00:03:15,520 the population from 100 to 200. 62 00:03:15,520 --> 00:03:18,030 So in summary, here's what you have to do. 63 00:03:18,030 --> 00:03:21,930 First, start by prompting the user for the starting population 64 00:03:21,930 --> 00:03:24,840 size of your population of llamas. 65 00:03:24,840 --> 00:03:29,340 And you should check to make sure that the user's input is at least nine. 66 00:03:29,340 --> 00:03:31,500 The population size needs to be at least nine 67 00:03:31,500 --> 00:03:34,980 so that we can actually start to grow this population of llamas. 68 00:03:34,980 --> 00:03:38,100 So if the user types in a number that's less than nine, 69 00:03:38,100 --> 00:03:40,860 you should keep re-prompting the user to type in a number 70 00:03:40,860 --> 00:03:45,300 again until they give you a number that's at least nine. 71 00:03:45,300 --> 00:03:49,410 Next, you should prompt the user for the ending population size, 72 00:03:49,410 --> 00:03:52,290 here making sure that the user's input is 73 00:03:52,290 --> 00:03:55,130 at least as large as the starting size. 74 00:03:55,130 --> 00:03:58,200 It wouldn't make sense, for example, to imagine the population going 75 00:03:58,200 --> 00:04:01,460 from a population of 100 to a population of 50 76 00:04:01,460 --> 00:04:04,470 because the population is always going to be growing. 77 00:04:04,470 --> 00:04:07,140 So you'll want to make sure that the end population 78 00:04:07,140 --> 00:04:11,380 size is greater than or equal to the starting population size. 79 00:04:11,380 --> 00:04:13,890 If the user tries to type in an end population 80 00:04:13,890 --> 00:04:17,320 size that is less than the starting population size, 81 00:04:17,320 --> 00:04:20,370 then your program should continually re-prompt the user 82 00:04:20,370 --> 00:04:22,980 to type in another ending population size 83 00:04:22,980 --> 00:04:28,210 until they provide you one that is at least the starting population size. 84 00:04:28,210 --> 00:04:30,340 After that, your program should calculate 85 00:04:30,340 --> 00:04:32,740 how many years would be required to bring 86 00:04:32,740 --> 00:04:35,830 the population from the starting size to the ending size, 87 00:04:35,830 --> 00:04:40,420 recalling that every year we are going to add n divided by 3 llamas, 88 00:04:40,420 --> 00:04:42,190 if there are n llamas to begin with. 89 00:04:42,190 --> 00:04:45,010 And we're going to lose n divided by 4 llamas. 90 00:04:45,010 --> 00:04:48,670 And as with before, because we can't have fractions of a llama, 91 00:04:48,670 --> 00:04:52,360 if either of those computations ends up with something after the decimal, 92 00:04:52,360 --> 00:04:55,990 you're going to truncate or ignore what comes after that decimal 93 00:04:55,990 --> 00:04:57,660 when doing your math. 94 00:04:57,660 --> 00:05:00,040 Finally, after you've made that calculation, 95 00:05:00,040 --> 00:05:01,890 you'll print the number of years required. 96 00:05:01,890 --> 00:05:04,950 Printing years colon and then the number of years 97 00:05:04,950 --> 00:05:07,620 that would be required to bring the population up 98 00:05:07,620 --> 00:05:10,080 to that ending population size. 99 00:05:10,080 --> 00:05:12,000 As you start working on this problem, there 100 00:05:12,000 --> 00:05:14,460 are a few strategies that might prove helpful. 101 00:05:14,460 --> 00:05:17,160 One is a do while loop, a type of loop that 102 00:05:17,160 --> 00:05:21,310 lets you prompt the user one or more times for something, in this case. 103 00:05:21,310 --> 00:05:25,470 So in the loop you're seeing here, we start by declaring a variable called n. 104 00:05:25,470 --> 00:05:29,520 And then inside of a loop, we prompt the user for a positive integer, 105 00:05:29,520 --> 00:05:31,860 storing that integer inside of n. 106 00:05:31,860 --> 00:05:36,600 But we're going to keep repeating that loop as long as n is less than 1. 107 00:05:36,600 --> 00:05:39,580 Meaning if the user doesn't type in a positive integer, 108 00:05:39,580 --> 00:05:41,730 then we're going to prompt the user again. 109 00:05:41,730 --> 00:05:44,160 You won't use this exact code inside of your lab 110 00:05:44,160 --> 00:05:46,110 but you might use something similar, both 111 00:05:46,110 --> 00:05:48,360 when you're prompting for the starting population size 112 00:05:48,360 --> 00:05:52,260 to make sure that the starting population size is at least nine, 113 00:05:52,260 --> 00:05:55,140 and also when prompting for the end population size 114 00:05:55,140 --> 00:05:58,500 to make sure that the end population size is at least as 115 00:05:58,500 --> 00:06:01,410 large as the starting population size. 116 00:06:01,410 --> 00:06:04,230 You might also find it helpful to update a variable 117 00:06:04,230 --> 00:06:06,190 as you go about working on this lab. 118 00:06:06,190 --> 00:06:09,150 You might want to repeatedly update your population size variable, 119 00:06:09,150 --> 00:06:13,530 for example, in order to add new llamas and get rid of older llamas. 120 00:06:13,530 --> 00:06:15,900 You might do that using a formula like this. 121 00:06:15,900 --> 00:06:19,920 If you have a variable called n inside of which is your current population, 122 00:06:19,920 --> 00:06:22,950 you can update the value of n for the next year 123 00:06:22,950 --> 00:06:27,990 using a line that says n equals n plus n over 3, the number of llamas 124 00:06:27,990 --> 00:06:31,620 we're adding, minus n over 4, the number of llamas 125 00:06:31,620 --> 00:06:34,320 that we're losing in that particular year. 126 00:06:34,320 --> 00:06:36,810 And finally, once you've completed the calculation, 127 00:06:36,810 --> 00:06:40,860 you can print out a variable using the print f function, saying, 128 00:06:40,860 --> 00:06:47,620 printf years colon, and then %i, we're %i is going to stand in for an integer. 129 00:06:47,620 --> 00:06:50,610 Which Integer Well, that you specify after a comma. 130 00:06:50,610 --> 00:06:53,160 And here we're saying, print out the value of n 131 00:06:53,160 --> 00:06:56,850 as the integer to print to the user. 132 00:06:56,850 --> 00:06:58,820 Let's now take a look at the distribution code 133 00:06:58,820 --> 00:07:02,150 that we give you as a starting point for this problem. 134 00:07:02,150 --> 00:07:08,180 Here in population.c, we've already included cs50.h and stdio.h for you. 135 00:07:08,180 --> 00:07:11,720 And then giving you a main function with a few to dos to get started. 136 00:07:11,720 --> 00:07:16,760 You're going to first prompt for the start size, then prompt for the n size. 137 00:07:16,760 --> 00:07:19,550 Then calculate how many years it's going to take in order 138 00:07:19,550 --> 00:07:23,550 to reach that threshold value from the start size to the n size. 139 00:07:23,550 --> 00:07:25,850 And then finally, once you've done that computation, 140 00:07:25,850 --> 00:07:28,430 printing out the number of years that would be required 141 00:07:28,430 --> 00:07:31,260 to take you from the start to the end. 142 00:07:31,260 --> 00:07:33,270 Once you've written all of those pieces, you 143 00:07:33,270 --> 00:07:36,600 should then be able to compile your program and then run it, 144 00:07:36,600 --> 00:07:39,420 giving your program a starting population size and an ending 145 00:07:39,420 --> 00:07:40,690 population size. 146 00:07:40,690 --> 00:07:42,690 And then your program should be able to tell you 147 00:07:42,690 --> 00:07:47,430 how long it will take to get from point A to point B. My name is Brian. 148 00:07:47,430 --> 00:07:50,240 And this was population growth. 149 00:07:50,240 --> 00:07:51,000