By using an array of ten elements to store the input, instead of ten
individually-named variables, we can vastly shorten the program. Take
a look at this version of the program
(``array2.c'' in your ``examples'' directory).
This program is very similar to the first version, except that it uses an array
named n. Let's look at this program more closely to get a better feel
for how arrays are declared and used. Here are a few questions to consider.
How can you tell that n is an array?
Click here for the answer
How can you read from or write to an array element?
Click here for the answer
When you refer to an element of an array, like this,
printf("%d\n", n[5]);
the number is parentheses is called the subscript or the
index. In this case, the subscript is 5.
We said that by using an array, we could make our program smaller. However,
this second version of our program isn't any smaller than our first version.
In fact, it's a little larger! Obviously, we haven't taken advantage of the
fact that we're using an array. Let's do that now.
Eric N. Eide
Hamlet Project
Department of Computer Science
University of Utah