Multi Dimensional Array in C++ | 2D Array Complete Guide

0
Multi Dimensional Array in C++ | 2D Array Complete Guide
Multi Dimensional Array in C++ | 2D Array Complete Guide


📑 Table of Contents 

What is Multidimensional Array

2-D Array in C: A two-dimensional array in C++ is a data structure that can hold multiple values arranged in row and columns using square brackets. Array is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value. 

Array can be used to group of data simultaneously. We can not fetch data from array value directly therefore we can use index point value. 

The indexing of array always start with row 0 and column index value is always integer value. Arrays is allow to store multiple values of the same data type in a single variable.

Two Dimensional Array in C++ Syntax

  • Data_type: The array data type of data that the array will hold (int, float, char, etc.).
  • Array_name: The name you choose for the 2D array.
  • Row: The number of rows in the 2D array.
  • Columns: The number of columns in the array.

data_type array_name[row_size][column_size]={Array_Values};
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};

C++ 2D Array Initialization

Array is the are several ways to initialize 2D arrays. The choice of initialization method depends on the requirements of your C++ program. Here are some common methods for initializing 2D arrays in C++.

C++ 2 Dimensional Array Initialization


int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
a[0][0]=10
a[0][1]=20
a[0][2]=30
a[1][0]=40
a[1][1]=50
a[1][2]=60
a[2][0]=70
a[2][1]=80
a[2][2]=90

The values to initialize the array with the number of elements inside the curly braces must match the size of the array.

We declare and initialize an integer array numbers with a size of row 3 and column size 3 and we provide 9 values.

Multidimensional Array in C++ Example Program

#include<iostream>
using namespace std;
int main(){
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
cout<<"Value of a[0][0]:"<<a[0][0]<<endl;

cout<<"Value of a[0][1]:"<<a[0][1]<<endl;
cout<<"Value of a[0][2]:"<<a[0][2]<<endl;
cout<<"Value of a[1][0]:"<<a[1][0]<<endl;
cout<<"Value of a[1][1]:"<<a[1][1]<<endl;
cout<<"Value of a[1][2]:"<<a[1][2]<<endl;
cout<<"Value of a[2][0]:"<<a[2][0]<<endl;
cout<<"Value of a[2][1]:"<<a[2][1]<<endl;
cout<<"Value of a[2][2]:"<<a[2][2]<<endl;
}
============OUTPUT============
Value of a[0][0]:10

Value of a[0][1]:20
Value of a[0][2]:30
Value of a[1][0]:40
Value of a[1][1]:50
Value of a[1][2]:60
Value of a[2][0]:70
Value of a[2][1]:80
Value of a[2][2]:90

C++ Loop Through Multidimensional Array  

#include<iostream>
using namespace std;

int main(){
int i,j;
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
for(int i=0;i<3;i++){

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

cout<<"Value of a:["<<i<<"]["<<j<<"]:"<<a[i][j]<<endl;
}
}
}
============OUTPUT============
Value of a[0][0]:10
Value of a[0][1]:20
Value of a[0][2]:30
Value of a[1][0]:40
Value of a[1][1]:50
Value of a[1][2]:60
Value of a[2][0]:70
Value of a[2][1]:80
Value of a[2][2]:90

Two Dimensional Array in C++ Example with Output

#include<iostream>
using namespace std;

int main(){
int i,j;
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}};
for(int i=0;i<3;i++){

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

cout<<" "<<a[i][j];
}
cout<<endl;

}
}
============OUTPUT============
10 20 30
40 50 60
70 80 90
10 20 30 40 50 60 70 80 90

C++ Multidimensional Array Size

A array size total number of array elements can be stored in 2D array can be calculate by multiply the size of both dimensions row and columns including the array a[5]5]. 2D array is data type int a[5][5] can be store total array elements like (5*5) => 25 elements.  
You can find size of memory in bytes than you can multiply the size of a single element by the total number of elements in the array. integer data type consume 4 bytes in memory. You can find memory size this method 
a[5][5]=5*5*4=100 bytes.

C++ Multidimensional Array Length  

#include<iostream>
using namespace std;

int main(){
int i,j;
int a[5][5] = {{20, 48, 87,56,68}, {56, 19,10,89,80},{54,56,34,68,45},{67,45,76,34,68},{44,34,56,77,55}};
for(i=0;i<5;i++){
for(j=0;j<5;j++){
cout<<" "<<a[i][j];
}
cout<<endl;
}
}

============OUTPUT============
20 48 87 56 68
56 19 10 89 80
54 56 34 68 45
67 45 76 34 68
44 34 56 77 55

