Arrays
What are they?
- Arrays are a series of variables of the same type related
by the same name. Each variable in the series is accessed
by its index location. The indexes run from 0 to size - 1,
where size is the total number of variables or positions in
the array. If an array is of size 5, there are 5 positions
(0 to 4) in the array starting with 0, the second position
is 1, the third position is 2, the fourth position is 3, the
fifth position is 4.
Creating Arrays
- When Creating an array there are two things that must be
considered. What type will the array be and what size will
the array be. There are two steps that need to be followed
when creating an array.
- Declare the Array - state what type the array is and
what the name of the array is.
- Create the Space for the Array - state what the size
of the array is, so that the computer reserves the
space in memory.
- Here are examples, note that the declaration is listed first,
followed by the name of the array, and the creation of space
is inside the brackets [].
Size corresponds to the int size of each array.
char word[size];
int numbers[size];
float decimals[size];
- Initial values can be assigned to an array instead of giving a
size. The size is taken from the number of initial values.
char word[] = {'p', 'e', 't', 'e'};
int numbers[] = {0, 2, 4, 6, 8};
float decimals[] = {3.14, 2.86, 4.57};