【问题标题】:adding, subtracting, multiplying matrices in functions [closed]函数中的加法、减法、乘法矩阵[关闭]
【发布时间】: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

标签: c++ matrix


【解决方案1】:

为什么 first[10][10], second[10][10], sum[10][10] 声明为大小为 10 的数组?

该程序的作者假设最大可能的矩阵大小为 10 x 10。 如果需要,请添加缩小的大小。

另外,为了做减法,我只需更改符号和其他一些东西,对吗?

sum[c][d] = first[c][d] + second[c][d]; //Do what you want with this 

乘法呢?对此有何解释?

试试笔和纸,然后翻译成代码。

有用的链接:

享受吧。

【讨论】:

  • 旁注 - 矩阵乘法不可交换 (A x B != B x A),因此对于矩阵乘法,有一个左矩阵和一个右矩阵。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多