現在、3 つの異なる 10x10 配列をパラメーターとして受け取り、最初の 2 つの配列の積で 3 番目の配列を埋める関数を使用するプログラムを作成しようとしています。
私はウェブを精査して自分で問題を解決しようとしましたが、これまでのところ、これしか思いつきませんでした:
(最初の配列に 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;
}
私の理解では、これは機能するはずですが、3番目の配列内のセルを印刷するたびに、次のように60になりません。
どんな助けでも大歓迎です。