c++ - 添加 3*3 矩阵的代码有什么错误?
问题描述
#include <iostream>
using namespace std;
int main()
{
int a[3][3] = {1,1,1,1,1,1,1,1,1};
int b[3][3] = {1,1,1,1,1,1,1,1,1};
int c[3][3];
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<"Element of C"<<i<<j<<"is\t"<<c[i][j]<<endl;
}
}
return 0;
}
在上面的代码中,我在 A22 之前得到了正确的输出,但在上面它给我一个垃圾值。
解决方案
您需要从 0 开始索引矩阵,而不是 1。因此,您需要将for
循环更改为for(int i=0;i<3;i++)
.
推荐阅读
- flutter - 在列中换行
- constraints - ocl中的一对多关联约束
- python - 如何使用 selenium 从 python 中的 span 类中获取特定数字?
- regex - 在文件编辑器上用分号替换每行中的第一个空格
- python-3.x - 来自其他 python 脚本的未定义函数的变量
- python - 与 Python 的 map 和 list 函数混淆
- php - 尝试使用 PHP / PDO / MySQL 插入时出错
- swift - Swift 如何通过辅助功能 VoiceOver 引擎了解当前聚焦的对象
- scala - Spark Schema JSON nested
- python - Python中加密方法的解密方法