Friday, 14 March 2014

C program using Dynamic Memory Allocation to Add two Matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,p,q,r,s,*m1,*m2,*a;
clrscr();
printf("Enter the order of matrix A");
printf("\n");
printf("rows=");
scanf("%d",&p);
printf("column=");
scanf("%d",&q);
printf("\nEnter the order of matrix B");
printf("\n");
printf("rows=");
scanf("%d",&r);
printf("column=");
scanf("%d",&s);
if(q==r)
{
m1=(int*)calloc(p*q, sizeof(int));
m2=(int*)calloc(r*s, sizeof(int));
a=(int*)calloc(p*s, sizeof(int));
printf("\nEnter the elements of matrix A:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",(m1+i*q+j));
}
}
printf("\nEnter the elements of matrix B:");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",(m2+i*r+j));
}
}
printf("\nAddition:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
*(a+i*q+j)=*(m1+i*q+j)+(*(m2+i*r+j));
}
}
printf("\nSum of the Matrices A and B:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",*(a+i*q+j));
}
}
}
getch();
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

 
Copyright © 2013. BloggerSpice.com - All Rights Reserved
Author: MohammadFazle Rabbi | Powered by: Blogger
Designed by: Blogger Spice
^