Matrix multiplication

Page 1 of 1

2 Replies - 1478 Views - Last Post: 07 April 2010 - 08:12 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=166483&amp;s=7eb9a1c73f954887bf5e340f3bf96816&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Claud745

Reputation: 1
• Posts: 54
• Joined: 29-October 09

Matrix multiplication

Posted 06 April 2010 - 11:56 AM

The code for my matrix multiplication is good I think, but need some help with displaying the last matrix. I want it to display the matrix as a matrix and not just by columns. Anyway here is my code.. Any suggestions to improve my code I will appreciate also. Thank You!

```#include<iostream>
using namespace std;

int main()
{

int m, n , p, a[100][100], b[100][100], c, i, j;

cout<< "Matrix Multiplication!"<<endl;
cout<< "Instructions:"<<endl;
cout<< "Your A matrix is [A]= m x n"<<endl;
cout<< "Your A matrix is [B]= n x p"<<endl;
//Enter the size//
cout<< "Enter m: "<<endl;
cin>> m;
cout<< "Enter n: "<<endl;
cin>> n;
cout<< "Enter p: "<<endl;
cin>> p;
cout<< "Your size for A = " <<m<< "x" <<n<< "."<<endl;
cout<< "And your size for B = "<<n<< "x" <<p<< "."<<endl;
//Enter the 1st matrix//
cout<< "Now enter your first matrix: (enter row1, then row2, etc.)"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j< n;j++)
{
cin>>a[i][j];
}
}
//Enter the second matrix//
cout<< "Now enter your second matrix: (enter row1, then row2, etc.)"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j< p;j++)
{
cin>>b[i][j];
}
}
//Display the matrices//

cout<<"The First Matrix Is:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
cout<<"The Second Matrix Is: "<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
cout<<b[i][j]<<" ";
}
cout<<"\n";
}

//Multiply the matrices//
cout<<"Your resulting matrix is: (the first set of numbers is the first column, second set is second cloumn, and etc.)"<<endl;
for(int i=0; i<m; i++)
{
for(int j=0; j<p; j++)
{
c = 0;
for(int k=0; k<n; k++)
c = a[i][k]*b[k][j];
cout<<c<<endl;
}
cout<<"\n";
}

return 0;
}
```

Is This A Good Question/Topic? 0

Replies To: Matrix multiplication

#2 CTphpnwb

• D.I.C Lover

Reputation: 3389
• Posts: 12,002
• Joined: 08-August 08

Re: Matrix multiplication

Posted 06 April 2010 - 01:09 PM

Use tabs. And functions. Structure is a good thing.

Here's an example using vectors:
```#include <iostream>
#include <vector>
using namespace std;
vector< vector<double> > Matrx1;
vector< vector<double> > Matrx2;
vector< vector<double> > reslt;
vector< int > test;

void display( vector< vector< double > > &vec );

void mult( vector< vector<double> > &A, vector< vector<double> > &B, vector< vector<double> > &R );

int rows = 7, cols = 7, n;
double a1[49] = {1,0,0,0,0,0,0, 0.67,0,0.33,0,0,0,0, 0,0.67,0,0.33,0,0,0, 0,0,0.67,0,0.33,0,0, 0,0,0,0.67,0,0.33,0, 0,0,0,0,0.67,0,0.33, 0,0,0,0,0,0,1};

int main (int argc, char * const argv[]) {
Matrx1.resize(rows);
Matrx2.resize(rows);
reslt.resize(rows);
test.resize(rows);
for (int j = 0; j < rows; j++) {
Matrx1[j].resize(cols);
Matrx2[j].resize(cols);
reslt[j].resize(cols);
}

n = 0;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
Matrx1[i][j] = a1[n];
Matrx2[i][j] = a1[n];
n++;
}
}
display(Matrx1);
display(Matrx2);
cout << endl;
mult(Matrx1, Matrx2, reslt);
display(reslt);

return 0;
}

void display( vector< vector< double > > &vec )
{
cout << endl;
int vrows;
int vcols;
vrows = vec.size();
vcols = vec[1].size();
for (int r = 0; r < vrows; r++) {
for (int c = 0; c < vcols; c++) {
printf("%1.4f \t", vec[r][c]);
}
cout << endl;
}
}

void mult( vector< vector<double> > &A, vector< vector<double> > &B, vector< vector<double> > &R)
{
int vrows;
int vcols;
vrows = A.size();
vcols = A[1].size();
for (int i = 0; i < vrows; i++) {
for (int j = 0; j < vcols; j++) {
for (int rwcl = 0; rwcl < cols; rwcl++) {
R[i][j] += (A[i][rwcl] * B[rwcl][j]);
}
}
}
}

```

#3 Claud745

Reputation: 1
• Posts: 54
• Joined: 29-October 09

Re: Matrix multiplication

Posted 07 April 2010 - 08:12 AM

I've never used vector but I'll try and learn it. The functions I've just forgotten about so I'll try and incorporate that. Thank you for the help.