【问题标题】:How Do I Multiply Two 10x10 Arrays in C++?如何在 C++ 中将两个 10x10 数组相乘?
【发布时间】:2013-04-15 01:32:26
【问题描述】:

我目前正在尝试编写一个程序,该程序使用一个函数,该函数将 3 个不同的 10x10 数组作为参数,并用前 2 个数组的乘积填充第三个数组。

我已经在网上搜索并尝试自己解决问题,但到目前为止,我只是想出了这个:

(我用 2 填充第一个数组,用 3 填充第二个数组)

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

/************************************************
** Function: populate_array1
** Description: populates the passed array with 2's
** Parameters: 10x10 array
** Pre-Conditions:
** Post-Conditions:
*************************************************/
void populate_array1(int array[10][10])
{
  int i, n;
  for (i = 0; i<10; i++)
  {
    for (n = 0; n<10; n++)
    {
      array[i][n] = 2;
    }
  }
}

/************************************************
** Function: populate_array2
** Description: populates the passed array with 3's
** Parameters: 10x10 array
** Pre-Conditions:
** Post-Conditions:
*************************************************/
void populate_array2(int array[10][10])
{
  int i, n;
  for (i = 0; i<10; i++)
  {
    for (n = 0; n<10; n++)
    {
      array[i][n] = 3;
    }
  }
}

/************************************************
** Function: multiply_arrays
** Description: multiplies the first two arrays,
and populates the 3rd array with the products
** Parameters: 3 10x10 arrays
** Pre-Conditions:
** Post-Conditions:
*************************************************/
void multiply_arrays(int array1[10][10], int array2[10][10], int array3[10][10])
{
  int i, n, j;
  for (i = 0; i<10; i++)
  {
    for (n = 0; n<10; n++)
    {
      for (j = 0; j<10; j++)
      {
        array3[i][n] += array1[i][j]*array2[j][n];
      }
    }
  }
}

int main()
{
  int array1[10][10];
  int array2[10][10];
  int array3[10][10];

  populate_array1(array1); // Fill first array with 2's
  populate_array2(array2); // Fill second array with 3's

  multiply_arrays(array1, array2, array3);

  cout << array1[5][2];
  cout << endl << array2[9][3];
  cout << endl << array3[8][4];

  return 0;
}

据我了解,这应该可以,但是每当我打印第三个数组中的任何单元格时,我都没有得到 60,如下所示:

任何帮助将不胜感激。

【问题讨论】:

    标签: c++ arrays multidimensional-array matrix-multiplication


    【解决方案1】:

    您需要将 array3 中的所有值初始化为 0。它不是为您完成的。如果你不这样做,你就是在使用一个随机值作为你的初始值。

    【讨论】:

      【解决方案2】:

      另一个将 array3 初始化为 zero 的选项是

      int array3[10][10] = {{}};
      

      【讨论】:

        【解决方案3】:

        你没有初始化array3,所以你在array3中得到了随机值,尝试添加这个函数:

        void populate_array3(int array[10][10])
        {
          int i, n;
          for (i = 0; i<10; i++)
          {
            for (n = 0; n<10; n++)
            {
              array[i][n] = 0;
            }
          }
        }
        

        并在main 中调用它:

        populate_array3(array3); 
        

        【讨论】:

          【解决方案4】:

          您需要在向其中添加内容之前初始化 array3,即在该计算函数中执行 += 之前

          int i, n;
          for (i = 0; i<10; i++)
          {
             for (n = 0; n<10; n++)
             {
                 array3[i][n] = 0;
             }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多