Comments

Tuesday, August 27, 2013

Matrix multiplication algorithm

Posted by at 2:05 AM Read our previous post


 Translate the matrix multiplication algorithm into a program which finds the product C of an nxm matrix A and pxn matrix B. Test the program using
4 -3 5
A=
6 1 -2
2 3 -7 -3
B= 5 -1 6 2
0 3 -2 1

Source Code;

#include <iostream>
using namespace std;


class matrix
{
private:
    int A[2][3];
    int B[3][4];
    int C[2][4];
public:
    void product()
    {
        int A[2][3] = {
                       {4,-3,5},
                       {6,1,-2}
                      };
        int B[3][4] = {
                        {2, 3, -7, -3}, 
                        {5, -1, 6, 2}, 
                        {0, 3, -2, 1} 
                      };
        int C[2][4]= {
                        {0,0,0,0},
                        {0,0,0,0}
                     };
        for(int r=0; r<2 ; r++)
        {
            for(int c=0;c<4;c++)
            {
                for(int k=0; k<3; k++)
                {
                    C[r][c]=C[r][c]+(A[r][k]*B[k][c]);
                }
              
            }
        }
        cout<<" MATRIX C is: "<<endl;
        for(int r=0; r<2 ; r++)
        {
            for(int c=0;c<4;c++)
            {
                cout<<C[r][c]<<"    ";
            }
            cout<<endl;
        }
    }
};
void main()
{
    matrix ob;
    ob.product();
}




Output:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template