C++ Tutorial #8 – Arrays

Facebooktwittergoogle_plusredditpinterestlinkedinmail

At the end of my last tutorial: While Loops & Do While Loops, I told you that I was going to teach you Range-Based For Loops next. I’m sorry, but I lied. I think it would be easier to teach you arrays first, that way you can apply it in Range-Based For Loops.  Let’s get started!

 

An array is a group of elements of the same variable type placed in neighboring memory locations. That means that you can declare a variable, and set it to values in each element. Declaring an array is a lot like declaring a variable. In fact, you start by declaring a variable and write the number of elements you want inside square brackets.

 

 

This is how you would write an array:

 

type variable [elements]

 

If you wanted to use an integer with four elements, you would write it like this:

 

 

You can assign values to your elements like this:

 

 

Make sure you place the values inside curly braces and separate each with a comma.You don’t always have to assign values. You can leave it blank like this:

 

 

All the values will equal to zero. Make sure you add the curly braces if you want all your elements to start out with zero, otherwise each of your elements will have different values of random numbers.

 

Note, this is the same for regular variables (just in case you didn’t know). When you declare a variable and don’t give it any value, it will equal a random value. I don’t mean to confuse you, but I just wanted to add that just in case you’re writing a program and need a variable that starts at zero. If you want a regular variable with a value of zero, just set it to zero regularly.

 

Like this:

 

 

Now, back to arrays. This is where it gets really tricky. When you declare an array, you place the number of elements you want to inside square brackets. If you want to access a specific element of an array, you simply write the name of the variable and the element number. This may not be what you think. If you want to access the first element, you start with zero. So if I wanted to access the first element in int x, I would write:

 

 

This would be the first element in:

 

 

x[0] is equal to 5, x[1] is equal to 13, x[2] is equal to 9.

 

So remember, the order of the elements starts with 0 (not one), and goes up.

 

accessing arrays

Now let’s write a program using these variables. In this program, I’ll show examples of arrays with different variable types. You can copy, or write this code:

 

 

When we build & run our program, it should look something like this:

 

ARRAY
2, 4, 8, 16
3.14, 2.23, 5.56
101
John Jacob Jingleheimer Schmidt

 

Now, let me explain what we just did:

Inside of our main function, we created a char, int, float, bool, and string. We made them into arrays and assigned values to each of the elements.

 

 

Next, we used “cout” print the value of each of the elements in the arrays. Notice how I separated each of the values with a space and a comma. This is totally optional.

 

 

Some of you may be wondering why you would use this in game programming. I will teach you this in a later lesson, but you will be using arrays inside of arrays to make things like tile map editors. You’re going to have to program your game to read your tile maps., load them, and draw them onto the screen. Don’t think too hard on this right now. I’ll explain this in another tutorial. I’m just preparing you for future tutorials. As you wait for the next lesson, think about some of the things you’ve learned so far. and think how you can use this to make a video game. There’s still a lot to learn, but someday it will click into your head, and you’ll know how to do it. Keep up the good work and I will post a new tutorial soon.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

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