CCNezin, if you know arrays, that's good:
1 2 3 4 5 6
|
//Declaration of an array:
type variable_name[number];
type variable_name[] = {0, 0, 0, 0};
//To access the value stored in an element, you do this:
variable_name[x]; //where x is the index of the element.
|
The first array is merely given a size. For instance, if I would replace number with 10, the size of the array would be 10, that is, it would hold 10 elements. In the second array, we do not need to give it its size because the size is determined by the number of elements, in this case 4. To clarify, an element is basically a "slot" with a value can be stored. An index is basically the "slot number". The first index is always 0. An array with 10 elements (slots) would have 0 as its first number, and 9 as its last number. So the last element of n array is always (size of array) - 1. As a side note, when dealing with char arrays, remember that it needs to be null terminated, which means that the last element must contain the character '\0'.
As for loops, read up on these loops: while and for. I like for loops when dealing with arrays. For example, assume you create this array:
1 2 3 4 5 6 7 8
|
//exampleArray is of the type int and its size is 4. Thus, it has 4 elements
//that are number from 0 to 3. 0 contains the value 2, 1 contains the value 6,
//2 contains the value 12 and 3 contains the value 28.
int exampleArray[] = {2, 6, 12, 28};
//And you can use a for loop to print out the value of every element:
for (int i = 0; i < 4; i++)
std::cout << exampleArray[i] std::endl;
|
You can read up on for loops on the main site here. But essentially, it first declares a variable called i, and it does so only once when the loop is first initialized. It then checks that the value of i is less than 4. THEN it runs the code within the loop. When it's done with that, it adds 1 to the value of i (so if the value of i was 4, it will now be 2+1 which is 3). It then starts the loop again, checking if i is less than 4, runs the code within the block, adds 1 to i again... until the value of i is not less than 4. When that happens, the loop ends.