Tuesday 13 December 2011

program for addition of two matrices by pointers

#include<stdio.h>
#include<malloc.h>
main()
{
        int **a,**b,**c;
        int m,n,i,j;
        printf("\n enter the size of matrix (m*n): \n");
        scanf("%d %d",&m,&n);
        a = (int **)malloc(sizeof(int*)*m);
        for(i=0;i<m;i++)
                a[i] = (int *)malloc(sizeof(int)*n);
        b = (int **)malloc(sizeof(int*)*m);
        for(i=0;i<m;i++)
                b[i] = (int *)malloc(sizeof(int)*n);
        printf("\n enter the elements of first matrix: \n");
        for(i=0;i<m;i++)
                for(j=0;j<n;j++)
                        scanf("%d",&a[i][j]);
        printf("\n enter the lements of second matrix: \n");
        for(i=0;i<m;i++)
                for(j=0;j<n;j++)
                        scanf("%d",&b[i][j]);
        c = (int **)malloc(sizeof(int*)*m);
        for(i=0;i<m;i++)
                c[i] = (int *)malloc(sizeof(int)*n);
        for(i=0;i<m;i++)
                for(j=0;j<n;j++)
                        c[i][j]=a[i][j]+b[i][j];
        printf("\n the addition matrix is: \n");
                for(i=0;i<m;i++)
                {
                        printf("\n");
                        for(j=0;j<n;j++)
                                printf("%d ",c[i][j]);
                }

}

No comments:

Post a Comment