>> DAVID J. MALAN: Suppose we want to write a program that asks everyone in
a room for their age, and then prints out how old those people will be a
year hence?
Now, obviously the arithmetic for this problem is going to be fairly
straightforward.
But the interesting question is, if we don't know in advance how many people
are going to be in this room, how could we go about storing
all of their ages?
Well, let's take a look.
>> Let's begin by first prompting the user, as I've done here, for the
number of people in the room using getInt and a do-while loop in order to
get an integer n.
Suppose we now want to ask each such person in the room for their age.
Well, my instincts would be to use a loop to do that prompting, but I also
need a place to store those people's ages.
And my first instincts there would be to use a variable for the first
person's age, another variable for the second person's age,
sort of along lines.
Int age--
well, let's call it 1 for the first person.
Int age 2 for the second person.
Int age 3 for the third person.
>> But wait a minute, this isn't perhaps the best path to go down.
Because I don't know in advance of writing and compiling this program how
many users there are going to be.
And moreover, if there's as many as 100 users, declaring 100 variables
sort of oddly named like this doesn't feel like the very best design.
>> Well, thankfully there exists another type of variable called an array that
allows us to store any number of ints inside of it, even if we don't know
when writing my program how many such ints we're going to need.
So let's backtrack and delete these several ints, and instead replace it
with one variable called, say, ages, plural.
But let's further specify on this line of code in square brackets that we
want n ints.
And therefore, we will collectively refer to these ints as ages.
>> Now in just a moment I'll be able to get at each of the ints in this array
similarly by way of square bracket notation, starting at 0.
So let's proceed now in a loop to prompt the users for their ages.
For int I get 0.
I is less than N, the number of people in the room, I plus plus.
>> And now within this loop, let's say printf age of person number, percent I
is a placeholder, comma.
And now, rather than start counting from 0 in the program itself, let's at
least increment I by 1 so that a normal person using this program
doesn't have to count like a computer scientist might.
Let's now do ages, bracket I, thereby specifying that the i-th age in our
array of ages is going to get the return value of getInt.
>> Now below this loop, let's proceed to assume that some time passes.
And let's now proceed in another loop to actually age everyone in the room
by one year.
So again, for int I get 0, I is less than N, the number of people in the
room, I plus plus.
>> And now inside of this loop, let's say printf a year from now person number,
percent I is a placeholder, will be, percent I is another
placeholder, years old.
And then to plug into those placeholders, let's first say I plus
1, so that again we start counting for the user from 1.
And then let's plug in that person's age as ages bracket I plus 1, thereby
specifying go get the i-th age in our array of ages, add 1 to it, and then
insert that sum into our placeholder, close paren, semicolon.
>> Let's now compile this program with make ages, and let's run it with dot
slash ages.
And suppose that there are only three people in the room, and someone is 18,
someone is 19, someone is 20.
Well, in a year, each of those folks is going to be 19, 20, and 21,
respectively.