A set of similar data types is called an array these data types can be of any initial type. The C++ language provides a capability that enables the user to design a set of similar data types, called array. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.There are 3 types of array


1. One Dimensional array

It is a collection of data elements arranged in row is called 1 D array.


2. Two Dimensional array

 It is a collection of 1D array and arranged in row and column are 2d array.

            

            3. Multi-dimensional arrays

C++ supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array.

 

1. One Dimensional array

 

Array Declaration

To begin with, like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. In our program we have done this with the statement:

            int marks[5] ;

 

Accessing Elements of an Array


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

{

cout<<”Enter marks ";

cin>>marks[i];

}

 

Array Initialisation

So far we have used arrays that did not have any values in them to begin with. We managed to store values in them during program execution. Let us now see how to initialize an array while declaring it. Following are a few examples that demonstrate this.

int num[6] = { 2, 4, 12, 5, 45, 5 } ;

int n[ ] = { 2, 4, 12, 5, 45, 5 } ;

float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;


Example of One Dimensional array

#include<iostream.h>

#include<conio.h>

void main(){

            int i,v=0,a[5];

            clrscr();

            cout<<"Enter value of i=";

            for(i=0;i<5;i++){

                        cin>>a[i];

            }

            for(i=0;i<5;i++){

                   v=v+a[i];

            }

            cout<<"Sum is=" <<v;

getch();

}

Output

        Enter value of i=6

        10

        4

        20

        8

        Sum is=48


2. Two Dimensional array

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.

 

Array Declaration

How do we initialize a two-dimensional array? As simple as this...

            int arr[2][2]

           

Array Initialisation

Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns.

int arr[3][4] = {

{0, 1, 2, 3} ,

{4, 5, 6, 7} ,

{8, 9, 10, 11}

};

 

Accessing Elements of an Array

An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example:

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

{

            for ( j = 0 ; j <= 5 ; j++ )

            {

cin>>arr[i][j];

cout<<”Enter marks";

cout<<arr[i][j];

                        }

}


Example of Two Dimensional array

#include<iostream.h>

#include<conio.h>

void main(){

            int i,j,a[3][3];

            clrscr();

            cout<<"Enter elements in matrix=";

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    cin>>a[i][j];

                        }

            }

            cout<<endl <<"Elements in array=” <<endl;

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    cout<<a[i][j] <<” “;

                        }

                        cout<<endl;

            }

getch();

}

Output

        Enter elements of matrix=9

        8

        7

        6

        5

        4

        3

        2

        1

        Elements in array=

        9          8          7

        6          5          4

        3           2         1


3. Multi-dimensional arrays


Example of Multi-Dimensional array

#include<iostream.h>

#include<conio.h>

void main(){

            int i,j,k,a[3][3],b[3][3],c[3][3]={0};

            clrscr();

            cout<<"Enter elements in matrix a=";

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    cin>>a[i][j];

                         }

            }

            cout<<"Enter elements in matrix b=";

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    cin>>b[i][j];

                        }

            }

            clrscr();

            cout<<endl <<"Matrix Multiplication:";

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    for(k=0;k<3;k++){

                                                c[i][j]=c[i][j]+a[i][k]*b[k][j];

                                    }

                        }

            }

            cout<<endl <<"Matrix a:";     

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    cout<<endl <<a[i][j] <<” “;

                         }

                         cout<<endl;

            }

            cout<<endl <<"Matrix b:";

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    cout<<endl <<b[i][j] <<” “;

                         }

                         cout<<endl;

            }

            cout<<endl <<"Matrix c:";

            for(i=0;i<3;i++){

                        for(j=0;j<3;j++){

                                    cout<<endl <<c[i][j] <<” “;

                         }

                         cout<<endl;

            }

getch();

}

Output

        Enter elements in matrix a =1

        2

        3

        4

        5

        6

        7

        8

        9

        Enter elements in matrix b =1

        2

        3

        4

        5

        6

        7

        8

        9

        Matrix Multiplication

        Matrix a :

        1          2          3

        4          5          6

        7          8          9

        Matrix b :

        1          2          3

        4          5          6

        7          8          9

        Matrix c :

        30        36        42

        66        81        96

        102      126      150