Arrays

COS1511 - Introduction To Programming I · Learning C++

Arrays

An array is a collection of items stored at contiguous memory locations. It is used to store multiple values of the same type in a single variable. Arrays are particularly useful when you need to manage large amounts of data efficiently. In C++, arrays can be declared to hold a fixed number of elements.

Declaring Arrays

To declare an array in C++, you specify the type of the elements, followed by the name of the array and the size in square brackets. For example:

int numbers[5];

This line declares an array named numbers that can hold five integers. The indices of this array range from 0 to 4.

Remember: Array indices start at 0 in C++. The first element is accessed with index 0, the second with index 1, and so on.

Initialising Arrays

You can initialise an array at the time of declaration. For example:

int numbers[5] = {1, 2, 3, 4, 5};

This line creates an array of integers with values 1, 2, 3, 4, and 5. If you do not specify the size, C++ will automatically determine it from the number of initial values:

int numbers[] = {1, 2, 3, 4, 5};

This also creates an array of size 5.

Accessing Array Elements

You can access an element of an array using its index. For example:

int firstNumber = numbers[0];

This line assigns the value of the first element of the numbers array to the variable firstNumber. To modify an element, you can do the following:

numbers[0] = 10;

This changes the first element of the array to 10.

Watch out: Accessing an index that is out of bounds (not between 0 and 4 for this example) results in undefined behaviour. Always ensure your indices are valid.

Looping Through Arrays

To perform operations on each element of an array, you can use a loop. A common choice is the for loop. Here is an example that prints all elements of the numbers array:

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

This loop iterates from index 0 to 4, printing each element of the array.

Multidimensional Arrays

Arrays can have more than one dimension. A two-dimensional array can be thought of as a table with rows and columns. To declare a two-dimensional array, you specify two sizes in the brackets:

int matrix[3][4];

This declares a 3x4 matrix (3 rows and 4 columns). You can initialise it like this:

int matrix[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

To access an element in a two-dimensional array, you use two indices:

int value = matrix[1][2]; // This gets the value 7

Dynamic Arrays

In some cases, you may not know the size of the array at compile time. You can create dynamic arrays using pointers and the new keyword. For example:

int* dynamicArray = new int[n];

This creates an array of size n dynamically. Remember to free the memory when you are done using the array:

delete[] dynamicArray;

Common Array Operations

Here are some common operations you might perform with arrays:

  • Finding the length: You can find the number of elements in a static array using the sizeof operator:
int length = sizeof(numbers) / sizeof(numbers[0]);
  • Copying arrays: To copy one array to another, you can use a loop:
for (int i = 0; i < 5; i++) {
    newArray[i] = numbers[i];
}

Practical Example

Let’s put everything together in a practical example. This program creates an array of integers, fills it with values, and prints them:

#include 
using namespace std;

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};
    
    for (int i = 0; i < 5; i++) {
        cout << "Element at index " << i << ": " << numbers[i] << endl;
    }
    
    return 0;
}

This program will output:

Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

Summary

  • An array is a collection of elements of the same type.
  • Arrays can be declared with a fixed size.
  • Access elements using indices starting from 0.
  • Use loops to iterate over arrays.
  • Two-dimensional arrays can represent tables.
  • Dynamic arrays can be created using pointers.

Check your understanding

  • What is the index of the last element in an array of size 10?
  • How do you declare a two-dimensional array in C++?
  • What happens if you try to access an array index that is out of bounds?
  • How can you dynamically allocate memory for an array in C++?