/*
首先,根据输入的二维数组的行数和列数,动态地为该数 组分配存储空间;
其次,向二维数组中 输入数据;最后输出该数组中的所有元 素。请完善下面的程序。
*/
#include<iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;
int main(void){
int *p;
int row, col;
int i,j, k=1;
cout<<"Input number of row:\n";
cin>>row;
assert(row>0);
cout<<"Input number of column\n";
cin>>col;
assert(col>0);
p=new int[row*col];
if( !p ) {
cout<<"Not allocate memory success!\n";
exit(1);
}
for(i=0;i<row;i++){
for (j = 0; j < col; j++) {
p[i*col+j] = k++;//根据行列下标,计算值
}
}
for(i=0;i<row;i++){
for(j=0; j<col; j++){
cout<<p[i * col + j] <<"\t";//根据行列下标,显示值
}
cout<<endl;
}
delete [] p ;
system("pause");
return 0;
}
20191216动态分配数组,而且用一维数组来表示二维数组
猜你喜欢
转载自blog.csdn.net/weixin_40071289/article/details/103560446
今日推荐
周排行