Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: Arrays

C++ Programming Series: Arrays

Previous two posts are (or just looks like) useless and really, are not interesting. But in this post, you are going to get the reward! Yes, a reward! You are going to learn my favourite thing, the favourite in the way that it makes life with programming very easy! We call it: Arrays.

Before proceeding, remember that this post is a very long post because it is very significant.

What is an array? An array is a list of something; a group of variables of a specific type. They are group together in a linear way just like ‘3 after and 2 before the 1’.

#include <iostream>
using namespace std;
int main()
    //This is an array of integers which holds 5 elements(variable of type 'int')
    int array[5] = { 5, 10, 15, 20, 25 };

    //Printing all elements of the array using the index.
    //Possible indexes are from 0 to 4, not 5 as it doesn't start from 1.
    //Anything other than that would give you an error or just garbage values!
    for(int i = 0; i < 5; i++)
        cout << array[i] << endl;

It looks very simple and I hope that you may have got it here! An array of variables can be for any type! It is not necessary to put 5 before 10 and so on, you can put any number for any index. Here is the format of an array declaration:

-type- -name- [-number_of_elements-];

It’s too simple to make one. To initialize every element of an array, there is another method. Just like we output the element of an array via the index, we can alter that number as well!

int array[5]; //Only declaration

for(int i = 1; i <= 5; i++)
    array[i-1] = i*5; //i-1 as index to get a proper range of 0-4 instead of 1-5
//array is now = 5, 10, 15, 20, 25

array[0] = 10; //previous value = 5
array[1] *= 100; //previous value = 10
array[2] -= 5; //previous value = 15
array[3] = 0; //previous value = 20
array[4] %= 5; //previous value = 25

It might be possible that the for-statement and %= may look unclear to you. If that is the case, please check out my previous posts on C++ Programming Series to remember them.

Arrays are just a list of elements with the number of elements or range of indexes unalterable or constant. We can’t change the total number of elements in an array! But there are arrays which can have an alterable number of elements; this term is associated with STL or C++ Standard Template Library which we will talk about later.

In the first block of this post, it is not necessary to type in the total number of variables inside the square brackets as the compiler can check that by the number of elements in between the curly brackets. It is not possible to initialize only one or two variables in between the curly brackets, we must initialize all variables in such a case.

Now, we are going to discuss a small case of arrays to pinpoint two things: its importance and the sizeof() operator’s importance (yeah, that useless sizeof()!)

Suppose that we are going to make a game with many enemies. Let’s say that they are just 100 in the count and we need to have a variable that holds the health for each!

Lets us try to do it without arrays. In the main(or int main()) method:

int enemy1_HP = 10;
int enemy2_HP = 10;
int enemy3_HP = 10;
//and so on to...
int enemy99_HP = 10;
int enemy100_HP = 10;


OK? Is that OK? If your answer is ‘No’, then you really don’t like to work hard. But if it is ‘Yes’, then I should have told you that you also need a variable for the position, speed, attack, bullets, coins… and more. Just with the mentioned ones, there will be about 500 lines of code! That is one of the reasons why we should not measure a programmer’s capability by the number of lines in his/her program.

Apart from the problem of having a lot of lines being wasted on the variable declaration, there is an even bigger problem! Just tell me, how are you supposed to combine your 100 enemies’ variables with the looping? I mean that there is nothing like an index in a large list of singleton variables. It will just get more and more complex!

In short, it will take enough time to complete the game without arrays that we will begin to hate programming. Of course, it is NOT OK at all!

Here is with arrays:

//Replacing 100 lines with just 3!
int enemyHP[100];
for(int i = 0; i < 100; i++)
    enemyHP[i] = 10;

//I am in love with arrays!

The index can serve as an ID number for the enemy and you can always get and set the health, position, speed, attack, bullets, coins, etc, of the appropriate enemy just by having the ID or index of that enemy.

Now, at the end of the program, for some reason, you want all enemies’ health(or HP) to be equal to zero. Let’s say that the program consists of more than 10000 lines of code and 100 files. At the specific line of a specific file, there is the declaration of the enemyHP array. You just forgot the total number of elements in that array. Finding the declaration in the files might take some time. Your IDE has the feature to find the declaration but you don’t know how to access that feature. So, what? We can always use sizeof() to find out the amount of memory allocated by the variable or even array.

In this case, it will give the value of 400 for enemyHP array. For each integer, it will have 4 bytes and so, for 100, 400 bytes. So, we need to divide sizeof(enemyHP) by sizeof(int) to get the total amount of elements for that array:

for(int i = 0; i < sizeof(enemyHP)/sizeof(int), i++)
    enemyHP[i] = 0;

And there you go! It is ended and with every post, our database is getting expanded. I can’t just let you go with nothing so, here are some exercises. Strictly speaking, if you are learning to program for the first time with this series of post, just stick with the first 3 exercises. Don’t bother to see the exercises beyond the third one because they might confuse you. The first 3 exercises assume that you must have some knowledge about console printing, arrays, user input, conditional statements. Just try these 3 exercises. If you get failed to get proper results, try to fix the problems in your code. But after that, if you still get failed, just check out the solution given with the exercise.

To learn how to program stuff is just like to learn how to paint stuff. It just gets better with you, trying to do it, face it and then, experiment with it, again and again, and not just by reading and watching tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *