Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiply matrix in C++
05-16-2010, 08:53 PM (This post was last modified: 01-07-2011 04:03 AM by drdebcol.)
Post: #1
Multiply matrix in C++
This is program for mathematical matrix multiplication.
You have everything about that here :
http://en.wikipedia.org/wiki/Matrix_multiplication
And here is the program :
Code:
#include <iostream>
using namespace std;

int main()
{
  unsigned m,n1,n2,p;
  do
  {
  cout<<"Enter height of first matrix !"<<endl;
  cin>>m;
  cout<<"Enter width of first matrix !"<<endl;
  cin>>n1;
  cout<<"Enter height of second matrix !"<<endl;
  cin>>n2;
  cout<<"Enter width of second matrix !"<<endl;
  cin>>p;
  }
  while (n1 != n2);
  //Dynamic allocation of matrix
  //Allocate every one-dmensional array in matrix
  double **a;
  a=new double*[m];
  for (int i=0;i<m;i++)
    a[i]=new double[n1];
    
  double **b;
  b=new double*[n2];
  for (int i=0;i<n2;i++)
    b[i]=new double[p];
    
  double **c;
  c=new double*[m];
  for (int i=0;i<m;i++)
    c[i]=new double[p];
  
  for (int i=0;i<m;i++)
    for (int j=0;j<p;j++)
      c[i][j]=0;
  cout<<"Enter first matrix (separate numbers with spaces) ! "<<endl;
  for (int i=0;i<m;i++)
    for (int j=0;j<n1;j++)
      cin>>a[i][j];
  cout<<endl;
  cout<<"Enter second matrix (separate numbers with spaces) ! "<<endl;
  for (int i=0;i<n2;i++)
    for (int j=0;j<p;j++)
      cin>>b[i][j];  
      // Here calculation starts
      for (int i=0;i<m;i++)
        for (int j=0;j<p;j++)
          for (int k=0;k<n1;k++)
            c[i][j]+=a[i][k]*b[k][j];
    cout<<"Result is : "<<endl;
    for (int i=0;i<m;i++)
    {
      for (int j=0;j<p;j++)
        cout<<c[i][j]<<" ";
      cout<<endl;  
    }  
    system("pause");
}

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
05-17-2010, 06:32 AM
Post: #2
RE: Multiply matrix in C++
Very good work Drdebcol! Nice work with multidimensional arrays

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
01-06-2011, 03:13 PM (This post was last modified: 01-06-2011 11:02 PM by Xupicor.)
Post: #3
RE: Multiply matrix in C++
Note that it is not Standard C++. To get dynamic array you either use non-standard compiler extension (this) or use a standard way of using a pointer and allocating memory by yourself:
Code:
unsigned int n;
cin >> n;
int* array = new int[n];
//do something with it
delete[] array;

You can also forget plain arrays and use a std::vector or any other smart container, as a good C++ programmer would do:
Code:
std::vector<int> array;
array.push_back(15);
array.push_back(16);
array.push_back(17);
// array has 3 elements now, it is flexible and allocates/frees memory by itself
Find all posts by this user
Quote this message in a reply
01-07-2011, 03:54 AM
Post: #4
RE: Multiply matrix in C++
Oh, yeah that is probably an error. I am going to fix that !
I know that way of declaring arrays can work in all functions except main.
And yeah of course i know how to allocate a dynamic array using new and delete !
Good reply !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-09-2011, 03:08 AM (This post was last modified: 01-09-2011 03:13 AM by CrAzY-KiD.)
Post: #5
RE: Multiply matrix in C++
that was very confusing and complicated for me....
i m at initial level.... and i can't understand that things... Sad
Find all posts by this user
Quote this message in a reply
01-09-2011, 03:25 AM
Post: #6
RE: Multiply matrix in C++
(01-09-2011 03:08 AM)CrAzY-KiD Wrote:  that was very confusing and complicated for me....
i m at initial level.... and i can't understand that things... Sad
Well mathematically that is not so hard, multiplying matrix is a simple action.
But when you look at the code, there are mostly simple things, except matrix allocation and pointers, which most beginners find hard !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-09-2011, 12:04 PM
Post: #7
RE: Multiply matrix in C++
but my teacher gave me that question....
i almost solve it but when mulitdimension Multiply matrix in C++ for 3*3 using more loop
can u guide me to make a good program with less loop

[Image: cazykid1.png]
Find all posts by this user
Quote this message in a reply
01-09-2011, 09:50 PM
Post: #8
RE: Multiply matrix in C++
The easiest way to multiply matrix is with 3 for loops (complexity O(n^3)). Every other way is harder to implement, though it has slightly better complexity.
So the easiest way to multiply two matrix-es a and b and get result c is :
Code:
for (int i=0;i<m;i++)
    for (int j=0;j<p;j++)
        for (int k=0;k<n;k++)
            c[i][j]+=a[i][k]*b[k][j];
And sizes are :
a (m*n)
b (n*p)
c (m*p)
where m,n,p are integers that user enters !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-10-2011, 02:26 AM (This post was last modified: 01-10-2011 02:44 AM by CrAzY-KiD.)
Post: #9
RE: Multiply matrix in C++
for 2*2...!!!
what should i do...!
please write full code...
so that i can understand batter Smile

[Image: cazykid1.png]
Find all posts by this user
Quote this message in a reply
01-10-2011, 04:38 AM
Post: #10
RE: Multiply matrix in C++
Basically you need to understand concept of multiplication !
It doesn't matter if size is 2x2 or 3x3 etc.
But if you which, here is the code for hand multiplication (without loops) for two matrices 2x2 :
Code:
#include <iostream>

using namespace std;

int main()
{
  int a[2][2],b[2][2],c[2][2];
  cout<<"Enter first matrix : "<<endl;
  cin>>a[0][0]>>a[0][1]>>a[1][0]>>a[1][1];
  cout<<"Enter second matrix : "<<endl;
  cin>>b[0][0]>>b[0][1]>>b[1][0]>>b[1][1];
  c[0][0]=a[0][0]*b[0][0]+a[0][1]*b[1][0];
  c[1][0]=a[1][0]*b[0][0]+a[1][1]*b[1][0];
  c[0][1]=a[0][0]*b[0][1]+a[0][1]*b[1][1];
  c[1][1]=a[1][0]*b[0][1]+a[1][1]*b[1][1];
  cout<<"Result is : "<<endl;
  cout<<c[0][0]<<" "<<c[0][1]<<endl;
  cout<<c[1][0]<<" "<<c[1][1]<<endl;
  system("pause");    
}

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: