Two Dimensional Array in C++

Two dimensional array in c++. An array of arrays can be defined as a two-dimensional array. The matrices that make up the 2D array are represented as a collection of rows and columns. 2D arrays, on the other hand, are used to build a data structure that resembles that of a relational database.

#include<iostream>
using namespace std;
int main()
{
int arr[10][10], rows, cols, i, j;
cout<<"\n Enter the rows elements for array";
cin>>rows;
cout<<"\n Enter the column elements for array";
cin>>cols;
cout<<"\n Enter "<<rows<<"*"<<cols<<"Array elements are\n";
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
cout<<" ";
cin>>arr[i][j];
}
}
cout<<"\n 2D array is\n";
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
cout<<" "<<arr[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}

 

Output:

Enter the rows elements for array2

Enter the column elements for array2

Enter 2*2Array elements are
34
45
67
89

2D array is
34 45
67 89

2D Array in C++ Program

Geometric Flat plane figures with two dimensions – length and width – are referred to as 2-dimensional shapes or objects. Two-dimensional or 2-D shapes have no thickness and can be measured only on two sides. A square, circle, rectangle, and triangle are examples of two-dimensional objects. 

A dimension is something that can be measured, such as your length, width, or height. A two-dimensional (2D) object has only two dimensions, such as length and width, and no thickness or height.

Namespace in C++ Program

The namespace “std” is recognised as a namespace with members that are used in the application. Cout, cin, endl, and other members of the “std” namespace are examples. This namespace can be found in the header file iostream.h. So, when we run a program to print something, “using namespace std” tells us that if we come across something that isn’t declared in the current scope, we should look in std. The reason for this is that the computer need the code for the cout and cin functions, as well as the namespace in which they are defined.