Thursday, 16 May 2013

c program to find addition & multiplication of two matrices

Matrix elements are represented in the form of rows and columns. All the matrices elements are arranged in the form of rows and columns. An element can be represented by using its row index and column index. To represent and store the matrix elements we use a two dimensional array. It can store the elements in two directions. thus we can use it for storing matrices elements by assuming one dimension as rows and the other dimension as columns. by this we can store and retrieve matrix elements in a two dimensional array and can also find the arithmetic operations like addition and multiplication of the two matrices. we have combined matrix addition and matrix multiplication into a single program in order to find matrix addition and multiplication easily. The below is the c program to find addition and multiplication of the given two matrices. It takes the matrices elements as input and stores them in two dimensional arrays and computes and gives the required output. The entire c program for finding addition and multiplication of matrices is below.

c program to find matrix addition and multiplication:-

c program to find addition-multiplication of two matrices
/* Write a C program that uses functions to perform the following:
i) Addition of Two Matrices
ii) Multiplication of Two Matrices */
#include<stdio.h>
int main(void)
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
printf("************************************");
printf("\n\t\tMENU");
printf("\n**********************************");
printf("\n[1]ADDITION OF TWO MATRICES");
printf("\n[2]MULTIPLICATION OF TWO MATRICES");
printf("\n[0]EXIT");
printf("\n**********************************");
printf("\n\tEnter your choice:\n");
scanf("%d",&ch);
if(ch<=2 && ch>0)
{
printf("Valid Choice\n");
}
switch(ch)
{
case 1:
printf("Input rows and columns of A & B Matrix:");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements of matrix B:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
}
printf("\n =====Matrix Addition=====\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",a[i][j]+b[i][j]);
printf("\n");
}break;
case 2:
printf("Input rows and columns of A matrix:");
scanf("%d%d",&m,&n);
printf("Input rows and columns of B matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("matrices can be multiplied\n");
printf("resultant matrix is %d*%d\n",m,q);
printf("Input A matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Input B matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
}
/*Function for Multiplication of two matrices*/
printf("\n =====Matrix Multiplication=====\n");
for(i=0;i<m;++i)
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("Resultant of two matrices:\n");
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
/*end if*/
else
{
printf("Matrices cannot be multiplied.");
}
/*end else*/
break;
case 0:
printf("\n Choice Terminated");
break;
default:
printf("\n Invalid Choice");
}
return 0;
}

output :-

************************************
                          MENU
************************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
************************************
Enter your choice:
1
Valid Choice
Input rows and columns of A & B Matrix:
2     2
Enter elements of matrix A:
1     2     3     4
Enter elements of matrix B:
5     6     7     8
=====Matrix Addition=====
6          8
10        12

The above c program will read two matrices as input and finds its addition and multiplication and displays the resultant matrix. if you have any doubts, share them in the comments....

We Will Send Some More Information For Free!

Like This Story? Please Share!

Other Ways To Share:

Like Our Site? Follow Us!


0 comments:

Post a Comment

Your feedback is always appreciated. We will try to reply to your queries as soon as time allows.
Please do not spam. Spam comments will be deleted upon our review.

Regards:
Srinath Reddy.

 

Articles cannot be reproduced without permission from the author.

BEGINNERS-TUTORIALS © 2013. All Rights Reserved | Contact | About