noobtuts

C++ Tutorial for noobs - Part 8: Arrays

...Back to Part 7: For, While and Do-Until

The Traditional Way

Arrays are a interesting C++ concept. Let's assume we want to make a game and right now we are trying to implement a monster. If it's a 3-dimensional world, then the monster needs a position like so:

float x = 0.0f;
float y = 0.0f;
float z = 0.0f;

If we want to calculate the distance between two monsters, we could do it like this:

#include "math.h"

// calcluate f²
float sqr(float f)
{
    return f * f;
}

float distance(float mob1_x, float mob1_y, float mob1_z,
               float mob2_x, float mob2_y, float mob2_z)
{
    return sqrt(sqr(mob2_x-mob1_x) +
                sqr(mob2_y-mob1_y) +
                sqr(mob2_z-mob1_z));
}

While that works just fine, it's a lot to write and it doesn't really look very pretty.

Arrays

Creating an Array

Another way to do it would be by using an array. An array always holds a whole bunch of variables at once. Here is how to create an array that holds the three float values 1, 2 and 3:

float a[3] = {0.0f, 1.0f, 2.0f};

Here is what happens in the code:

Accessing Array Elements

We can then access each value in the array like so:

std::cout<<a[0];
std::cout<<a[1];
std::cout<<a[2];

Please note that in C++ we start counting at 0, hence the first value is at position [0], and the last value in an array of size 3 is at position [2].

Of course we can also modify the values of the array like this:

a[0] = 42.0f;

Looping through an Array

We previously learned about for loops, which work very nicely with arrays too. For example if we want to increase each value in the array by 1, we can do it with a for loop like so:

for (int i = 0; i < 3; ++i) {
    a[i] = a[i] + 1.0f;
}

Using an Array for our Monster

Let's take a look at a more practical example by modifying our above Monster code to work with Arrays instead of three float variables.

This is how the monster position looks like when using an array:

float pos[3] = {0.0f, 0.0f, 0.0f};

And this is how the distance function looks like:

float distance(float mob1[3], float mob2[3])
{
    return sqrt(sqr(mob2[0]-mob1[0]) +
                sqr(mob2[1]-mob1[1]) +
                sqr(mob2[2]-mob1[2]));
}

Or by using a for-loop we can do it like this:

float distance(float mob1[3], float mob2[3])
{
    float sum = 0.0f;
    for (int i = 0; i < 3; ++i) {
        sum = sum + sqr(mob2[i]-mob1[i]);
    }
    return sqrt(sum);
}

Which version of the Monster code you use is up to you. Some people prefer arrays, some people prefer using a lot of variables instead. The important thing is to know that arrays exist and to know how to work with them if we have to.

Arrays as Lists

Another way to use Arrays is as lists. For example, if we have a list of monsters in a game, we could store each monster in an array. The problem with this approach is that arrays always have a fixed size that we have to set when creating an array:

float a[SOME_SIZE_HERE];

Hence Arrays aren't really useful when we need a list that sometimes holds one, sometimes two, sometimes even a hundred monsters.

Even though some people still use Arrays for lists, the better way would be to use a std::list or std::vector, which are both capable of holding a varying amount of things. But more on that in the next part.

Continue to Part 9: Lists...