【发布时间】:2020-11-24 08:33:05
【问题描述】:
#include <iostream>
using namespace std;
int main()
{
system("cls");
int m,n;
int ar[m][n];
cout<<"Enter the rows: "<<endl;
cin>>m;
cout<<"Enter the columns: "<<endl;
cin>>n;
cout<<m<<"x"<<n<<" Matrix"<<endl;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>ar[i][j];
}
cout<<endl;
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<ar[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
输出:
输入行:
2
输入列:
2
2x2 矩阵
1 2
3 4
矩阵:
3 4
3 4
它只显示第二行两次。 我再次输入了 2-3 次程序,但没有变化 如何克服这个错误?
请帮帮我?
【问题讨论】:
-
int ar[m][n];不是标准 C++,而是编译器扩展。最好使用std::vector。 -
这里
int ar[m][n];:m和n的值仍然未知 -
嗨,就像上面的 cmets 所说,我们必须在编译源代码之前确定数组的大小。要么使用最大大小声明
ar(例如int ar[100][100];),要么使用std::vector,这将在运行时动态定位内存。 -
打开警告并认真对待。你的编译器可以帮助你很多,如果你让它