Skip to content

Arrays

An array is a container that stores objects of the same type. The objects stored in an array do not have names; instead, they are accessed by their positions. The size of an array is fixed; the length of an array cannot be changed arbitrarily.

Defining Arrays

An array is declared as a[d], where a is the name of the array and d is the number of elements in the array. At compile time, d must be known, meaning d should be an integer constant expression.

1
2
3
4
unsigned int d1 = 42;
const int d2 = 42;
int arr1[d1];  // Error: d1 is not a constant expression
int arr2[d2];  // Correct: arr2 is an array of length 42

You cannot directly assign one array to another:

1
2
3
int arr1[3];
int arr2 = arr1;  // Error
arr2 = arr1;      // Error

It is advisable to define larger arrays as global variables. Local variables are created on the stack, and excessively large arrays (larger than the stack size) can cause a stack overflow, leading to a runtime error (RE). If an array is declared in the global scope, it will be created in the static area.

Accessing Array Elements

Array elements can be accessed using the subscript operator []. Array indices (the values inside the square brackets) start from 0. For example, an array with 10 elements has indices from 0 to 9, not 1 to 10. However, in competitive programming, for convenience, we often allocate arrays slightly larger and start accessing elements from index 1, ignoring the first element.

Example 1: Read an integer \(n\) from standard input, then read \(n\) numbers and store them in an array. Here, \(n \leq 1000\).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>
using namespace std;

int arr[1001];  // The index range of array arr is [0, 1001)

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
  }
}

Example 2: (Continuing from Example 1) Sum the elements in array arr and output the sum. It is guaranteed that the sum of all elements in the array is less than or equal to \(2^{31} - 1\).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int arr[1001];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> arr[i];
  }

  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += arr[i];
  }

  printf("%d\n", sum);
  return 0;
}

Out-of-Bounds Access

The array index \(\mathit{idx}\) should satisfy \(0 \leq \mathit{idx} < \mathit{size}\). If the index is outside this range, it results in undefined behavior, which can lead to unpredictable consequences such as segmentation faults or unintended modifications of other variables.

Multidimensional Arrays

A multidimensional array is essentially an "array of arrays," meaning the elements of the outer array are themselves arrays. A two-dimensional array requires two dimensions to define: the length of the array and the length of its elements. Accessing a two-dimensional array requires two indices:

1
2
int arr[3][4];  // An array of length 3, whose elements are "arrays of int of length 4"
arr[2][1] = 1;  // Accessing the two-dimensional array

We often use nested for loops to process two-dimensional arrays.

Example: Read two numbers \(n\) and \(m\) from standard input, representing the height and width of a black-and-white image, respectively, with \(n, m \leq 1000\). For the next \(n\) lines of data, each line contains \(m\) space-separated numbers representing the brightness values at each position. Now, read this image and store it in a two-dimensional array.

1
2
3
4
5
6
7
const int MAXN = 1001;
int pic[MAXN][MAXN];
int n, m;

cin >> n >> m;
for (int i = 1; i <= n; ++i)
  for (int j = 1; j <= m; ++j) cin >> pic[i][j];

Similarly, you can define three-dimensional, four-dimensional, and even higher-dimensional arrays.