Arrays

  • An array is a collection of variables of the same type that are referred to by a common name
  • . Arrays may have from one to several dimensions, although the one-dimensional array is the most common.
  • Arrays offer a convenient means of creating lists of related variables.
  • The array that you will probably use most often is the character array, because it is used to hold a character string.
  • The C++ language does not define a built-in string data type. Instead, strings are implemented as arrays of characters.

 

Types of Arrays

There are 3 types of arrays.

  • One Dimensionl Array
  • Two Dimensionl Array
  • Multi Dimensionl Array

One-dimensional arrays

  • A one-dimensional array is a list of related variables. Such lists are common in programming.

One-dimensional array declaration

 Data type name[size];

Example:-

#include using namespace std; 
int main()
 {
  int age[4]; //array ‘age’ of 4 ints 
  for(int j=0; j<4; j++) //get 4 ages
   { 
    cout << “Enter an age: “;
    cout > age[j]; //access array element
   } 
  for(j=0; j<4; j++) //display 4 ages
  { 
   cout << “You entered “ << age[j] << endl;
  }
  return 0;
}

Output

 Enter an age: 44
 Enter an age: 16
 Enter an age: 23
 Enter an age: 68

 You entered 44
 You entered 16
 You entered 23
 You entered 68

 

Two-Dimensional Arrays

  • C++ allows multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array.
  • A two-dimensional array is, in essence, a list of one-dimensional arrays.
  • A two dimensional array call be think as a table which will have x number of rows and y number of columns.​​​​​​​
 Data type array_name[x][y];

 

Multidimensional Arrays

  •  C++ programming language allows multi dimensionl arrays.
  •  Multi Dimensionl Array may be initialized by specifying bracketed values for each row.​​​​​​​

Syntax:-

 Data_type array_name[array size1][array size2]...[array sizeN];

Example:-

 int multidim[4][10][3];