Array of Values
Explore arrays in C++ including declaration, initialization, and accessing elements by index. Understand how to use sizeof to determine array size. Practice common array operations such as summing elements, testing for palindromes, and reversing arrays through guided examples to build foundational coding skills.
What is an array?
In C++, an array is a collection of the same type of values. It is generally a comma-separated range of values enclosed in curly brackets { } and stored as a single variable. For example, {10, 20, 30} is an array of integers. It can be stored as a variable as shown in the code below:
In the code above:
- We declare a variable
arrto assign{10,20,30}. These comma-separated values enclosed in curly brackets are called an initialization array. - We call the variable
arran array as indicated by[]after the variable name. Here the size 3 is determined by the number of values in the initialization array.
Accessing individual values
The individual values in an array are accessed through an index. An index is an integer representing the position of an individual value in the array. We enclose the index in square brackets after the array variable name. The first value is at index 0 ...