Array of Values
Explore the concept of arrays in C++ by learning how to declare, access, and manipulate elements using indexes. Understand how to calculate array sizes and practice with several example programs designed to strengthen your array handling skills. This lesson prepares you to use arrays effectively in your C++ projects.
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 ...