# Matrix Multiplication

Page 1 of 1

## 1 Replies - 5803 Views - Last Post: 14 March 2008 - 10:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=46294&amp;s=7f7db0b20acd04f032a19c976b6e5238&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mkrkarthi

Reputation: 1
• Posts: 0
• Joined: 06-March 08

# Matrix Multiplication

Posted 14 March 2008 - 06:59 PM

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
```/* 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 */
{
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");
}
}

{
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");
}

}

```

Is This A Good Question/Topic? 1

## Replies To: Matrix Multiplication

### #2 Trotter

Reputation: 0
• Posts: 43
• Joined: 11-March 08

## Re: Matrix Multiplication

Posted 14 March 2008 - 10:32 PM

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.