C++ Dynamic 2D array Initialization

A dynamic initialization of 2d array you can enter value at a runtime of c++ program. It can be used 2D array and 3D array create easily using dynamic array. This is useful when you need to read values from user input perform calculations to determine the array elements. Arrays initialized at runtime can be modified during program execution.

Create 2D Array Dynamically in C++  

#include<iostream>
using namespace std;

int main(){
int a[3][3];
int i,j,r,c;
cout<<"Enter Row Size:";
cin>>r;
cout<<"Enter Colum Size:";
cin>>c;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
    cout<<"Enter 2D Array Element["<<i<<"]["<<j<<"]:";
    cin>>a[i][j];
}
}
cout<<"Your 2D Matrix"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
cout<<" "<<a[i][j];
}
cout<<endl;
}
}
============OUTPUT============
Enter Row Size:3

Enter Column Size:3
Enter 2D Array Elements[0][0]:12
Enter 2D Array Elements[0][1]:13
Enter 2D Array Elements[0][2]:45
Enter 2D Array Elements[1][0]:67
Enter 2D Array Elements[1][1]:87
Enter 2D Array Elements[1][2]:45
Enter 2D Array Elements[2][0]:78
Enter 2D Array Elements[2][1]:98
Enter 2D Array Elements[2][2]:12
Your 2D Matrix
12 13 45
67 87 45
78 98 12

Update 2D Array Elements in C++

2D Array modify or change the value of an array element by assigning a new value to  using index number.

How To Update 2D Array Element in C++  

#include<iostream>
using namespace std;

int main(){
int i,j,a[3][3] = {{10, 28, 80},{56, 90, 20},{20,60,70}};
cout<<"Before Update 2D Matrix"<<endl;
for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            cout<<" "<<a[i][j];
        }
cout<<endl;
}
cout<<"After Update 2D Matrix"<<endl;
a[1][0] = 99;
for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            cout<<" "<<a[i][j];
        }
cout<<endl;
}
}
============OUTPUT============
Before Update 2D Matrix
10 28 80
56 90 20
20 60 70
After Update 2D Matrix
10 28 80
99 90 20
20 60 70

2D Arithmetic Method : It is a below program is a two-dimensional array in C++. You can use this program create more program like Addition, Subtraction, Multiplication and division using this method. You can create fully dynamically 2D array program. You can only addition sign change now you can use arithmetic operation perform like (+,-,*,/). 

Addition of Two Matrix in C++ using 2D Array  

#include<iostream>
using namespace std;

int main(){
int x[3][3];
int y[3][3];
int z[3][3];
int i,j,r,c;
cout<<"Enter First Matrix Row Size:";
cin>>r;
cout<<"Enter First Matrix Colum Size:";
cin>>c;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
    cout<<"Enter First Matrix Array Element["<<i<<"]["<<j<<"]:";
    cin>>x[i][j];
}
}
cout<<"Your First Matrix"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
cout<<" "<<x[i][j];
}
cout<<endl;
}
cout<<"Enter Second Matrix Row Size:";
cin>>r;
cout<<"Enter Second Matrix Colum Size:";
cin>>c;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
    cout<<"Enter Second Matrix Array Element["<<i<<"]["<<j<<"]:";
    cin>>y[i][j];
}
}
cout<<"Your Second Matrix"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
cout<<" "<<y[i][j];
}
cout<<endl;
}
cout<<"Addition of Two Matrix"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
z[i][j]=x[i][j]+y[i][j]; //sign change create more program.
cout<<" "<<z[i][j];
}
cout<<endl;
}
}

============OUTPUT============
Enter First Matrix Row Size:2

Enter First Matrix Colum Size:2
Enter First Matrix Array Element[0][0]:1
Enter First Matrix Array Element[0][1]:2
Enter First Matrix Array Element[1][0]:3
Enter First Matrix Array Element[1][1]:4
Your First Matrix
1 2
3 4
Enter Second Matrix Row Size:2
Enter Second Matrix Colum Size:2
Enter Second Matrix Array Element[0][0]:5
Enter Second Matrix Array Element[0][1]:6
Enter Second Matrix Array Element[1][0]:7
Enter Second Matrix Array Element[1][1]:8
Your Second Matrix
5 6
7 8
Addition of Two Matrix
6 8
10 12

Conclusion of Multi Dimensional Array in C++

Conclusion: This is a all about 2D array in C++ language. I have complete every concept of related 2D array. Array program you can understand simple easy method for beginners. If your any query or problems array related than you can contact me by email. I can more easy solution provide.

Post a Comment

0 Comments
Post a Comment (0)
To Top