# Thread: How do I find product of the matrices?

1. ## How do I find product of the matrices?

Hi

I'm trying to learn to find the product of two matrices using arrays. Please help me. I'm really stuck. And please don't introduce those topics which I won't be able to understand (I'm a beginner). In other words, please keep it simple. Thanks.

Code:
```// product_of_two_matrices_1x3_and_3x1.cpp
// read two matrices and find product

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
float m1[1][3];
float m2[3][1];
float m3[1][3];
int r, c;

cout << "Enter matrix #1 below\n\n";

for ( r=0; r<1; r++)
{
for (c=0; c<3 ; c++)
{
cout << "enter entry for row #" << (r+1) << " and column"
<< " #" << (c+1) << ": ";
cin >> m1[r][c];
}
}

cout << "\n\nEnter matrix #2 below\n\n";

for ( r=0; r<3; r++)
{
for (c=0; c<1; c++)
{
cout << "enter entry for row #" << (r+1) << " and column"
<< " #" << (c+1) << ": ";
cin >> m2[r][c];
}
}

cout << "\n\nproduct of matrix #1 and matrix #2 is given below\n\n";

//How do I find product of the matrices?

return 0;

}```

2. Do you know how to multiply matrices on paper?

3. ^^ +1 I was just going to say that.

If you do not know how then check this out:
4. Multiplication of Matrices

Or maybe use Paul's Online Notes, another excellent resource. The basics look like a nested for loop with a running sum and the loops controlling the rows/columns. I'd write it out more but I'm at work :P If someone hasn't helped you with it in a couple hours I will write it out ha

4. Originally Posted by tabstop
Do you know how to multiply matrices on paper?
Yes.

5. Then you just need to convert that to code. Write down the steps you use to do the multiplication. Look for repeated steps that are almost the same, but differ only in which row/column of the original you're using, and put those in a loop.

6. Code:
```#include<iostream.h>

void input(int t[][3]);

void product(int a[][3], int b[][3], int c[][3]);

void display(int [][3]);

void main()

{

int a[3][3], b[3][3], c[3][3];

cout << "\tPROGRAM OF PRODUCT OF TWO 3X3 MATRICES\n\nEnter 9 Values for 3x3 Matrix 1 \n";

input(a);

cout << "Enter 9 Values for 3x3 Matrix 2 \n";

input(b);

product(a,b,c);

cout << "\nProduct of two matrices are \n\n";

display(c);

}

void input(int t[][3])

{

int i, j;

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

cin >> t[i][j];

}

}

void product(int a[][3], int b[][3], int c[][3])

{

int i, j, k;

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

{

c[i][j]=0;

for(k=0; k<3; k++)

{

c[i][j] += + a[i][k] * b[k][j];

}

}

}

}

void display(int c[][3])

{

int i, j;

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

cout << c[i][j] << " ";

cout << "\n";

}

}```
This is program for multiplying 3x3 matrices.......hope it will help u a lot

7. Thanks a lot, Jasir Ali.

8. Originally Posted by Jasir Ali
This is program for multiplying 3x3 matrices.......hope it will help u a lot
Wow Jasir, strike two! I told you in my other post that you need to read the forum guidelines. I linked you to them, and I'll do it again here: forum guidelines (that red text is a link -- click it and read the entire page). You even "liked" my post. Number 1 of the forum guidelines mentions the homework policy (red text again -- click the link and read everything), which states in no uncertain terms this board is not in the business of handing out homework solutions. We're here to help people learn. That is not done by giving out solutions to people, but by letting them to try to solve their problems and giving them a nudge in the right direction when they stray off course. Your code did not help Jackson6612, it only served to cripple him in the long run by ensuring he only learns how to ask for free code on forums, and does not learn to problem solve or learn how to turn solutions into code.

Popular pages Recent additions