1 00:00:00,000 --> 00:00:03,160 >> DAVID J. MALAN: Suppose we want to write a program that asks everyone in 2 00:00:03,160 --> 00:00:06,740 a room for their age, and then prints out how old those people will be a 3 00:00:06,740 --> 00:00:07,520 year hence? 4 00:00:07,520 --> 00:00:09,900 Now, obviously the arithmetic for this problem is going to be fairly 5 00:00:09,900 --> 00:00:10,660 straightforward. 6 00:00:10,660 --> 00:00:14,090 But the interesting question is, if we don't know in advance how many people 7 00:00:14,090 --> 00:00:16,790 are going to be in this room, how could we go about storing 8 00:00:16,790 --> 00:00:17,980 all of their ages? 9 00:00:17,980 --> 00:00:19,680 Well, let's take a look. 10 00:00:19,680 --> 00:00:22,760 >> Let's begin by first prompting the user, as I've done here, for the 11 00:00:22,760 --> 00:00:26,410 number of people in the room using getInt and a do-while loop in order to 12 00:00:26,410 --> 00:00:28,220 get an integer n. 13 00:00:28,220 --> 00:00:32,310 Suppose we now want to ask each such person in the room for their age. 14 00:00:32,310 --> 00:00:35,820 Well, my instincts would be to use a loop to do that prompting, but I also 15 00:00:35,820 --> 00:00:37,840 need a place to store those people's ages. 16 00:00:37,840 --> 00:00:40,760 And my first instincts there would be to use a variable for the first 17 00:00:40,760 --> 00:00:43,690 person's age, another variable for the second person's age, 18 00:00:43,690 --> 00:00:44,780 sort of along lines. 19 00:00:44,780 --> 00:00:46,230 Int age-- 20 00:00:46,230 --> 00:00:48,850 well, let's call it 1 for the first person. 21 00:00:48,850 --> 00:00:51,480 Int age 2 for the second person. 22 00:00:51,480 --> 00:00:53,980 Int age 3 for the third person. 23 00:00:53,980 --> 00:00:56,750 >> But wait a minute, this isn't perhaps the best path to go down. 24 00:00:56,750 --> 00:01:00,620 Because I don't know in advance of writing and compiling this program how 25 00:01:00,620 --> 00:01:02,330 many users there are going to be. 26 00:01:02,330 --> 00:01:06,590 And moreover, if there's as many as 100 users, declaring 100 variables 27 00:01:06,590 --> 00:01:10,350 sort of oddly named like this doesn't feel like the very best design. 28 00:01:10,350 --> 00:01:14,430 >> Well, thankfully there exists another type of variable called an array that 29 00:01:14,430 --> 00:01:18,710 allows us to store any number of ints inside of it, even if we don't know 30 00:01:18,710 --> 00:01:22,190 when writing my program how many such ints we're going to need. 31 00:01:22,190 --> 00:01:25,970 So let's backtrack and delete these several ints, and instead replace it 32 00:01:25,970 --> 00:01:29,620 with one variable called, say, ages, plural. 33 00:01:29,620 --> 00:01:33,420 But let's further specify on this line of code in square brackets that we 34 00:01:33,420 --> 00:01:35,460 want n ints. 35 00:01:35,460 --> 00:01:39,570 And therefore, we will collectively refer to these ints as ages. 36 00:01:39,570 --> 00:01:43,490 >> Now in just a moment I'll be able to get at each of the ints in this array 37 00:01:43,490 --> 00:01:47,270 similarly by way of square bracket notation, starting at 0. 38 00:01:47,270 --> 00:01:51,720 So let's proceed now in a loop to prompt the users for their ages. 39 00:01:51,720 --> 00:01:54,780 For int I get 0. 40 00:01:54,780 --> 00:01:59,464 I is less than N, the number of people in the room, I plus plus. 41 00:01:59,464 --> 00:02:06,610 >> And now within this loop, let's say printf age of person number, percent I 42 00:02:06,610 --> 00:02:09,430 is a placeholder, comma. 43 00:02:09,430 --> 00:02:13,210 And now, rather than start counting from 0 in the program itself, let's at 44 00:02:13,210 --> 00:02:17,180 least increment I by 1 so that a normal person using this program 45 00:02:17,180 --> 00:02:20,120 doesn't have to count like a computer scientist might. 46 00:02:20,120 --> 00:02:26,130 Let's now do ages, bracket I, thereby specifying that the i-th age in our 47 00:02:26,130 --> 00:02:31,480 array of ages is going to get the return value of getInt. 48 00:02:31,480 --> 00:02:37,800 >> Now below this loop, let's proceed to assume that some time passes. 49 00:02:37,800 --> 00:02:41,690 And let's now proceed in another loop to actually age everyone in the room 50 00:02:41,690 --> 00:02:42,800 by one year. 51 00:02:42,800 --> 00:02:48,110 So again, for int I get 0, I is less than N, the number of people in the 52 00:02:48,110 --> 00:02:49,680 room, I plus plus. 53 00:02:49,680 --> 00:02:57,210 >> And now inside of this loop, let's say printf a year from now person number, 54 00:02:57,210 --> 00:03:00,990 percent I is a placeholder, will be, percent I is another 55 00:03:00,990 --> 00:03:03,210 placeholder, years old. 56 00:03:03,210 --> 00:03:07,230 And then to plug into those placeholders, let's first say I plus 57 00:03:07,230 --> 00:03:11,220 1, so that again we start counting for the user from 1. 58 00:03:11,220 --> 00:03:18,630 And then let's plug in that person's age as ages bracket I plus 1, thereby 59 00:03:18,630 --> 00:03:23,740 specifying go get the i-th age in our array of ages, add 1 to it, and then 60 00:03:23,740 --> 00:03:28,370 insert that sum into our placeholder, close paren, semicolon. 61 00:03:28,370 --> 00:03:33,280 >> Let's now compile this program with make ages, and let's run it with dot 62 00:03:33,280 --> 00:03:34,990 slash ages. 63 00:03:34,990 --> 00:03:38,770 And suppose that there are only three people in the room, and someone is 18, 64 00:03:38,770 --> 00:03:40,700 someone is 19, someone is 20. 65 00:03:40,700 --> 00:03:45,350 Well, in a year, each of those folks is going to be 19, 20, and 21, 66 00:03:45,350 --> 00:03:46,600 respectively. 67 00:03:46,600 --> 00:03:48,948