Creating and Using Arrays in C++
Declaring Arrays
An array is declared by specifying the type of its elements followed by the name of the array and the number of elements it can hold, enclosed in square brackets.
int numbers[5]; // Declare an array of 5 integers
Initializing Arrays
You can initialize an array at the time of declaration by providing a list of values enclosed in curly braces.
int numbers[5] = {1, 2, 3, 4, 5}; // Initialize an array with values
Accessing Array Elements
You can access individual elements of an array using their index, which starts from 0.
int firstElement = numbers[0]; // Access the first element
int secondElement = numbers[1]; // Access the second element
Modifying Array Elements
You can modify elements of an array by assigning new values to specific indices.
numbers[0] = 10; // Modify the first element
Conclusion
Arrays are essential for managing collections of data efficiently in C++. By understanding how to declare, initialize, access, and modify arrays, you can write more organized and effective programs. Practice creating and manipulating arrays with different types and sizes to deepen your understanding.