- Now that you saw how we can declare an array,
let's look at the slightly different,
not necessarily better just different way
to declare an array.
We saw one way which is an explicit stating
the size of the array.
For example int numbers[100],
which means I'm declaring an array of type int
called numbers that can store
up to 100 integer numbers.
An alternative way is using a predefined constant.
Let's assume somewhere I have defined
a constant named MAX_ITEMS,
I can then use MAX_ITEMS rather than 100.
Suppose I use that array or that array declaration
or that number in multiple places in my program,
if I write 100 everywhere
and then I need to change it to say 200,
I will have to make those changes everywhere
and I might risk not changing all of them
and therefore causing an error.
It is sometimes safer to use a predefined constant.
Again, if all you have is a very small program
with one instant, it's arguable which one is better,
I refer to your personal preference and style.
Let's look at the code and take another look at this
from a slightly different angle.
Look at lines 17, 18, and 19,
those are the same three arrays from the previous videos
except now I'm using MAX_SCORES,
MAX_LETTERS and MAX_DATA_POINTS.
I'm actually doing two things here,
first I'm using the predefined constant,
second, I'm using the number that sounds more meaningful.
For example, if I would say
int scores 20 or 30,
it is not clear what that 20 or 30 means.
However, if I write the word MAX_SCORES
or MAX_LETTER it implies there's some thought
or some rules that have been placed onto that program
and it adds another layer of information
to whoever reads the program,
whether it's the programmer itself later on
or another person.
However, if you look in this file,
there's no where a mentioning of MAX_SCORES,
MAX_LETTERS or MAX_DATA_POINTS
so I did an additional thing.
Notice line 13 there's an
include constants.h.
This is a more elegant way to declare those constants.
Let's see what is in this file.
As you can see lines 11 through 13
define those exact data points as
MAX_SCORES, MAX_LETTERS, and MAX_DATA_POINTS as 50.
The reason to do that is first of all,
the constant.h is a file separate
from the main code.
Should I need to modify this, I don't even have to open
or touch the main code at all.
Second, I may want to share constant.h
with other programmers.
Many others on my team or in other teams
can have access to constants.h
for example, if it is placed on a server.
This can be a common repository of all agreed
standard constants for a particular project.
We could have a team of game programmers
in which for example we define a constant MAX_LEVELS,
all those programmers will then access the same number
in the constants.h.
There is no danger that one programmer
will work on level eight,
if the maximum level allowed is seven.
Furthermore, we can create loops and conditions
that don't check against a specific number
but check against a concept MAX_LEVELS
which is then predefined externally.
We will see in the upcoming videos
more aspects of interacting with arrays
using them in lots of different ways.