Resources » Articles/Knowledge Sharing » Computer & Technology

C program of matrix addition,substraction,and multiplication


Posted Date: 15-Mar-2008  Last Updated:   Category: Computer & Technology    
Author: Member Level: Gold    Points: 5



/* Program of addition,substraction,and multiplication of matrix */

#include
#include
void read_mat(int m,int n,int a[5][5])
{
int i,j;
for(i=0;i {
for(j=0;j {
printf("\n Enter element :");
scanf("%d",&a[i][j]);
}
}
void print_mat(int m,int n,int a[5][5])
{
int i,j;
for(i=0;i {
for(j=0;jprintf(" %d",a[i][j]);
printf("\n");
}
}
void add_mat(int m,int n,int a[5][5],int b[5][5],int s[5][5])
{
int i,j;
for(i=0;i {
for(j=0;j {
s[i][j]=a[i][j]+b[i][j];
}
}
}
void sub_mat(int m,int n,int a[5][5],int b[5][5],int sd[5][5])
{
int i,j;
for(i=0;i {
for(j=0;j {
sd[i][j]=a[i][j]-b[i][j];
}
}
}
void mul_mat(int m,int n,int q,int a[5][5],int b[5][5],int mt[5][5])
{
int i,j,k;
for(i=0;i {
for(j=0;j {
mt[i][j]=0;
for(k=0;k {
mt[i][j]=mt[i][j]+a[i][k]*b[k][j];
}
}
}
}
void main()
{
int m,n,p,q,i,j,a[5][5],b[5][5],s[5][5],sd[5][5],mt[5][5];
printf("\n Enter order of first matrix :");
scanf("%d%d",&m,&n);
printf("\n Order of second matrix :");
scanf("%d%d",&p,&q);
if((m!=p)&&(n!=q))
printf("\n Matrices are imcompatable for addotion and substraction");
else
{
printf("\n Read the element of first matrix ");
read_mat(m,n,a);
printf("\n Read the element of second matrix ");
read_mat(p,q,b);
printf("\n FIRST MATRIX IS ......\n");
print_mat(m,n,a);
printf("\n SECOND MATRIX IS ....\n");
print_mat(p,q,b);
add_mat(m,n,a,b,s);
printf("\n AFTER ADDITION MATRIX IS ....\n");
print_mat(m,n,s);
sub_mat(m,n,a,b,sd);
printf("\n AFTER SUBSTRACTION MATRIX IS ....\n");
print_mat(m,n,sd);
}
if(p==q)
{
mul_mat(m,n,q,a,b,mt);
printf("\n AFTER MULTIPLICATION MATRIX IS ....\n");
print_mat(m,q,mt);
}
else
printf("\n Matrices are incompatible for multiplication ");
getch();
}


Did you like this resource? Share it with your friends and show your love!




Responses to "C program of matrix addition,substraction,and multiplication"
Guest Author: S Nair     08 Mar 2013
C programme to find the addition, subtraction and multiplication of matrices is very informative, helpful article and nice site. Thanks


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.