【发布时间】:2015-12-04 16:09:39
【问题描述】:
#include<iostream>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
return 0;
}
大家好,我在网上找到了这个源代码来添加两个矩阵,但我有几个问题要问: 为什么 first[10][10], second[10][10], sum[10][10] 声明为大小为 10 的数组?
另外,为了做减法,我只需更改符号和其他一些东西,对吗?
乘法呢?对此有何解释?谢谢大家。
【问题讨论】:
-
数组被声明为 10 的大小,因为这是作者想要的。如果您需要更多详细信息,则需要询问他们。
-
"为什么 first[10][10], second[10][10], sum[10][10] 声明为大小为 10 的数组?”实际上我们应该怎么知道? 10x10 矩阵足以满足问题要求吗?
-
你在哪里找到的,它的用途是什么?无论如何,我建议联系作者以获取更多信息。
-
矩阵乘法与仅仅改变运算符有很大不同。建议在尝试编写代码之前先学习如何使用铅笔和纸进行操作。
-
@kounliu 可能是的。更喜欢任何合适的standard container classes!