Friday, 13 September 2013

C PROGRAM FOR ADDITION OF TWO MATRICES


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main()
{
    int i=0,j=0 ;//looping purpose
    unsigned int r1=0,c1=0,r2=0,c2=0;
    unsigned int num_elm=0;
    unsigned int m1[10][10],m2[10][10] ,k[10][10]; //initaliaing the matrices


a:
printf("enter no of rows of matrix_1:");
scanf("%ud",&r1);
printf("enter no of  col of matrix_1:");
scanf("%ud",&c1);

printf("enter no of rows of matrix_2:");
scanf("%ud",&r2);
printf("enter no of  col of matrix_2:");
scanf("%ud",&c2);


if(r1!=r2 || c1!=c2)
{
    printf("matrices with uneqal dimensions cannot be added\n");
    getchar();
    exit(1);
}


//reading values for matrix 1

for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
{
    printf("\nenter the element m1[%d][%d]: ",i,j);
    scanf("%d",&m1[i][j]);
}


//reading values for matrix 2
for(i=0;i<r2;i++)
    for(j=0;j<c2;j++)
{
    printf("\nenter the element m2[%d][%d]: ",i,j);
    scanf("%d",&m2[i][j]);
}


//implimenting the addition of matrix

for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
{

    k[i][j]=m1[i][j]+m2[i][j];
}


//printing the output matrix
printf("\n\n\n\n");
for(i=0;i<r1;i++)
{
    for(j=0;j<c1;j++)
        printf("%d\t",k[i][j]);

    printf("\n");
}


getchar();
return 0;
}

No comments:

Post a Comment