ARRAY

An array is a collection of data, of the same type, which are stored in a contiguous memory locations. It is a data  structure as the stored data are organized in a sequential manner. For 'n' elements, there are total (n-1) indexes starting with '0' for the first element. Index numbers are used to access the elements of the array.


Types of Arrays:

One-Dimensional Array

An array, in which, the elements are stored in the form of row is called 1D array.

Syntax: 

data_type array_name[number of elements];


To access Nth element of an array: arr[N-1]


Example:

//C++ program demonstrating 1D array

#include <iostream> 

using namespace std; 

int main() 

int arr[6];   //declaration of 1d array

arr[0] = 5;   //initialization 

arr[1] = 19;  

arr[2] = -2; 

arr[3] = 17; 

arr[2*2] = 11;      //same as arr[4] = 11

arr[5] = arr[2] ; 


cout << arr[0] << " " << arr[1] << " " << arr[2] << " " << arr[3] << " "<< arr[4]<< " " << arr[5]; 

        return 0; 

OUTPUT:


Two-Dimensional Array 

An array, in which, the elements are stored in the form of rows and columns is called a 2D array. 

Syntax:

data_type  array_name[size1][size2]....[sizeN];

 

Initialization and accessing elements of 2D Array:

arr[2][3]= { { 6, 7, 3 }, {9, 4, 2} };

Here, it make use of nested braces. Each set of inner braces represents a row which contains 3 elements i.e.  number of columns is 3.

The elements in 2D array can be accessed by using row indexes and column indexes.

To access element of Nth row and Mth column: arr[N-1][M-1]

For example,

Access element of 2nd row and 2nd column : arr[1][1]= 4

Example:

//C++ program demonstrating 2D array

#include<iostream>

using namespace std;

int main()

{

   int arr[3][2] = {{0,1}, {2,3}, {4,5}};    // declaration and intialization done together

   cout << arr[0][0] <<" "<< arr[0][1] <<" "<<"\n"<< arr[1][0] <<" "<< arr[1][1] <<" "<<"\n"<< arr[2][0] <<" "<< arr[2][1]<< endl;

   return 0;

} 

OUTPUT:



Advantages:

1. Array is used to represent multiple data items of same type using only single name.

2. Any array elements can be accessed randomly using indices of the respective data item. 

3. Other data structures such as stacks, queues etc. can be implemented using array.

4. 2D array is used for matrix representation.

5. Avoids overflow or shortage of memory.

6. Arrays are used for different sorting techniques such as bubble sort, merge sort etc.


Disadvantages:

1. An array is of fixed size which means, once after declaration, the array size can't be altered. Hence, the allocated memory can neither be     increased nor decreased.

2. Allocation of more memory than it is required, leads to wastage of the memory space.

3. The total number of elements to be stored in the array must be known in advance due to its fixed size.


Arrays as Class Members in C++

Arrays can be declared as private, public or protected members of a class. And, the member functions can perform any operation on the arrays which are data member of the class.

Syntax:

class class_name

{

     //data members

  data_type array_name[size];

  public:

    //member functions

};


Let's understand the concept of array as member of a class with the help of an example.

Example:

//C++ program to demonstrate array as class member

#include<iostream>

using namespace std;

const int size = 5;

class student   

 {

    int roll;

    int marks[size];    //array declaration as a private member

    public:

    void showdata ()

    {

      cout<<"\nEnter Roll No.: ";

      cin>>roll;

      for(int i=0; i<size; i++)

      {

        cout<<"Marks scored in subject"<<(i+1)<<": ";

        cin>>marks[i] ;

      }

}

    void total_marks ()    //calculation of total marks

    {

      int total=0;

      for(int i=0; i<size; i++)

      total = total + marks[i];

      cout<<"\n\nTotal marks obtained: "<<total;

    }

};

int main()

{

   student stu;

   stu.showdata() ;

   stu.total_marks() ;

   return 0;

}


OUTPUT:




please note that the elements of an array occupy contiguous memory locations along with other data members of the object. Foe example, when the objects 'stu' of class 'student' is declare, then memory space is allocated for both roll and marks.

Array of Objects

An array of objects means a collection of similar objects referenced by a common name. It is declared just as an array of any built-in type. Each element of an array is an object of the same class. Thus, an array of a class type is also known as an array of objects. Hence, it indicates that a class is similar to a type.


Syntax:

class class_name

{

  data members

  member functions

};

class_name array_name[size];


Example:

//C++ program to demonstrate array of objects

#include<iostream>

#include<conio.h>

using namespace std;

class alpha

{

   char letter[21];

   int val;

   public:

   void print_data()

   {

     cout<<"\n\tEnter the Letter: ";

     cin>>letter;

     cout<<"\n\tThe value is: ";

     cin>>val;

   }

   void put_data()

   {

      cout<<"\n"<<letter<<"\t"<<val;

   }

};

int main()

{

  int i;

  alpha a[2];     //declaring an array of 2 objects of class alpha

  for(i=0;i<2;i++)

  {

      cout<<"\nEnter details of Alphabet"<<i+1;

      a[i].print_data();

  }

  cout<<"\nDetails of Alphabets";

  for(i=0;i<2;i++)

  a[i].put_data();

}

OUTPUT:



Array of objects makes it easier to program the same operation over all of the objects. 

Note: The real objects are not stored in the array, only the references of objects are stored in it.

If you don't know what function is, we suggest that you read our function in oops tutorial.