Arrays
are useful critters because they can be used in many ways. For example, a
tic-tac-toe board can be held in an array. Arrays are essentially a way to
store many values under the same name. You can make an array out of any
data-type including structures and classes.
Think about arrays like this:
[][][][][][]
Each of the bracket pairs is a slot(element) in the array, and you can put
information into each one of them. It is almost like having a group of
variables side by side.
Lets look at the syntax for declaring an array.
int example array[100]; //This declares an array
This would make an integer array with 100 slots, or places to store values(also
called elements). To access a specific part element of the array, you merely
put the array name and, in brackets, an index number. This corresponds to a
specific element of the array. The one trick is that the first index number,
and thus the first element, is zero, and the last is the number of elements
minus one. 0-99 in a 100 element array, for example.
What can you do with this simple knowledge? Lets say you want to store a
string, because C++ has no built-in datatype for strings, at least in DOS, you
can make an array of characters.
For example:
char astring[100];
will allow you to declare a char array of 100 elements, or slots. Then you can
receive input into it it from the user, and if the user types in a long string,
it will go in the array. The neat thing is that it is very easy to work with
strings in this way, and there is even a header file called string.h. There is
another lesson on the uses of string.h, so its not necessary to discuss here.
The most useful aspect of arrays is multidimensional arrays.
How I think about multi-dimensional arrays.
[][][][][]
[][][][][]
[][][][][]
[][][][][]
[][][][][]
This is a graphic of what a two-dimensional array looks like when I visualize it.
For example:
int twodimensionalarray[8][8];
declares an array that has two dimensions. Think of it as a chessboard. You can
easily use this to store information about some kind of game or to write
something like tic-tac-toe. To access it, all you need are two variables, one
that goes in the first slot and one that goes in the second slot. You can even
make a three dimensional array, though you probably won't need to. In fact, you
could make a four-hundred dimensional array. It would be confusing to visualize,
however.
Arrays are treated like any other variable in most ways. You can modify one
value in it by
putting:
arrayname[arrayindexnumber]=whatever;
//or, for two dimensional arrays
arrayname[arrayindexnumber1][arrayindexnumber2]=whatever;
However, you should never attempt to write data past the last element of the
array, such as when
you have a 10 element array, and you try to write to the 11 element. The memory
for the array
that was allocated for it will only be ten locations in memory, but the twelfth
could be
anything, which could crash your computer.
You will find lots of useful things to do with arrays, from store information
about certain
things under one name, to making games like tic-tac-toe. One suggestion I have
is to use for
loops when access arrays.
#include <iostream.h>
int main()
{
int x, y, anarray[8][8];//declares an array like a chessboard
for(x=0; x<8; x++)
{
for(y=0; y<8; y++)
{
anarray[x][y]=0;//sets the element to zero; after the loop all elements == 0
}
}
for(x=0; x<8;x++)
{
for(y=0; y<8; y++)
{
cout<<"anarray["<<x<<"]["<<y<<"]="<<anarray[x][y]<<" ";//you'll see
}
}
return 0;
}
Here
you see that the loops work well because they increment the variable for you,
and you only
need to increment by one. Its the easiest loop to read, and you access the
entire array.
One thing that arrays don't require that other variables do, is a reference
operator when
you want to have a pointer to the string. For example:
char *ptr;
char str[40];
ptr=str; //gives the memory address without a reference operator(&)
//As opposed to
int *ptr;
int num;
ptr=#//Requires & to give the memory address to the ptr