Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > C-C++

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Multiplication of 2 Matrix in C

By JamesBond JamesBond is offline

On 15th October, 2006
Red face Multiplication of 2 Matrix in C

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Multiplication of 2 Matrix in C link

Author

JamesBond ( Newbie Member )

Yet to provide details about himself


All articles By JamesBond

Recent Articles

Similar Articles

Background

The output to the Matrix multiplication will be generated as follows.
Code:
a11 a12 a13   A11 A12 A13   a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32
a21 a22 a23 x A21 A22 A23 = a21xA11+a22xA21+a23xA31 a21xA12+a22xA22+a23xA32
a31 a32 a33   A31 A32 A33   a31xA11+a32xA21+a33xA31
More specific details are beyond the scope of this article but you can refer Matrix multiplication in wikipedia.

The Code

Code: C
void main()
{
    int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
    printf("Enter number of rows and columns of first matrix MAX 10\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix MAX 10\n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("Enter rows and columns of First matrix \n");
        printf("Row wise\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                scanf("%d",&m1[i][j]);
        }
        printf("You have entered the first matrix as follows:\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                printf("%d\t",m1[i][j]);
            printf("\n");
        }
        printf("Enter rows and columns of Second matrix \n");
        printf("Again row wise\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);
        }
        printf("You have entered the second matrix as follows:\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d\t",m2[i][j]);
            printf("\n");
        }
        if(r1==r2&&c1==c2)
        {
            printf("Now we add both the above matrix \n");
            printf("The result of the addition is as follows;\n");
            for(i=0;i<r1;i++)
            {
                for(j=0;j<c1;j++)
                {
                    add[i][j]=m1[i][j]+m2[i][j];
                    printf("%d\t",add[i][j]);
                }
                printf("\n");
            }
        }
        else
        {
            printf("Addition cannot be done as rows or columns are not equal\n");
        }
        printf("Now we multiply both the above matrix \n");
        printf("The result of the multiplication is as follows:\n");
        /*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                {
                    mult[i][j]+=m1[i][k]*m2[k][j];
                    /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
                }
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
        }
        getch();
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
}
Old 05-20-2007, 07:05 PM   #2
elena
Newbie Member
 
Join Date: May 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
elena is on a distinguished road

Re: Multiplication of 2 Matrix in C


I'm a student and I've got to realize matrix multiplication using threads. I know how to do it without threads, but with threads it seems to me a little confusing, cause I haven't used threads.
elena is offline   Reply With Quote
Old 01-29-2008, 12:13 PM   #3
asadullah.ansari
Pro contributor
 
Join Date: Jan 2008
Location: Bangalore
Posts: 352
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 3
asadullah.ansari will become famous soon enoughasadullah.ansari will become famous soon enough

Re: Multiplication of 2 Matrix in C


If you are user of Visual Studio (VC8) then you can easily use this

Code:
#include<omp.h>

omp_set_num_threads(2 or 4 or 6) // as depends on your loop
#pragma omp parallel for shared(mult, r1,c2) private(j,k) 
 for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                {
                    mult[i][j]+=m1[i][k]*m2[k][j];
                    /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
                }
               //printf("%d\t",mult[i][j]);
            }
            //printf("\n");
        }

By the use of OpenMP, parallel computing will be done. Today Google is using OpenMP, MPI, Grid Computing.
__________________
Asadullah Ansari
(Trueth Can'nt Be Changed that is Only One In The World "Death" )
asadullah.ansari is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
100 Multiple choice questions in C coderzone C-C++ 98 08-26-2009 12:17 AM
Commonly used C Preprocessor Directives shabbir C-C++ 1 03-06-2008 01:59 PM
ANSI C Standard Amit Ray C-C++ 1 03-06-2008 01:38 PM
How to use COM Interop in C# Nataraj1978 C# 1 02-20-2006 09:49 PM

 

All times are GMT +5.5. The time now is 06:15 AM.