Arrays are fundamental data structures in C++ that allow you to store multiple values of the same type in a single variable. They provide an efficient way to manage collections of data, making them essential for various programming tasks such as handling lists, matrices, and more.
In this tutorial, we'll explore 1D arrays (one-dimensional arrays) and multidimensional arrays, including how to declare, initialize, access elements, determine array size, and understand the concept of array decay. By the end of this lesson, you'll have a solid understanding of how to work with arrays in C++.
A 1D array is essentially a list of elements stored in contiguous memory locations. You can declare an array by specifying its type and size, or by initializing it directly.
To declare a 1D array, you need to specify the data type followed by the array name and the number of elements in square brackets:
1#include <iostream>2using namespace std;34int main() {5int numbers[5]; // Declare an array of 5 integers67return 0;8}
You can initialize an array at the time of declaration by providing a list of values enclosed in curly braces:
1#include <iostream>2using namespace std;34int main() {5int numbers[5] = {1, 2, 3, 4, 5}; // Initialize an array with 5 integers67return 0;8}
If you provide fewer initializers than the size of the array, the remaining elements are automatically initialized to zero:
1#include <iostream>2using namespace std;34int main() {5int numbers[5] = {1, 2}; // Initialize first two elements; others are set to 067return 0;8}
Array elements are accessed using their index, which starts from 0. You can access and modify individual elements as follows:
1#include <iostream>2using namespace std;34int main() {5int numbers[5] = {1, 2, 3, 4, 5};67cout << "First element: " << numbers[0] << endl; // Output: 18cout << "Third element: " << numbers[2] << endl; // Output: 3910numbers[1] = 10; // Modify the second element11cout << "Modified second element: " << numbers[1] << endl; // Output: 101213return 0;14}
Multidimensional arrays are arrays of arrays. The most common type is a two-dimensional array, which can be visualized as a table or matrix.
To declare a 2D array, you specify the data type followed by two sets of square brackets representing rows and columns:
1#include <iostream>2using namespace std;34int main() {5int matrix[3][4]; // Declare a 3x4 matrix67return 0;8}
You can initialize a 2D array using nested curly braces:
1#include <iostream>2using namespace std;34int main() {5int matrix[3][4] = {6{1, 2, 3, 4},7{5, 6, 7, 8},8{9, 10, 11, 12}9};1011return 0;12}
Access elements in a 2D array using two indices: the row index and the column index:
1#include <iostream>2using namespace std;34int main() {5int matrix[3][4] = {6{1, 2, 3, 4},7{5, 6, 7, 8},8{9, 10, 11, 12}9};1011cout << "Element at row 1, column 2: " << matrix[1][2] << endl; // Output: 71213return 0;14}
The size of an array can be determined using the sizeof operator. However, note that this returns the total size in bytes, not the number of elements:
1#include <iostream>2using namespace std;34int main() {5int numbers[5] = {1, 2, 3, 4, 5};67cout << "Total size in bytes: " << sizeof(numbers) << endl; // Output depends on the system8cout << "Number of elements: " << sizeof(numbers) / sizeof(numbers[0]) << endl; // Output: 5910return 0;11}
In C++, when you pass an array to a function, it decays into a pointer to its first element. This means that the size information is lost:
1#include <iostream>2using namespace std;34void printArraySize(int arr[]) {5cout << "Size of array in function: " << sizeof(arr) << endl; // Output depends on the system6}78int main() {9int numbers[5] = {1, 2, 3, 4, 5};1011cout << "Size of array in main: " << sizeof(numbers) << endl; // Output depends on the system1213printArraySize(numbers);1415return 0;16}
Warning
Let's create a practical example that multiplies two matrices using 2D arrays:
1#include <iostream>2using namespace std;34void multiplyMatrices(int A[3][3], int B[3][3], int result[3][3]) {5for (int i = 0; i < 3; ++i) {6for (int j = 0; j < 3; ++j) {7result[i][j] = 0;8for (int k = 0; k < 3; ++k) {9result[i][j] += A[i][k] * B[k][j];10}11}12}13}1415void printMatrix(int matrix[3][3]) {16for (int i = 0; i < 3; ++i) {17for (int j = 0; j < 3; ++j) {18cout << matrix[i][j] << " ";19}20cout << endl;21}22}2324int main() {25int A[3][3] = {26{1, 2, 3},27{4, 5, 6},28{7, 8, 9}29};3031int B[3][3] = {32{9, 8, 7},33{6, 5, 4},34{3, 2, 1}35};3637int result[3][3];3839multiplyMatrices(A, B, result);4041cout << "Result of matrix multiplication:" << endl;42printMatrix(result);4344return 0;45}
Result of matrix multiplication: 30 24 18 84 69 54 138 114 90
sizeof to determine the total size in bytes; divide by element size to get the number of elements.In the next topic, we'll explore how to pass arrays to functions and handle them properly. Understanding array decay is crucial for working with arrays in functions, so make sure you're comfortable with these concepts before moving on.