C++ Arrays

C++ arrays store one or more than one element. An array contains a number of items. A single variable is used to define an array of multiple items. Each item in an array is called an element. The image below shows an array that has 10 elements. Hence the length of array is 10. The image also displays the indices (0-9) of the array.

C++ arrays are used to define a series of elements so that there is no need to define them separately. Arrays contain a collection of same type of data. Each element of an array has its own identity as it is stored in contiguous memory locations. It can be referred to by its own index value. An array index starts from zero. The first entered element in the array has 0 index. The next element will have index 1 and so on.

For example, if a user wishes to define different colors. Instead of defining all colors with a new variable (color1, color2, color3), he/she can use a single array (colors) to define all colors.

C++ Arrays Declaration

The syntax for declaring an array is:

Here ArrayType defines the data type of array. Array can have any of the allowed C++ data types. ArrayName defines the name of the array. ArraySize defines size of the array in square brackets. The size of array is usually equal to the number of elements in the array. It should be an integer greater than 0.

For example an array containing 7 elements can be declared as:

C++ Arrays Initialization

Array can be of type int, char, double, float, or string. For example, int array can be initialized as:

Double and float type arrays have same syntax as int arrays. Elements of char type array are written in single quotes. Char array is written as:

Elements of string type array are written inside double quotes. An array having elements of type string, can be initialized as:

A programmer can either initialize an array at the time of declaration or can declare an empty array. Programmer can fill data in empty array later on. Usually, for user input, we use empty array.

A programmer can also initialize an array of indefinite size, by keeping the square brackets empty. The compiler determines the size of array on compile time.

The number of elements in array should not exceed the size of array. User can also assign a variable as array size. For example:

Accessing Array Elements

After an array declaration and initialization, user can access its elements by calling the array with specific index value. Each element of array is separated by a comma. For example, if we want to get the third element of an array, we write:

Note that the index of last element of an array is one number less than the size of array.

Printing Arrays

Loops are used to display arrays. Below code displays all elements of an array:

#include <iostream>
using namespace std;
int main() {
    string colors[7] = {"red", "yellow", "orange", "green", "blue", "indigo", "violet"};
    cout << "The colors are: ";
    for (int i = 0; i < 7; ++i) {
        cout << colors[i] << "  ";
    }
    return 0;
}

Printing Arrays Elements

The below code displays how to print specific elements of array

#include <iostream>
using namespace std;
int main() {
    string colors[7] = {"red", "yellow", "orange", "green", "blue", "indigo", "violet"};
    cout << "The basic colors are:" << endl;
    cout << colors[0] << endl;
    cout << colors[1] << endl;
    cout << colors[3] << endl;
    return 0;
}

The output of the above code will be:

Changing Array Elements

User can change elements of an array and assign new value to them.

#include <iostream>
using namespace std;
int main() {
    string colors[7] = {"red", "yellow", "orange", "green", "blue", "indigo", "violet"};
    cout << "The neutral colors are:" << endl;
    colors[0] = "black";
    colors[1] = "white";
    colors[2] = "grey";
    cout << colors[0] << endl;
    cout << colors[1] << endl;
    cout << colors[2] << endl;
    return 0;
}

Getting Arrays as user input

The below code shows how to take arrays as user input:

#include <iostream>
using namespace std;
int main() {
    int numbers[7];
    cout << "Enter any 7 integers:" << endl;
    for (int i = 0; i < 7; ++i) {
        cin >> numbers[i];
    }
    cout << "The numbers you entered are:";
    for (int i = 0; i < 7; ++i) {
        cout << numbers[i] << "  ";
    }
    
    return 0;
}

Arrays Library

Programmers can also initialize arrays by using array library. The program includes the array header file in the beginning. The syntax of initializing arrays using array library is:

Below code displays arrays declared using array library:

#include <iostream>
#include <array>
using namespace std;

int main()
{
  array<int,3> arr {10,20,30};
  cout << "The array is: ";
  for (int num : arr)
    cout << num << " ";
}

Arrays as parameters

In C++ programming language, functions take variables as their input parameters. These parameters can also be in the form of arrays.

For example, the below code displays a function that takes an int array and its length as inputs. It then returns the sum of elements of the array.

#include <iostream>
using namespace std;
void sumarray (int arr[], int length) {
  int sum = 0;
  for (int n=0; n<length; ++n)
  {
      sum = sum + arr[n];
  }
  cout << sum;
}
int main ()
{
  int arr1[] = {5, 10, 15};
  sumarray (arr1, 3);
}

Other than single arrays, there are also Multi-dimensional (2D, 3D) arrays present in C++ programming language.