Introduction to C++ | Arrays

C++ Arrays

An array in C++ is a collection of variables of the same type, stored in contiguous memory locations. It allows you to store multiple values under a single variable name and access them using an index.

What is an Array?

An array is a fixed-size sequence of elements of the same data type. The size of the array is defined at the time of declaration and cannot be changed during runtime. Each element of the array can be accessed by its index, where the first element has an index of 0.

Syntax for Declaring an Array

To declare an array in C++, you need to specify the type of elements, followed by the array name and size (number of elements).

dataType arrayName[arraySize];

Example of Array Declaration and Initialization

The following example shows how to declare and initialize an array:

Code Example: Array Declaration and Initialization


        {% raw %}
        #include <iostream>
        using namespace std;
        
        int main() {
            // Declare and initialize an array
            int numbers[5] = {1, 2, 3, 4, 5};
        
            // Accessing elements using the index
            cout << "First element: " << numbers[0] << endl;
            cout << "Second element: " << numbers[1] << endl;
        
            return 0;
        }
        {% endraw %}
                    

Output

First element: 1
Second element: 2

Accessing Array Elements

Array elements can be accessed using their index. Remember, the index of the first element is always 0.

Multi-Dimensional Arrays

C++ supports multi-dimensional arrays, such as 2D or 3D arrays. A 2D array is often used to represent matrices or tables.

Syntax for a 2D Array
dataType arrayName[rows][columns];

Example of a 2D Array

Here’s an example that shows how to declare and access elements in a 2D array:

Code Example: 2D Array


        {% raw %}
        #include <iostream>
        using namespace std;
        
        int main() {
            // Declare and initialize a 2D array
            int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
        
            // Accessing elements
            cout << "Element at [0][1]: " << matrix[0][1] << endl;
            cout << "Element at [1][2]: " << matrix[1][2] << endl;
        
            return 0;
        }
        {% endraw %}
                    

Output

Element at [0][1]: 2
Element at [1][2]: 6

Accessing Array Elements in Multi-Dimensional Arrays

In multi-dimensional arrays, each element can be accessed using a combination of indices. For example, for a 2D array, you need to specify both the row and the column index to access an element.

Array Initialization Methods

C++ allows different ways to initialize arrays. Apart from manually initializing elements, there are other methods available:

Example of Default and Partial Initialization

Here's an example illustrating default and partial initialization of an array:

Code Example: Default and Partial Initialization


        {% raw %}
        #include <iostream>
        using namespace std;
        
        int main() {
            // Default Initialization
            int arr1[5]; // All elements are initialized to 0
        
            // Partial Initialization
            int arr2[5] = {1, 2}; // Remaining elements are initialized to 0
        
            // Displaying values
            cout << "Default Initialization: ";
            for (int i = 0; i < 5; i++) {
                cout << arr1[i] << " ";
            }
        
            cout << "nPartial Initialization: ";
            for (int i = 0; i < 5; i++) {
                cout << arr2[i] << " ";
            }
            cout << endl;
        
            return 0;
        }
        {% endraw %}
                    

Output

Default Initialization: 0 0 0 0 0
Partial Initialization: 1 2 0 0 0

Arrays in Functions

Arrays can be passed to functions either by reference or by value. When you pass an array to a function, it decays to a pointer. However, the size of the array is lost, so it is a good practice to also pass the size of the array to the function.

Example of Passing Arrays to Functions

Code Example: Passing Arrays to Functions


        {% raw %}
        #include <iostream>
        using namespace std;
        
        void printArray(int arr[], int size) {
            for (int i = 0; i < size; i++) {
                cout << arr[i] << " ";
            }
            cout << endl;
        }
        
        int main() {
            int arr[5] = {1, 2, 3, 4, 5};
            printArray(arr, 5); // Passing array and its size
            return 0;
        }
        {% endraw %}
                    

Output

1 2 3 4 5

Conclusion

Arrays are a fundamental concept in C++ programming, enabling efficient storage and access of multiple elements of the same type. By mastering array declaration, initialization, access, and passing arrays to functions, you will be able to tackle a wide variety of programming problems.