【发布时间】:2020-03-23 16:03:23
【问题描述】:
我一直在尝试编写一个用于添加 2 个矩阵的 c++ 程序,这是代码 我已经写了。但是我一直有错误说“进程返回-1073741819(0xC0000005)”你能帮我找出我的错误吗?
int main()
{
float a[3][3],b[3][3],c[3][3];
int l,k;
cout<<"tell me the nr of lines in the vectors"<<endl;
cin>>l;
cout<<"tell me the nr of columns in the vectors"<<endl;
cin>>k;
for(int i=1;i<=l;i++){
for(int j=1;j<=k;j++){
cout<<"A["<<i<<"]"<<"["<<j<<"]= ";
cin>>a[i][j];
}
}
for(int i=1;i<=l;i++){
for(int j=1;j<=k;j++){
cout<<"B["<<i<<"]"<<"["<<j<<"]= ";
cin>>b[i][j];
}
}
for(int i=1; i<=l;i++){
for(int j=1;j<=k;i++){
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"the sum of matrices A & B is;"<<endl;
/* i have also added this code here instead of the following loop just to see if there was a problem with the addition procedure or displayin the results;
cout<<c[i][j]; */
for(int i=1;i<=l;i++){
for(int j=1;j<=k;j++){
cout<<c[i][j];
}
}
return 0;
}
【问题讨论】:
-
@Eyup Yilmaz C++ 中的数组索引从 0 开始。
-
@dan 我应该将其添加为 using namespace std::array<:array>, 3> 还是如何添加?
-
@VladfromMoscow 我试过了,但没有任何改变。
-
0xC0000005是访问冲突。这意味着您的代码正在尝试访问它不拥有的内存。 -
@Eyup Yilmaz 还有这些语句 cout>l; cout>k;没有意义,因为没有检查用户是否输入了正确的值。
标签: c++ nested-loops