Custom Search

Monday 12 December 2011

Q10)write a program to add 2 matrices using pointers


Author: Sanif S S 
Site: theeduzone.blogspot.com 
Email: sanifss@gmail.com

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

#include<stdlib.h>
void main()
{
 clrscr();
 int *a[10],*b[10],*c[10];
 int m,n,p,q,i,j;
 printf("\nenter order of first matrix : ");
 scanf("%d %d",&m,&n);
 printf("\nenter order of second matrix : ");
 scanf("%d %d",&p,&q);
 if((m!=p)&&(n!=q))
 printf("\n\naddition not possible\n");
 else
 {
  for(i=0;i
  {
   a[i]=(int *)malloc(n*sizeof(int));
   b[i]=(int *)malloc(n*sizeof(int));
   c[i]=(int *)malloc(n*sizeof(int));
  }
  printf("\nenter elements of first matrix : ");
  for(i=0;i
  {
   for(j=0;j
   {
    scanf("%d",a[i]+j);
   }
  }
  printf("\nenter elements of second matrix : ");
  for(i=0;i

  {
   for(j=0;j
   {
    scanf("%d",b[i]+j);
   }
  }
  printf("\n\nfirst matrix :\n");
  for(i=0;i
  {
   for(j=0;j
   {
    printf(" %d ",*(a[i]+j));
   }
   printf("\n");
  }
  printf("\nsecond  matrix :\n");
  for(i=0;i

  {
   for(j=0;j
   {
    printf(" %d ",*(b[i]+j));
   }
   printf("\n");
  }
  for(i=0;i
  {
   for(j=0;j
   {
   *(c[i]+j)=*(a[i]+j)+*(b[i]+j);
   }
  }
  printf("sum of two matrices \n");
  for(i=0;i
  {
   for(j=0;j
   {
    printf("%d ",*(c[i]+j));
   }
   printf("\n");
  }
 }
 getch();
}

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...