C+中的嵌套循环和矩阵+; 我一直在尝试编写一个C++程序来增加2个矩阵,这就是代码。 我已经写信了。但是我一直有这样的错误:“进程返回-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; } intmain() { 浮动a[3][3],b[3][3],c[3][3];; int l,k; cout
在求和逻辑中,嵌套迭代器变量C+中的嵌套循环和矩阵+; 我一直在尝试编写一个C++程序来增加2个矩阵,这就是代码。 我已经写信了。但是我一直有这样的错误:“进程返回-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; } intmain() { 浮动a[3][3],b[3][3],c[3][3];; int l,k; cout,c++,nested-loops,C++,Nested Loops,在求和逻辑中,嵌套迭代器变量j不是递增的,而是i。 这看起来像: for(int i=1; i<=l;i++){ for(int j=1;j<=k;i++){ /*Change i to j*/ c[i][j]=a[i][j]+b[i][j]; } } for(int i=1; i<=l;i++){ for(int j=1;j<=k;j++){
j
不是递增的,而是i
。
这看起来像:
for(int i=1; i<=l;i++){
for(int j=1;j<=k;i++){ /*Change i to j*/
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=1; i<=l;i++){
for(int j=1;j<=k;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
PS:不要从1开始索引,从0@EyupC++中的yIMAZ数组索引从0开始。@ DAI应该添加为使用命名空间STD::数组或如何?@ VLADROFMOSJOY我已经尝试过但是没有改变。<代码> 0xC000 00 5> <代码>是访问违规。意思是您的代码试图访问它自己没有的内存。@ YouUp YIMAZ也就是这些状态。这很重要。你能告诉我为什么从0开始索引很重要吗?如果我从1开始索引有效的话?@drescherjm只是想指出OP到底在哪里出错。为什么从0开始索引很重要,因为数组在
c++
中使用1时索引为0..size-1..size是未定义的行为nd使整个程序未定义。当您有UB时,程序的结果是没有意义的。使用0进行索引的原因是数组通常可以使用指针进行索引,即int-arr[5]={1,2,3,4,5};int*ptr=arr;
当您编写arr[0]
时,它通常意味着*(arr+0)
除非您在Matlab中,不想有未定义的行为,否则请始终使用0进行索引。如果从1开始时它有效,则实际上不会。未定义行为最糟糕的行为之一是代码被破坏(违反语言规则的代码)似乎给了您预期的结果。这给了您一种错误的感觉,即当程序被破坏时,程序工作正常,当您重新编译、更新编译器时,程序可能会失败,室内温度会发生变化……在这里,您访问的数组超出了语言规则中确定的范围。
#include <iostream>
using namespace std;
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 = 0; i < l; i++)
{
for (int j = 0; j < k; j++)
{
cout << "A[" << i << "]"
<< "[" << j << "]= ";
cin >> a[i][j];
}
}
for (int i = 0; i < l; i++)
{
for (int j = 0; j < k; j++)
{
cout << "B[" << i << "]"
<< "[" << j << "]= ";
cin >> b[i][j];
}
}
for (int i = 0; i < l; i++)
{
for (int j = 0; j < k; j++)
{
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 = 0; i < l; i++)
{
for (int j = 0; j < k; j++)
{
cout << c[i][j];
}
}
return 0;
}
tell me the nr of lines in the vectors
2
tell me the nr of columns in the vectors
3
A[1][1]= 1
A[1][2]= 2
A[1][3]= 3
A[2][1]= 4
A[2][2]= 5
A[2][3]= 6
B[1][1]= 1
B[1][2]= 2
B[1][3]= 3
B[2][1]= 4
B[2][2]= 5
B[2][3]= 6
the sum of matrices A & B is;
24681012
Process finished with exit code 0