Sei sulla pagina 1di 1

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? No
w, obviously the arithmetic for this problem is going to be fairly straightforwa
rd. 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 pe
ople 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 t
o store those people's ages. And my first instincts there would be to use a vari
able for the first person's age, another variable for the second person's age, s
ort of along lines. Int age-- well, let's call it 1 for the first person. Int ag
e 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 g
oing to be. And moreover, if there's as many as 100 users, declaring 100 variabl
es 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 allo
ws us to store any number of ints inside of it, even if we don't know when writi
ng my program how many such ints we're going to need. So let's backtrack and del
ete these several ints, and instead replace it with one variable called, say, ag
es, plural. But let's further specify on this line of code in square brackets th
at we want n ints. And therefore, we will collectively refer to these ints as ag
es.
Now in just a moment I'll be able to get at each of the ints in this array simil
arly 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 it
self, 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, brack
et 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 no
w proceed in another loop to actually age everyone in the room by one year. So a
gain, for int I get 0, I is less than N, the number of people in the room, I plu
s plus.
And now inside of this loop, let's say printf a year from now person number, per
cent I is a placeholder, will be, percent I is another placeholder, years old. A
nd 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 o
f 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 a
ges. 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.

Potrebbero piacerti anche