EasyManua.ls Logo

Thames & Kosmos Code Gamer - Arrays; Understanding Array Structure

Thames & Kosmos Code Gamer
66 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Arrays
If you want to save a lot of values of the same type, for
example the last 100 values, it can be pretty time-
consuming and confusing to have to enter all that in.
You would have to apply a separate variable for each
individual value, as follows:
int value 1;
int value 2;
int value 3;
...e tc...
int value 100;
It’s a lot simpler to use what’s called an array.
In an array, you can store many values at one time. To
make it possible to access the individual variables in
the array, they are all consecutively numbered. So you
can think of an array as a kind of table in which the
individual values are entered into numbered columns.
The numbering of the columns, however, starts with 0
rather than 1:
INDEX 0 1 2 3 n
VALUES Value 0 Value 1 Value 2 Value 3 Value n
In general, an array is created as follows:
type arrayName[length];
You can also assign values to an array by specifying
the individual values in curly brackets:
The type specifies which type of data can be saved in the
array (int, float, etc.). The number of values that can be
stored in an array is known as its length.
int myArray[10]; // An array that can
// take 10 int values
int myArray[4] = {0, 4, 2, 3};
In that case, you don’t necessarily have to indicate
how many values there are. You can also write:
int myArray[] = {0, 4, 2, 3};
So, for example, to create an array that can take 10
integer values, you proceed as follows:
KNOWLEDGE BASE
An array is sometimes called a Field or
Data Field. However the term Array is
the much more common name.

CodeGamer
CodeGamer manual inside english.indd 41 7/19/16 12:32 PM