Thursday, 6 December 2012

Program for addition of 2 sparse Matrix



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

struct sparse
{
 int r,c,v;
}s1[10],s2[10],s3[10];

void display(struct sparse s[]);
void checksparse(int r,int c,int m[][10])
{
          int i,j,cnt=0,cnt1=0;
          for(i=0;i<r;i++)
          {
                   for(j=0;j<c;j++)
                   {
                             if((m[i][j])==0)
                             {
                                      cnt++;
                             }
                             else
                             {
                                      cnt1++;
                             }
                   }
          }
          if(cnt1>cnt)
          {
                   printf("Addition not possible matrix is not sparse\n");
                   getch();
                   exit();
          }
}

void makematrix(int r,int c,int m[10][10])
{
          int i,j;
          for(i=0;i<r;i++)
          {
                   for(j=0;j<c;j++)
                   {
                   printf("Enter value for m[%d][%d]:",i,j);
                   scanf("%d",&m[i][j]);
                   }
          }

}

void makesparse(int r,int c,int m[][10],struct sparse s[10])
{       int i,j,k=1;
          s[0].r=r;
          s[0].c=c;
          for(i=0;i<r;i++)
          for(j=0;j<c;j++)
          {
                   if(m[i][j]!=0)
                   {
                             s[k].r=i;
                             s[k].c=j;
                             s[k].v=m[i][j];
                             k++;
                   }
          }
          k--;
          s[0].v=k;

}

void addsparse(struct sparse s1[],struct sparse s2[])
{
  int i=1,j=1,k=1;
  s3[0].r=s1[0].r>s2[0].r?s1[0].r:s2[0].r;
  s3[0].c=s1[0].c>s2[0].c?s1[0].c:s2[0].c;

  while((i<=s1[0].v) && (j<=s2[0].v))
  {
          if(s1[i].r==s2[j].r)
          {
                   if(s1[i].c==s2[j].c)
                   {
                             s3[k].r=s1[i].r;
                             s3[k].c=s1[i].c;
                             s3[k].v=s1[i].v+s2[j].v;
                             i++;j++;k++;
                   }
                   else if(s1[i].c<s2[j].c)
                             {
                                      s3[k].r=s1[i].r;
                                      s3[k].c=s1[i].c;
                                      s3[k].v=s1[i].v;
                                      i++;
                                      k++;
                             }
                             else
                             {
                                      s3[k].r=s2[j].r;
                                      s3[k].c=s2[j].c;
                                      s3[k].v=s2[j].v;
                                      j++;
                                      k++;
                             }
          }
          else if(s1[i].r<s2[j].r)
                   {
                             s3[k].r=s1[i].r;
                             s3[k].c=s1[i].c;
                             s3[k].v=s1[i].v;
                             i++;k++;
                   }
                   else
                   {
                             s3[k].r=s2[j].r;
                             s3[k].c=s2[j].c;
                             s3[k].v=s2[j].v;
                             j++;k++;
                   }

          }
          while(i<=s1[0].v)
          {
               s3[k].r=s1[i].r;
               s3[k].c=s1[i].c;
               s3[k].v=s1[i].v;
               i++;k++;
          }
          while(j<=s2[0].v)
          {
               s3[k].r=s1[j].r;
               s3[k].c=s1[j].c;
               s3[k].v=s1[j].v;
               j++;k++;
          }
          k--;
          s3[0].v=k;
          printf("Addition of 2 sparse matricies is ");
          printf("\n");
          display(s3);

}
void display(struct sparse s[])
{
          int i;
          printf("ROW\tCOLUMN\tVALUE\n");
          for(i=0;i<=s[0].v;i++)
          {
                   printf("%d\t%d\t%d\n",s[i].r,s[i].c,s[i].v);
          }
}

void main()
{
          int r1,r2,c1,c2,m1[10][10],m2[10][10];
          clrscr();
          printf("Enter number of rows and columns in first Matrix:");
          scanf("%d%d",&r1,&c1);
          makematrix(r1,c1,m1);
          checksparse(r1,c1,m1);
          printf("Enter number of rows and columns in second Matrix:");
          scanf("%d%d",&r2,&c2);
          makematrix(r2,c2,m2);
          checksparse(r2,c2,m2);
          makesparse(r1,c1,m1,s1);
          makesparse(r2,c2,m2,s2);
          display(s1);
          display(s2);
          addsparse(s1,s2);
          getch();
}

No comments:

Post a Comment