## Introduction to Arrays: C++ Crash Course (Free Tutorial)

Do you have a busy to-do list? Get ready to check one thing off: learning about lists in C++! If you are a beginner and want to learn how to code, check out our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Arrays are lists of objects. For instance, there are integer arrays, which will give you lists of integers. String arrays will give you lists of strings. Sometimes, you can even have a mixture of the two.

Let’s start by looking at integer arrays. If you feel like testing out some code in real time, go on over to rextester.com. This site allows you to code in the main window, and test out your code instantly. Declare an integer named ` my_array`.

``int my_array;``

In rextester.com, your main coding screen should look like this:

````//g++ 4.9.3`

`#include <iostream>`

`int my_array;`

`int main ()`
`{`
`std:cout << "Hello, world!\n";`
`}````

Right now, `my_array` is still an integer. To make it into an integer array, add square brackets to the end of the array name.

This symbolizes that it’s an array of integers rather than just one variable of type integer.

````int my_array[];`
```

Now let’s give our array a size. Let’s say there are 3 elements in this array.

````int my_array[3];`
```

Yay! We have just created an array of 3 integers. As we haven’t specified which 3 integers will be in this array, the C++ compiler will populate the array with 3 zeros. However, zeros are boring, so let’s add some values ourselves. Open up the curly brackets to enter in values `1`, `2`, and `3`.

````int my_array[3]= {1, 2, 3};`
```

We have just populated our array with the integers 1, 2, and 3. Note: element counts in arrays start at zero. Thus, in this array, Element Zero is `1`, Element One is `2`, and Element Two is `3`.

In order to test your code, access one element of the array and print it out. To access the first element, call `my_array` and specify the first element. Replace `"Hello, world!\n"` with `my_array[0];`.

Scroll down to the bottom and press “Run It”. You will see the number “1” appear at the bottom of the screen.

To check that things are working properly throughout the array, check the last element (Element Two). Replace `my_array[0];` with `my_array[2];`.

If you run the code, you will see the number “3” appear at the bottom of the screen.

Now let’s take a look at string arrays. Beneath your integer array, start with a standard string, and call it `string_array`. To make it into a string array, add square brackets to the end of the array name.

````std::string string_array[];`
```

Populate the array with two strings named `"mammoth"` and `"interactive"`.

````std::string string_array[] = {"mammoth", "interactive"};`
```

Note that it’s not necessary to specify the size of the array within the square brackets. If you want to, you can put a `2` between the square brackets.

Instead of `my_array`, we want to access `string_array`. Let’s access the very first element in that array:

````std:: cout << string_array[0];`
```

By running the code, you will see “mammoth” appear at the bottom of your screen, which is the first element of our string array.

To check that things are working properly throughout the array, check the last element. Replace `string_array[0];` with `string_array[1];`.

If you run the code, you should see “interactive” appear at the bottom of your screen.

Now let’s learn how to modify a single element in an array. Going back to the integer array example, let’s say we want to replace `2` with `5`. In the main function, add `my_array[1] = 5`.

Also, we’ll be accessing `my_array` this time, so change `string_array` to `my_array`. As well, because we’ll be accessing Element One, you can leave the `[1]` where it is.

Your code should look like the following:

````//g++ 4.9.3`

`#include `

`int my_array[3]= {1, 2, 3};`

`std::string string_array[2] = {"mammoth", "interactive"};`

`int main ()`
`{`
`my_array[1] = 5;`
`std:cout << my_array[1];`
`}`
```

Print element one by running the code, and you should see a “5” appear on your screen.

There you have it! To learn even more about coding, sign up for our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes. See you soon!

## Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

### EMAIL US

support@mammothinteractive.com