AllExperts > Experts 
Search      

C++

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More C++ Answers
Question Library

Ask a question about C++
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Prince M. Premnath
Expertise
Years of research in Turbo C 3.0 Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Be free to ask questions.

Experience
Research For 3 years.

Education/Credentials
MCA

 
   

You are here:  Experts > Computing/Technology > C/C++ > C++ > matrix multiplication

Topic: C++



Expert: Prince M. Premnath
Date: 4/18/2008
Subject: matrix multiplication

Question

Hello

i have the following question

how can i multiply 2 matrices?

the rules are the following
assume that A and B are matrices
the result will go into matrix C
number of columns in A same as rows in B
rows in C = rows in A
columns in C = columns in B

i've been struggling a lot to write such algorithm
but never found the right solution

thanks

Answer
Hi Dear John .K

Your Problem is all about to multiply a square matrix , say number of rows equal to number of columns ,

Here im presenting a sample code to multiply two matrix a , b and the result will be stored in matrix C

#include<iostream.h>
void main()
{
  int a[3][3] , b[3][3] , c[3][3];

  int i , j , k;
  cout<<"Enter Matrix A";
  for( i = 0 ; i < 3 ; i++)
     for( j = 0 ; j < 3 ; j++)
        cin>>a[i][j];
  cout<<"Enter Matrix B";
  for( i = 0 ; i < 3 ; i++)
     for( j = 0 ; j < 3 ; j++)
        cin>>b[i][j];
  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];
     }
  cout<<"The resultant matrix is ";
  for( i = 0 ; i < 3 ; i++)
  {
     for( j = 0 ; j < 3 ; j++)
        cout<<a[i][j]<<" ";
     cout<<endl;
  }
}

Note : I have written this code for a 3x3 matrix with slight modifications you can extend this programme to perform multiplication over a m x n matrix

I havn't test this code , if you found any issues with this programme please do revert me with !

Thanks and Regards !
Prince M. Premnath

View Follow-Ups    Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.