C++ Arrays

This is a tutorial on the use of Arrays in C++

What are C++ Arrays?

In programming, an Array is a data structure which can store a fixed number of elements of the same data type within it. But where do we use Arrays in C++?

Let’s say we wanted to store 1000 values somewhere. With our current approach we would have to create a 1000 variables in order to make this possible. Naturally we see the need for a better solution than this.

Hence, we use Arrays, where we can store an almost infinite number of values within it. You may think of an Array as a collection of variables bound together by a common name.


Defining C++ Arrays

The syntax for defining arrays is shown below.

type arrayName [size];
  • type: The type of data to be stored in the array. The array may be able to store many hundreds of variables, but they must all be bound by the same name and data type.
  • arrayName: The name for the array.
  • size: The size of the array, or the “number of variables” inside it. Passing the value of 1000 will allow a 1000 different values to be stored.

Below is a real life example of us declaring an Array.

int myArray[5];

Assigning values

If you’ve declared an Array of size 5, you’ve got to also assign 5 values to it. While you can assign the value for each index personally (we’ll cover this in the next section), when creating Arrays we take the following approach.

#include <iostream>
using namespace std;

int main()
{
    int myArray[5] = {1,2,3,4,5};
}

As you might expect, the order of how you assign the values determines their position in the array. For instance, in the above example 1 is located at the start and 5 is located at the end.

In C/C+ Arrays, any missing elements will be initialized to 0. This means that if there is space for 20 elements, and only 10 were defined (given values), the other 10 will take on the value of 0.


Accessing the Array

Learning how to access and manipulate the array you just created is of utmost importance. First you should know about it’s indexing system.

In C++ the position of the first value in the array is called position 0 or “index 0”. The next is called index 1, the third is called index 2 and so on.

In the below example we’re going to print out a few values.

#include <iostream>
using namespace std;

int main()
{
    int myArray[5] = {1,2,3,4,5};

    cout << myArray[0] << endl;
    cout << myArray[1] << endl;
    cout << myArray[4] << endl;
}
1
2
5

Through the use of “indexes” you can also alter the individual values within an array. Below is a simple demonstration.

#include <iostream>
using namespace std;

int main()
{
    int myArray[5] = {1,2,3,4,5};

    cout << myArray[1] << endl;
    myArray[1] = 8;
    cout << myArray[1] << endl;
}
2
8

Iterating over Arrays

There are multiple ways in which one may “iterate” over an Array, but the method shown below is by far the simplest which also makes it the best.

The benefit of iteration is that it allows us to access each individual value within the Array. In the example below we’ll simply be printing them out though.

#include <iostream>
using namespace std;

int main()
{
    int myArray[5] = {1,2,3,4,5};

    for (int& x : myArray) {
        cout << x << endl;
    }
}
1
2
3
4
5

There are other options that you can use in C++ for storing objects, such as Lists and Vectors. In fact, in many situations, it is actually not suitable to use an Array, rather you should be using either a List or Vector. Follow the link to learn more about their differences.


C++ Multi Dimensional Arrays

So far we’ve been working with One dimensional arrays. We’ll now begin using 2 Dimensional Arrays in the below examples to demonstrate how multi dimensional Arrays work.

You can think of multidimensional Arrays of having arrays within arrays. This gives it more “depth or in more technical terms a new “dimension”. Previously, something like myArray[1] would point to a single variable where a single value could be stored. In 2 dimensional arrays myArray[1] would point to an array where multiple values can be stored.

The method of creating and initializing it are mostly the same, with some small changes. Previously we just had to define a single dimension, now we must do it for both.

int myArray[5][3];

The above array is capable of storing 15 values. This is because it has 5 one dimensional arrays inside it which can each hold 3 values. The simple formula is number of arrays multiplied by the number of values each array can hold.


Iterating over a 2D Array

In order to iterate completely (element by element) over a 2D Array, you need two for loops. The first for loops iterates over the parent array, which contains all the smaller arrays. The second for loop iterates over the arrays within the main array.

for (int i = 0; i < 5;  i++) {
    for (int j = 0; j < 3; j++) {
        cout << myArray[i][j] << endl;
    }
}

If you have understood how to handle 2-Dimensional arrays, you will be able to create 3 and 4 (and so on) dimensional arrays as well.


This marks the end of the C++ Arrays tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments