Thursday, 7 May 2015

SPARESE MATRIX[READING & ADDITION]

DESCRIPTION

In numerical analysis, a sparse matrix is a matrix in which most of the elements are zero 
so storing these much zeros ore wastage of memory and hence sparse matrices are represented using triplet  representation in computers
triplet  representation is a 3 column representation of a sparse matrix in which first row represent the whole matrix and preceding each row represent each element in it 

PROGRAM

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

int m,n;
void read(int c[5][5]);
void triple(int a[5][5],int t[10][3]);
int triplesum(int t1[10][3],int t2[10][3]);

void main()
{
 int a[5][5],b[5][5],t1[10][3],t2[10][3],i,j;
 clrscr();
 read(a);
 triple(a,t1);
 printf("The triple representation is:\n");
 for(i=0;i<=t1[0][2];i++)
  {
   for(j=0;j<3;j++)
   printf("%d ",t1[i][j]);
   printf("\n");
  }


 read(b);
 triple(b,t2);
 printf("The triple representation is:\n");
 for(i=0;i<=t2[0][2];i++)
  {
   for(j=0;j<3;j++)
   printf("%d ",t2[i][j]);
   printf("\n");
  }
 triplesum(t1,t2);
 getch();
}

 void read(int c[5][5])
  {
    int i,j;
    printf("\nEnter the row and column\n");
    scanf("%d%d",&n,&m);
    printf("Enter the number\n");
    for(i=0;i<n;i++)
     for(j=0;j<m;j++)
      scanf("%d",&c[i][j]);
  }

 void triple(int a[5][5],int t[10][3])
  {
    int i,j,l=1;
    t[0][0]=n;
    t[0][1]=m;
    t[0][2]=0;
    for(i=0;i<n;i++)
    {
     for(j=0;j<m;j++)
      {
       if(a[i][j]!=0)
       {
            t[0][2]++;
            t[l][0]=i;
            t[l][1]=j;
            t[l][2]=a[i][j];
            l++;
       }
      }
    }
  }

  int triplesum(int t1[10][3],int t2[10][3])
   {
    int i=1,j=1,k=1,p1,p2,q1,q2,s1,s2;
     int c[10][3];
     p1=t1[0][0];
     p2=t2[0][0];
     q1=t1[0][1];
     q2=t2[0][1];
     if((p1!=p2)||(q1!=q2))
      {
       printf("The addition is not possible!!!");
       getch();
       exit(0);
      }
      s1=t1[0][2];
      s2=t2[0][2];
      c[0][0]=p1;
      c[0][1]=q1;
      c[0][2]=0;
      while(i<=s1&&j<=s2)
      {
       if(t1[i][0]==t2[j][0]&&t1[i][1]==t2[j][1])
            {
              c[k][0]=t1[i][0];
              c[k][1]=t1[i][1];
              c[k][2]=t1[i][2]+t2[j][2];
              c[0][2]++;
              i++;
              j++;
              k++;
            }

      else if(t1[i][0]<t2[j][0])
            {
              c[k][0]=t1[i][0];
              c[k][1]=t1[i][1];
              c[k][2]=t1[i][2];
              c[0][2]++;
              i++;
              k++;
            }
      else if(t1[i][0]>t2[j][0])
            {
              c[k][0]=t2[j][0];
              c[k][1]=t2[j][1];
              c[k][2]=t2[j][2];
              c[0][2]++;
              j++;
              k++;
            }
     else  if(t1[i][0]==t2[j][0]&&t1[i][1]<t2[j][1])
            {
              c[k][0]=t1[i][0];
              c[k][1]=t1[i][1];
              c[k][2]=t1[i][2];
              c[0][2]++;
              i++;
              k++;
            }
       else
            {
              c[k][0]=t2[j][0];
              c[k][1]=t2[j][1];
              c[k][2]=t2[j][2];
              c[0][2]++;
              j++;
              k++;
            }
       while(j<=s2)
            {
              c[k][0]=t2[j][0];
              c[k][1]=t2[j][1];
              c[k][2]=t2[j][2];
              c[0][2]++;
              j++;
              k++;
            }
            while(i<=s1)
            {
              c[k][0]=t1[i][0];
              c[k][1]=t1[i][1];
              c[k][2]=t1[i][2];
              c[0][2]++;
              i++;
              k++;
            }
      }
      printf("The sum of triple representations are:\n");
 for(i=0;i<=c[0][2];i++)
  {
   for(j=0;j<3;j++)
   printf("%d ",c[i][j]);
   printf("\n");
  }
  return 0;
  }


OUTPUT

Enter the row and column
  3
  2
Enter the number
  0
  0
  0
  6
  0
  0
The triple representation is
  3 2 1
  1 1 6

Enter the row and column
3
2
Enter the number
0
1
3
0
0
6
The triple representation is:
3 2 3
0 1 1
1 0 3
2 1 6
The sum of triple representations are:
3 2 4
0 1 1
1 0 3
2 1 6

1 1 6

No comments:

Post a Comment