Saturday 1 November 2014

26. Write a program using UDF and pointers to add two matrices and to return the resultant matrix to the calling function.

No comments :
26. Write a program using UDF and pointers to add two matrices and to return the resultant
matrix to the calling function.


PRO-26:
#include<stdio.h>
#include<conio.h>
void main()
{              int *a[4][4],*b[4][4],*c[4][4],i,j,n;
                clrscr();
                printf("\nEnter size of Matrix : ");
                scanf("%d",&n);
                printf("\nEnter %d * %d matrix A\n",n,n);
                for(i=0;i<n;i++)
                {              for(j=0;j<n;j++)
                                {              scanf("%d",a[i][j]);
                }              }
                printf("\nEnter %d * %d matrix B\n",n,n);

for(i=0;i<n;i++)
                {              for(j=0;j<n;j++)
                            {  scanf("%d",b[i][j]);
                }              }
                add(n,a,b,c);
                printf("Addition of matrix A and B :\n");
                for(i=0;i<n;i++)
                {              for(j=0;j<n;j++)
                                {              printf("%d",*(c[i][j]));
                                                printf(" ");
                                }
                                printf("\n");
                }
                getch();
}
add(int n,int *a[4][4],int *b[4][4],int *c[4][4])
{              int i,j;
                for(i=0;i<n;i++)
                {              for(j=0;j<n;j++)
                                {                *(c[i][j])=*(a[i][j])+*(b[i][j]);
                }              }
                return(*(c[i][j]));
}
OUTPUT:
Enter size of Matrix : 2
Enter 2 * 2 matrix A
1
2
1
2
Enter 2 * 2 matrix B
2
1
2
1
Addition of matrix A and B :
3 3
3 3

No comments :

Post a Comment