Array

Two dimension Array in Cpp language

Two dimension Array in Cpp language

In this tutorial, we will learn about Two dimension Array in Cpp language

Array

In the Cpp programming language, an array is a special data structure of a fixed sequenced collection of the element which contains the same data types. An array can be used to represent a list of number(int, float, double etc..) or name (String) or other data type of similar elements.

We can group element of the similar data type in the single variable name

Three types of arrays are in the c++ programming language

Two-dimensional array

A two-dimensional array is an array within the array. We can simply identify using the table format.

Here, x is a two-dimensional array which contains 5 Rows and 5 Coloum and holds 25 places for the same type of values

 

int x[5][5];

Here, x is a two-dimensional array, it can hold a maximum 25 elements as int type

Two D Array

Declaration of two dimension array

Syntax

return_type Array_name[size][size];

Example

int marks[4][5];//it is a two dimensional array, it can hold 20 int type elements

char alphabet[5][4]; //it is a two dimensional array, it can hold 20 char type elements

Initializing of two dimension array

arr[1][2]=34;   //insert value 34 to array index arr[1][2]; 
arr[2][3]=63;  //insert value 34 to array index arr[2][3];

or

int marks[2][3]={45,67,46,87,32,35}  //method 2

or

int marks[2][3]={{67,43,78}{90,65,67}}  //method 3

or

int marks[][3]={{67,43,78}{90,65,67}} //method 4

 

The easy way to find the index of every elements Two dimension array

Collection of one-dimensional array create of the two-dimensional array

Find index of 2 D array using above diagram

Indices

x[0][0];

x[0][1]

x[0][2]

x[0][3]

x[0][4]

x[0][5]

We can find all index using this way

 

Example

program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int age[2][3];
    age[0][0]=12;
     age[0][1]=13;
      age[0][2]=14;
       age[1][0]=15;
        age[1][1]=16;
         age[1][2]=17;
    cout << "age[0][0] :" <<age[0][0]<< endl;
    cout << "age[0][1] :" <<age[0][1]<< endl;
    cout << "age[0][2] :" <<age[0][2]<< endl;
    cout << "age[1][0] :" <<age[1][0]<< endl;
    cout << "age[1][1] :" <<age[1][1]<< endl;
    cout << "age[1][2] :" <<age[1][2]<< endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

age[0][0]=12;
age[0][1]=13;
age[0][2]=14;
age[1][0]=15;
age[1][1]=16;
age[1][2]=17;

 

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int marks[2][5]={{16,65,36,87,54},{64,89,38,65,43}};
    // array declaration and initialization in same line
    cout<<"Array elements here"<<endl;
    cout<<marks[0][0]<<endl;
    cout<<marks[0][1]<<endl;
    cout<<marks[0][2]<<endl;
    cout<<marks[0][3]<<endl;
    cout<<marks[0][4]<<endl;
    cout<<marks[1][0]<<endl;
    cout<<marks[1][1]<<endl;
    cout<<marks[1][2]<<endl;
    cout<<marks[1][3]<<endl;
    cout<<marks[1][4]<<endl;
    //print every element stored in array
    cout << "End the program" << endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Array elements here

16
65
36
87
54
64
89
38
65
43

End the program

 

Access elements using nested for loop

Display of elements of the array from the two-dimensional array using nested for loop in C++

 

Input element using for loop to two D array

for(int i=0; i<=max; i++){
for(int j=0; j<=max; j++){
cin>>array_name[i][j];
}
}

Display element using for loop to two D array

for(int i=0; i<=max; i++){
for(int j=0; j<=max; j++){
cout<<array_name[i][j];
}
}

Program 1

 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int marks[3][4]=
    {
        {45,76,87,90},
        {43,73,28,85},
        {38,76,49,87}
    }; //declaration and initialization
    int i,j;
    for (i=0; i<3; i++){
        for (j=0; j<4; j++){
        printf("marks[%d][%d] :%d\n",i,j,marks[i][j]);
    }//display array elements using for loop
    }
    printf("end of program\n");
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

marks[0][0] :45
marks[0][1] :76
marks[0][2] :87
marks[0][3] :90
marks[1][0] :43
marks[1][1] :73
marks[1][2] :28
marks[1][3] :85
marks[2][0] :38
marks[2][1] :76
marks[2][2] :49
marks[2][3] :87
end of program

 

Program 2

