/
According to the dynamic two-dimensional array of applications, the initial value, use, free space five parts of the code to show the reference.
At the same time, a dynamic two-dimensional array is used as an example of formal parameters and arguments, which is convenient for us to search.
#include <iostream>
using namespace Std;
Dynamic two-dimensional array as formal parameter
void Display (int * * &p,int row,int Col)
{
Inti,j;
For (i= 0;i <= row-1;i++)
{
for (j= 0; J <= col-1;j++)
{
cout <<p[i][j]+i+j << "T";
}
cout << Endl;
}
}
Main function
void Main ()
{
-------format output template information content------start------
cout << "The results of the program output are as follows:" << Endl << Endl;
cout << "/***************************/" <<endl;
cout << "/* dynamicarray exemple * *" <<endl;
cout << "/* Tang 2011 Years/day *" << Endl;
cout << "/***************************/" <<endl << Endl;
-------format output template information content------END------
Int**p; This is a pointer to a pointer
Introw,col; This dynamic two-dimensional array is "row row", "Col" column
Inti,j; Cyclic variables
Application of dynamic two-dimensional array p
cout << "Please enter the number of rows and columns:" << endl << Endl;
Cin >> Row >> col;
cout << Endl;
p = newint *[row];
for (int i = 0;i <= row-1;i++)
{
P[i]=new Int[col];
}
To assign an initial value to an element of a dynamic two-dimensional array p
For (i= 0;i <= row-1;i++)
{
for (j= 0; J <= col-1;j++)
{
P[I][J] = 0;
}
}
Examples of the use of dynamic two-dimensional array p
For (i= 0;i <= row-1;i++)
{
for (j= 0; J <= col-1;j++)
{
cout << p[i][j]<< "T";
}
cout << Endl;
}
cout << Endl;
cout << "Output by calling Function:" << Endl << Endl;
Dynamic two-dimensional array p as a parameter
Display (P,ROW,COL);
When you release the resource, delete the elements inside and remove the P
For (i= 0;i <= row-1;i++)
{
Delete[] p[i];
}
Deletep;
-------format output template information content------start------
cout << Endl <<endl;
System ("pause");
-------format output template information content------END------
}
The results of the program output are as follows:
/***************************/
/* DYNAMIC ARRAY exemple * *
* Tang October 14, 2011 * *
/***************************/
Please enter the number of rows and columns:
2
3