Sunday, May 5, 2013

Addition of Two Matrices Using Functions


/*C Program addion of two matrixes using function*/

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

void read_mat(int,int[10][10]);
void add_mat(int,int[10][10],int[10][10]);

main()
{
    int a[10][10],b[10][10],n;
    clrscr();
    printf("Enter Matrix size:");
    scanf("%d",&n);
    printf("Your chooses %d*%d Matrix:\n",n,n);

    printf("Enter elements into First Matrix:");
    read_mat(n,a);

    printf("Enter elements into Second Matrix:");
    read_mat(n,b);

    printf("Addition of the Matrix is :\n");
    add_mat(n,a,b);

    getch();
}

/*Function Starts Main follow Me*/

void read_mat(int x,int a[10][10])
{
    int i,j;
    for(i=0;i<x;i++)
    for(j=0;j<x;j++)
    scanf("%d",&a[i][j]);
}

void add_mat(int x,int a[10][10],int b[10][10])
{
    int i,j,c[10][10];
    for(i=0;i<x;i++)
    for(j=0;j<x;j++)

    c[i][j]=a[i][j]+b[i][j];

    for(i=0;i<x;i++)
    {
        for(j=0;j<x;j++)
        printf("%d  ",c[i][j]);
        printf("\n");
    }
}

output
Enter Matrix size:2

Your chooses 2*2 Matrix:

Enter elements into First Matrix:
2
2
2
2
Enter elements into Second Matrix:
2
2
2
2
Addition of the Matrix is :
4  4
4  4

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More