Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,336 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,835 people online right now. Registration is fast and FREE... Join Now!




Matrix Multiplication

 
Reply to this topicStart new topic

Matrix Multiplication, In this program some error accures when geting the order of matrix 2 a

mkrkarthi
post 14 Mar, 2008 - 05:59 PM
Post #1


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 0

In this program some display order problem when getting the order of matrix 2 and its elements. Addition and subtraction works perfectly but multiplication do not shows the correct results
CODE

/* Addition , Subtraction and Multiplication of Matrices */
#include<stdio.h>
main()
{
    int a[20][20], b[20][20], i,j,m,n,p,q;
    clrscr();
    /*Getting the element of the Matrix1 */
    printf("Enter the order of first matrix\n");
    scanf("%d %d", &m, &n);
    printf("%d %d", m,n);
    printf("\n Enter the element of first matrix\n");
    for(i=0; i<m; i++)
        for(j=0; j<n; j++)
        {
            scanf("%d", &a[i][j]);
        }

    /*Getting the element of the Matrix2 */
    printf("\n Enter the order of second matrix\n");
    scanf("%d %d ", &p,&q);
    printf("%d %d\n", p,q);
    printf("\n Enter the element of second matrix\n");
    for(i=0; i<p; i++)
        for(j=0; j<q; j++)
        {
            scanf("%d", &b[i][j]);
        }
        printf("\n");
    /* Displaying Matrix1 and Matrix2 */
    clrscr();
    printf("\n\t\t Addition, Subtraction, Multiplication of Matrixes\n\n");
    printf("First matrix:\n");
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
           printf("\t%d", a[i][j]);
        printf("\n");

    }
    printf("\n");
    printf("Second Matrix: \n");
    for(i=0; i<p; i++)
    {
        for(j=0; j<q; j++)
            printf("\t%d", b[i][j]);
        printf("\n");
    }

    if(m==p & n==q) /*Checks whether the orders of the Matrixes are equal */
    {
        add(a,b,m,n);      /*If equal sends to the functions add & mul */
        mul(a,b,m,n,q);
    }
    else
    {
       if(n==p)      /* If columns(Mat.1) = Rows (Mat.2) it Multiplication only possible */
       {
            printf("\n Addition and Subtraction not possible with this order");
            mul(a,b,n,q);
       }
       else
       {
            printf("\n The order of the matrix are not proper");
       }
    }
getch();
}

mul(int m1[20][20], int m2[20][20], int r1, int c1, int c2)

{
    int i,j,k,p[20][20];
    for(i=0; i<r1; i++)
        for(j=0; j<c2; j++)
        {
            p[i][j] =0;
            for(k=0; k<c1; k++)
                p[i][j] += m1[i][k]*m2[k][i];
        }

    printf("\n\t The product: \n");
    for(i=0; i<r1; i++)
    {
        for(j=0; j<c2; j++)
        {
            printf("\t\t %d", p[i][j]);
        }
        printf("\n\n");
    }
}

add(int m1[20][20],int m2[20][20],int r,int c)
{
       int i,j,sum[20][20],diff[20][20];
       for(i=0;i<r;i++)
       for(j=0;j<c;j++)
       {
            sum[i][j]=m1[i][j]+m2[i][j];
            diff[i][j]=m1[i][j]-m2[i][j];
       }
       printf("\n");
       printf("\t The sum:\n");
       for(i=0; i<r; i++)
       {
            for(j=0;j<c;j++)
            printf("\t%d\t",sum[i][j]);
            printf("\n\n");
       }

    printf("\t The difference:\n");
    for(i=0; i<r; i++)
    {
        for(j=0; j<c; j++)
        printf("\t%d\t", diff[i][j]);
        printf("\n\n");
    }

}

User is offlineProfile CardPM

Go to the top of the page


Trotter
post 14 Mar, 2008 - 09:32 PM
Post #2


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 43


My Contributions


Maybe you missed the name of the forum. "Introduce Yourself" usually means a place to, umm, introduce yourself... ya think?

You will get the results you want if you post in the proper forum.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/19/08 03:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month