Application, initial value, use and free space of dynamic two-dimensional array

Source: Internet
Author: User

/

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.