Declaration of 2D-Arrays in C++
To declare the array, we use double pair of opening and closing square bracket.
Declaration statement:
DataType NameOfArray[Rows][Columns];
Eg; int a[3][5];
This declaration creates 3 rows and each row has 5 elements.
Initialization of 2D-Arrays in C++
Pre-initialization method
Long Method
In this method we initialize the values of each element in the array separately.
Eg: int a[2][2];
a[0][0]=4;
a[0][1]=12;
a[1][0]=8;
a[1][1]=1;
Initializing at Declaration
In this method we enter the values of the elements of the array while declaring it.
Eg: int a[2][2]={4,12,8,1};
Disadvantages of these two methods is that the size and elements of the array is initialized before only.
General Method(Initializing at run time)
In this method we use a nested loop to enter the elements of the array at run time.
Code
{
int a[row][column], i, j; //row and column should be replaced with size of the array
cout<<"\nEnter the elements of the Array:\n";
//For entering elements of the Array
for(i=0;i < row;i++)
{
for(j=0;j < column;j++)
cin>>a[i][j];
}
}
How to define size of Array at run-time of the program?
Array size defined at Run time:
Code
{
int a[180][180], i, j, m, n; //180 is the max number of rows and columns that can be used for array
cout<<"\nEnter the size of the Array(rows, column):\n";
cin>>m>>n;
cout<<"\nEnter the elements of the Array:\n";
//For entering elements of the Array
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
cin>>a[i][j];
}
}
The general misconception about 2D-Arrays is that their size can't be defined at run time, but it is possible by this method. The reason is, in this program we make an array with maximum number of rows and columns possible and while run-time of the program we just use the number of rows and columns which are required by us. This code is useful when we don't know the size of array required by the user.
C++ programs related to 2D-Arrays
Binary Search
{
int a[180][180], i, j, m, n, p, f=0;
cout<<"\nEnter the size of the Array(rows, column):\n";
cin>>m>>n;
cout<<"\nEnter the elements of the Array:\n";
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
cin>>a[i][j];
}
cout<<"\nEnter the number to search: ";
cin>>p;
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
{
if(p==a[i][j])
{
cout<<"\nNumber is found at "<<i+1<<" row and "<<j+1<<" column.";
f=1;
}
}
}
if(f==0)
cout<<"Number not found.";
}
Count of integers
{
int a[180][180], i, j, m, n, cp=0, cn=0, c0=0;
cout<<"\nEnter the size of the Array(rows, column):\n";
cin>>m>>n;
cout<<"\nEnter the elements of the Array:\n";
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
cin>>a[i][j];
}
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
{
if(a[i][j]>0)
cp++;
else if(a[i][j]<0)
cn++;
else
c0++;
}
}
cout<<"\nNo. positive integers="<<cp<<"\nNo. of negative integers="<<cn<<"\nNo. of zeroes="<<c0;
}
Maximum and Minimum
{
int a[180][180], i, j, m, n, max, min;
cout<<"\nEnter the size of the Array(rows, column):\n";
cin>>m>>n;
cout<<"\nEnter the elements of the Array:\n";
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
cin>>a[i][j];
}
max=a[i][j]; min=a[i][j];
for(i=0;i < m;i++)
{
for(j=0;j < n;j++)
{
if(max<a[i][j])
max=a[i][j];
if(min>a[i][j])
min=a[i][j];
}
}
cout<<"\nMaximum of the elements is: "<<max<<"\nMinimum of the elements is: "<<min;
}
Subscribe to:
Posts (Atom)