C++ program to store the value entered by the user in two-dimensional array display elements using nested for loop

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int marks[3][4];
    cout<<"Enter the marks for Array \n"<<endl;
    for(int i=0; i<=2; i++){
            for(int j=0; j<=3; j++){

    cout<<"marks["<<i<<"]["<<j<<"]"<<":";
    cin>>marks[i][j];
            }
    }
cout<<"\nYou entered :"<<endl;
    for(int i=0; i<=2; i++){
            for(int j=0; j<=3; j++){
    cout<<"marks["<<i<<"]["<<j<<"]" <<":"<<marks[i][j]<<endl;
        }
    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

Enter the marks for array

marks[0][0]:34
marks[0][1]:78
marks[0][2]:89
marks[0][3]:90
marks[1][0]:79
marks[1][1]:95
marks[1][2]:57
marks[1][3]:76
marks[2][0]:34
marks[2][1]:57
marks[2][2]:93
marks[2][3]:58

You entered :
marks[0][0]:34
marks[0][1]:78
marks[0][2]:89
marks[0][3]:90
marks[1][0]:79
marks[1][1]:95
marks[1][2]:57
marks[1][3]:76
marks[2][0]:34
marks[2][1]:57
marks[2][2]:93
marks[2][3]:58

 

Access elements using nested while loop

Display of elements of the array from the two-dimensional array using nested while loop in C++

Input element using while loop

while(i<=2){
while(j<=2){
cin>>array_name[i][j];
}
}

Display elements using while loop

while(k<=2){
while(l<=2){
cout<<array_name[k][l];
}
}

We can display the element of the two-dimension array using nested while loop

Program 1

#include <iostream>

using namespace std;

int main()
{
    string name[3][4]={
    {"Mathu","Jhon","Sali","Maji"},
     {"Guru","Kuna","Ram","Ravi"},
      {"Roj","Joe","Mariya","mugunth"},

    };
    int i=0;
    cout << "Your employee names\n";
    while(i<=2){
            int j=0;
    while(j<=3){
    cout<<name[i][j] << endl;
    j++;
    }
    i++;
      }
    return 0;
}

 

When the above code is executed, it produces the following results:

your employees names
Mathu
Jhon
Sali
Maji
Guru
Kuna
Ram
Ravi
Roj
Joe
Mariya
Mugunth

 

program 2

C++ program to store the value entered by the user in two-dimensional array display elements using nested while loop

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int num[2][3];
    cout <<"Enter element for array"<< endl;
    int i=0;
    while(i<=1){
        int j=0;
        while(j<=2){
            cout <<"num["<<i<<"]["<<j<<"]=";
            cin>>num[i][j];
            j++;
                }
                i++;
    }
    cout << "\n your entered :" << endl;
    int k=0;
    while(k<=1){
        int l=0;
        while(l<=2){
            cout <<"num["<<k<<"]["<<l<<"]="<<num[k][l]<< endl;
            l++;
        }
        k++;
    }
    cout << "End of program" << endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Enter the elements for array
num[0][0]=45
num[0][1]=63
num[0][2]=58
num[1][0]=76
num[1][1]=45
num[1][2]=67

you entered :
num[0][0]=45
num[0][1]=63
num[0][2]=58
num[1][0]=76
num[1][1]=45
num[1][2]=67
End the program

 

Program 3

We can input and display elements of two-dimensional array using nested while loop in C++

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    string names[2][3];
    cout <<"Enter name for array"<< endl;
    int i=0;
    while(i<=1){
        int j=0;
        while(j<=2){
            cout <<"names["<<i<<"]["<<j<<"]=";
            cin>>names[i][j];
            j++;
                }
                i++;
    }
    cout << "\n This names are stored in array :" << endl;
    int k=0;
    while(k<=1){
        int l=0;
        while(l<=2){
            cout <<"names["<<k<<"]["<<l<<"]="<<names[k][l]<< endl;
            l++;
        }
        k++;
    }
    cout << "End of program" << endl;
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

Enter name for array
names[0][0]=sumi
names[0][1]=sugi
names[0][2]=jio
names[1][0]=jhoni
names[1][1]=jumi
names[1][2]=thamana

This names are stored in array
names[1][0]=sumi
names[1][1]=sugi
names[1][2]=jio
names[0][0]=jhoni
names[0][1]=jumi
names[0][2]=thamana
End of program

 

Suggested for you

The new keyword in Java

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

 

Three dimension Array in Cpp language
Single dimension Array in Cpp language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago