CH - 6 Arrays

Introduction to arrays, including declaration, initialization, and accessing elements

Arrays:

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.


That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the
same type, Int for example, with a unique identifier.


For example, an array to contain 5 integer values of type int called billy could be represented like this:

fig 6.1
Diag: fig 6.1

where each blank panel represents an element of the array, that in this case are integer values of type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length.


Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

fig 6.2
Diag: fig 6.2

where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets [ ]), specifies how many of these elements the array has to contain.
Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:

fig 6.3
Diag: fig 6.3

Initializing arrays:

When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, it's elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros.


In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each
one of its elements by enclosing the values in braces { }

For example:

fig 6.4
Diag: fig 6.4

This declaration would have created an array like this:

fig 6.5
Diag: fig 6.5

The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ]. For example, in the example of array billy we have declared that it has 5 values, one for each element.


When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets
empty [ ] . In this case, the compiler will assume a size for the array that matches the number of values included between braces { } :

fig 6.6
Diag: fig 6.6

After this declaration, array billy would be 5 ints long, since we have provided 5 initialization values.

Accessing the values of an array:

In any point of a program in which an array is visible, we can access the value of any of its elements individually
if it was a normal variable, thus being able to both read and modify its value. The format is as simple as:

fig 6.7
Diag: fig 6.7

Following the previous examples in which billy had 5 elements and each of those elements was of type int, the name which we can use to refer to each element is the following:

fig 6.8
Diag: fig 6.8

For example, to store the value 75 in the third element of billy, we could write the following statement:

fig 6.9
Diag: fig 6.9

and, for example, to pass the value of the third element of billy to a variable called a, we could write:

fig 6.10
Diag: fig 6.10

Therefore, the expression billy (2) is for all purposes like a variable of type int.
Notice that the third element of billy is specified billy(2] , since the first one is billy(0] , the second one is billy[1] and therefore, the third one is billy [2] . By this same reason, its last element is billy [4) . Therefore, if we write billy[5], we would be accessing the sixth element of billy and therefore exceeding the size of the array.

2D ARRAYS:

A 2D array, or two-dimensional array, is a data structure that stores elements in a grid-like format with rows and columns. It’s essentially an array of arrays, where each element is itself an array. Here’s a breakdown:

Declaration

To declare a 2D array, you specify the data type, the array name, and the number of rows and columns. For example, in C:

int array[3][4];

This creates a 2D array with 3 rows and 4 columns.

Initialization

You can initialize a 2D array in several ways. Here are two common methods:

1. Using an initializer list:

int array[3][4] = { 

    {1, 2, 3, 4}, 

    {5, 6, 7, 8}, 

    {9, 10, 11, 12} 

}; 

2. Using nested loops:

int array[3][4]; 

for (int i = 0; i < 3; i++) { 

    for (int j = 0; j < 4; j++) { 

        array[i][j] = i + j; 

    } 

}