January 13, 2021

C++ Arrays

IN this lesson, we would consider Arrays. This is a very important topic, not just in C++, but across all programming languages. So, you need to understand it clearly.

 

1. Overview of C++ Arrays

Array in C++ is a data structure that stores fixed-length sequential collection of elements. These elements must be of the same type. In order words, we have a collection of variables of same type stored under similar name.

For example, if we have 100 scores, it would we could declare this hundred scores with just on name: score. Then we can access individual scores as score[0], score[1], score[2] and so on. Elements of an array are accessed using their index. That is the number in square brackets.

Arrays are stored in contiguous memory locations.

 

2. Array Declaration

Just like variable declaration, you also need to declare arrays before use. To declare an array, you need to specify the data type and the size of the array(that is the number of elements). The general syntax for a 1-dimensional array is:

type arrayName [ Size ];

For example, to declare an array of  50 integers, it would be:

int score[50]

Note that the size of the array must be integer and greater than zero.

 

3. Array Initialization

This is a way to assign initial values to array elements. For example, the code below declares array of 4 double and initializes them as well.

double balance[4] = {26.4, 3.4, 75.0, 750.50};

You can see that the number of elements in curly braces { } is same as the number in the declaration, 4.

You can also omit the size of the array like so

double score[] = {96.4, 67.4, 75.0, 75.50};

I would still work because the compiler would check the number of elements assigned and then reserve memory accordingly.

 

4. Addressing Array Elements

As mentioned before, each element of an array has an index. But note that the index is zero-based. So the first element has index of 0, the second element has index of 1 and so on.

To access an element, you use the name of the array with the index in square brackets [ ]. So the code below would retrieve the third score and assign it to a variable student_score

double student_score = score[2]

In the same way, you can assign a value to a particular array element. This would replace the existing value. For example, the code below would change the second score from 67.4 to 99.9.

score[1] = 99.9

 

5. Multidimensional Arrays

The array we’ve considered so far are 1-dimensional arrays. This means that they represent a list of numbers. However, we could also have arrays that could be used to represent a matrix.

A two-dimensional array looks like this:

Two dimensional arrays
Figure 1 – A 2-Dimensional Array

In this example, we have  a two dimensional array. We can define it using it’s rows and columns. So we can say it’s a 4 by 3 array (4 x 3 array). This means, 3 rows, 4 columns.

The general syntax to declare a 2D array is:

type arrayName [ no_of_rows ][ no_of_columns ];

So to create the the array in Figure 1 in C++, we would have:

int score[3][4];

However, to initialize it, you can think of creating 3 1-dimensional arrays separated with a comma. The code below does it:

int scores[3][4] = {  
   {34, 67, 12, 20} ,   /*  row 1 indexed by 0 */
   {98,  5, 11, 65} ,   /*  for row 2 indexed by 1 */
   {10,  4, 16, 45}     /*  for row 3 indexed by 2 */
};

You can see the use of nested braces. However, you can choose to remove then and use just one brace like a 1-dimensional array. The compiler will understand!

One thought on “C++ Arrays

Leave a Reply

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