Tony Anziano
Tony Anziano

Reputation: 423

How Do I Multiply Two 10x10 Arrays in C++?

I'm currently trying to write a program that uses a function that takes 3 different 10x10 arrays as parameters, and fills the 3rd array with the product of the first 2 arrays.

I have scoured the web to try and figure the problem out on my own, but so far, I have only come up with this:

(I populated the first array with 2's, and the second array with 3's)

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

To my understanding, this should work, however whenever I print any of the cells within the 3rd array, I do not get 60 as seen here:

code output

Any help would be much appreciated.

Upvotes: 2

Views: 4623

Answers (4)

stardust
stardust

Reputation: 5988

Another option to initialize array3 to zero would be

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

Upvotes: 4

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158469

You are not initializing array3, so you are ending up with random values in array3, try adding this function:

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

and call it in main:

populate_array3(array3); 

Upvotes: 3

taocp
taocp

Reputation: 23634

You need to initialized array3 before you add stuff into it,i.e., before doing += in that computing function

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

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93561

You need to initialize all values in array3 to 0. Its not done for you. And if you don't do that, you're using a random value as your initial value.

Upvotes: 8

Related Questions