C++ Arrays

In C++, an array is a collection of elements of the same data type stored in contiguous memory locations. Arrays are defined with a fixed size and are accessed using an index.

Array Declaration

To declare an array, you specify the data type of its elements, followed by the array name and the number of elements in square brackets.

int numbers[5]; // Declares an integer array named "numbers" with 5 elements double prices[10]; // Declares a double array named "prices" with 10 elements

Array Initialization

You can initialize an array at the time of declaration.

int daysOfWeek[7] = {1, 2, 3, 4, 5, 6, 7}; // Initializes an array with values char vowels[] = {'a', 'e', 'i', 'o', 'u'}; // Initializes an array without specifying size

Accessing Array Elements

To access an element in an array, you use the array's name and the element's index. The index of an element is a number that represents the element's position in the array. The first element in an array has an index of 0, the second element has an index of 1, and so on.

int thirdDay = daysOfWeek[2]; // Accesses the 3rd element (index 2) char firstVowel = vowels[0]; // Accesses the 1st element (index 0)

Array Size

You can find the number of elements in an array using the sizeof operator.

int size = sizeof(numbers) / sizeof(numbers[0]); // Calculates the size of the array

Iterating Through Arrays

You can use loops to iterate through the elements of an array.

for (int i = 0; i < 5; i++) { cout << numbers[i] << " "; // Prints each element in the "numbers" array }

Array Initialization with Zero

If an array is not explicitly initialized, its elements are set to zero by default.

int counts[3]; // All elements are initialized to 0

Multidimensional arrays

C++ also supports multidimensional arrays. A multidimensional array is an array of arrays. For example, the following statement declares a two-dimensional array of integers:

int my_array[2][3];

To access an element in a multidimensional array, you use the array's name and the element's indices. The first index represents the element's position in the outer array, and the second index represents the element's position in the inner array.

The following statement prints the element at the first row and first column of the array my_array to the console:

std::cout << my_array[0][0] << std::endl;

Here are some additional examples of using arrays in C++:

// Declare an array of characters char my_string[10]; // Initialize the array with the word "Hello" my_string[0] = 'H'; my_string[1] = 'e'; my_string[2] = 'l'; my_string[3] = 'l'; my_string[4] = 'o'; my_string[5] = '\0'; // Null terminator // Print the string to the console std::cout << my_string << std::endl; // Declare a two-dimensional array of integers int my_matrix[3][3]; // Initialize the array with the values 1, 2, 3, 4, ..., 9 int counter = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { my_matrix[i][j] = counter++; } } // Print the matrix to the console for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { std::cout << my_matrix[i][j] << " "; } std::cout << std::endl; } //Output: Hello

Conclusion

C++ arrays are a fundamental data structure used to store and manipulate collections of elements. They provide efficient memory usage and are commonly used for tasks involving lists of data, such as storing temperatures for a week or characters in a string. Understanding array indexing, initialization, and iteration is essential for working with arrays in C++.