# Pruduct of matrixes

This is a discussion on Pruduct of matrixes within the C++ Programming forums, part of the General Programming Boards category; Hello, all: I need to calculate the product of two known matrixes A(1xn) and B(nxn). n is about 30 to ...

1. ## Pruduct of matrixes

Hello, all:

I need to calculate the product of two known matrixes A(1xn) and B(nxn). n is about 30 to 50:

C=A*B

How calculate the C(1xn) with a program?

2. http://mathworld.wolfram.com/MatrixMultiplication.html

Here is a quick although, not complete example...

Code:
```/*================================================
A program which performs matrix
multiplication.

Notes
-----
Multiplication can only take place iff
the number of columns in matrix A is equal
to the number of rows in matrix B.

Or as I like to remember it:

C A R B

Columns in
A must equal
Rows in
B

================================================*/

#include <iostream>

using namespace std;

//Function declarations
void mmult(int,int,int[][100],int[][100]);

int main()
{
//Hard code matrix A
int m1[100][100] = {{1,5},
{2,7},
{3,4}};

//Hard code matrix B
int m2[100][100] = {{8,4,3,1},
{2,5,8,6}};
//call function
mmult(3,4,m1,m2);

//Where '3' is the maximum number of rows in either
//matrix A or B
//and '4' is the maximun number of columns in either
//matrix A or B

cin.get();
cin.get();

return 0;
}
/*=================================================  =====
Function to multiply matrix
==================================================  ===*/
void mmult (int rows, int cols, int m1[][100], int m2[][100])
{
int m3[100][100];//holds the result of A * B
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
int val = 0;
for (int k=0; k<cols; k++)
{
val = val + m1[i][k] * m2[k][j];
}
m3[i][j] = val;
}
}
//print matrix
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
cout<<" "<<m3[i][j];

}
cout<<"\n";
}
}```

Popular pages Recent additions