计算机二级

3773考试网计算机等级考试计算机二级正文

C++编程之二维动态数组

来源:fjzsksw.com 2010-4-28 15:30:12

 

  #include "stdafx.h"

  #include <iostream>

  using namespace std;

  int main()

  {

  int r;  //行数

  int c;  //列数

  cout<<"Please input the number of rows of the dynamic array: ";

  cin>>r;  //输入行数

  cout<<"Please input the number of columns of the dynamic array: ";

  cin>>c;  //输入列数

  //创建二维动态数组

  int **p=new int*[r];

  for(int i=0;i<r;i++)

  {

  p[i]=new int[r];

  }

  cout<<"The array named p["<<r<<"]["<<c<<"] is created."<<endl;

  //循环赋值

  int temp;

  for(int i=0;i<r;i++)

  {

  for(int j=0;j<c;j++)

  {

  cout<<"Please input a value of p["<<i<<"]["<<j<<"] in the array: ";

  cin>>temp;

  *(*(p+i)+j)=temp;  //寻址赋值

  }

  }

  //循环显示

  cout<<"The dynamic array is "<<endl;

  for(int i=0;i<r;i++)

  {

  for(int j=0;j<c;j++)

  {

  cout<<*(*(p+i)+j)<<" ";  //寻址读取

  }

  cout<<endl;

  }

  return 0;

  }

 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1