One Dimensionalal Aray

C++ Arrays Definition:

An array is a collection of similar data which are stored in consecutive memory location under common name.

In other words, an arrays are sets of values of the same type, which have the single name followed by an index.

C++ Initializing arrays

The variables are initialized when they are declared, in the same way the arrays can also be initialized.

Syntax:

 data_type array_name[size] = {element 1, element 2....};

Simple Example:

 int n[5]={10, 20, 30, 40, 50};  

Accessing array elements

The array elements are accessed by using the position of the element in the array.

The position of the array start with 0 and ends with (number_of_elements-10).

Example:

 int a[5];


  10   20   30   40   50  <=== Datas
 a[0] a[1] a[2] a[3] a[4] <=== Position

The user can access these elements in any order he likes and user them in the same way as simple variable.


Type Of Arrays:

There are 2 types of arrays in C++ programming:

  • One Dimensional Array
  • Multidimensional Array(Two dimensional)

One Dimensional Array ?

A one-dimensional array is a group of elements having the same datatype and same name. Individual elements are referred to using common name and unique index of the elements.


The simplest form of an array is one-dimensional-array. The array itself is given name and its elements are referred to by their subscripts. In C++, an array is denoted as follows:

An array is a collection of data that holds homogeneous values. That means values should be in same type

Syntax:

type variable_name[size]


Example 1:



Output:

 arr[0] =1
 arr[1] =2
 arr[2] =3
 arr[3] =4
 arr[4] =5