2

현재 매개 변수로 3 개의 다른 10x10 배열을 사용하는 함수를 사용하는 프로그램을 작성하려고하고 있으며 첫 번째 배열의 제품으로 세 번째 배열을 채 웁니다.C++에서 두 개의 10x10 배열을 곱하는 방법은 무엇입니까?

나는 시도하고 내 자신의 문제를 파악하기 위해 웹을 흐르고,하지만 지금까지 나는이 마련에만있다 :

는 (I 2의, 두 번째 배열로 첫 번째 배열을 채워 3의), 이것이 내가 3 배열 내의 세포 중 하나를 인쇄 할 때마다 여기 바와 같이하지만, 나는 60를 얻을하지 않습니다 작동해야 내 이해에

#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; 
} 

:

code output

도움을 주시면 감사하겠습니다.

답변

8

array3의 모든 값을 0으로 초기화해야합니다. 사용자를 위해 완료되지 않았습니다. 그리고 그렇게하지 않으면 무작위 값을 초기 값으로 사용하게됩니다.

+2

아 것입니다. 고마워. –

2

당신은, 당신이 array3에 임의의 값으로 끝나는되도록, 당신은, 즉, 그것으로 물건을 추가하기 전에 + = 당신은 array3 초기화되지 않은 컴퓨팅 기능

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

에서 수행하기 전에 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); 
zero에 array3를 초기화하는
4

또 다른 옵션은 지금은 완벽하게 작동,

int array3[10][10] = {{}}; 
관련 문제