Search⌘ K
AI Features

Array of Values

Explore how to declare arrays in C++, access elements by index, and determine array size. Learn to write programs to sum elements, check palindrome arrays, reverse arrays, and manipulate array data effectively through practical examples.

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:

C++
#include <iostream>
using namespace std;
int main()
{
int arr[] = {10,20,30};
cout << "The array is defined" << endl;
return 0;
}

In the code above:

  • We declare a variable arr to assign {10,20,30}. These comma-separated values enclosed in curly brackets are called an initialization array.
  • We call the variable arr an 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, and the ...