【发布时间】:2016-01-28 11:49:09
【问题描述】:
我想问你们如何打印一个二维数组,使它看起来像一个棋盘,并且只打印白色的数字。
#include <iostream>
using namespace std;
int main()
{
int m;
int n;
cout<<"Enter the amount of rows: "<<endl;
cin>>m;
cout<<"Enter the amount of columns: "<<endl;
cin>>n;
if(!cin){
cout<<"Error.Bad input."<<endl;
return 1;
}
double arr[m][n];
cout<<"Enter the element of the array: "<<endl;
for (int i=0;i<m;i++){
for(int x = 0;x<n;x++){
cin>>arr[i][x];
}
}
cout<<"Printed array like a chessboard: "<<endl;
for(int i=0;i<m;i++){
for(int x=0;x<n;x++){
cout<<arr[i][x]<<" ";
}cout<<endl;
}
}
如果我输入例如 4 行和 4 列,然后输入以下数字,则可以像这样打印出来:
1 2 3 4
5 6 7 8
9 0 11 12
13 14 15 16
我想要这样的输出:
1 3
6 8
9 11
14 16
先谢谢了!
【问题讨论】:
-
这与问题无关,如果我错了请纠正我,但我对c++的理解是你不能用编译后时间变量分配数组的大小,即编译器编译前需要知道数组的大小。
标签: c++ arrays for-loop multidimensional-array