Single dimension array

tutorial and example
tutorial and example

C++ Array

An array is a collection of data (elements) of same data types. The elements of array are allocated in contiguous memory allocation. Elements of array are accessed through index (indices) of that element.

Types of array

  • Single dimension array
  • Two dimension array
  • Multi dimension array

Declaration of Array

In the above declaration, data_type is C++ data type, array_Name is name of array and size is total size of elements.

Initialization of Array

Array is initialized with data type followed by array name and total size of elements. Here, elements are assigned into array.

Another way to initialize array is as follow:

Access of Array

Elements of array are accessed through the index (indices) of that element. The index of array is started from 0 and end at size-1 of array.

CPP Access of Array

Suppose we want to access the individual element of array, this can be done by passing index of the element in array name.

Single dimension array Example

Printing the elements of array using ‘for’ loop.