Friday, August 23, 2013

Represent Sparse Matrix using array and perform Matrix Addition, Simple Transpose.


/*-------------------------------------------------------------------------------------------------------
Program    : Represent Sparse Matrix using array and perform Matrix
                      Addition, Simple Transpose.

Created By : JABIR DAUD PATHAN
Branch        : COMPUTER ENGINEERING
-------------------------------------------------------------------------------------------------------*/


/*--------------------<<  Header File  >>-----------------------*/

#include<stdio.h>
#include<conio.h>
#define MAX 10

/*------------<< Global Declaration of Functions  >>------------*/

void readsparse1(void);
void readsparse2(void);
void addsparse(void);
void simpletrans(void);

int a[MAX][10],b[MAX][3],c[MAX][3],i,j,k,m,n,x,y,ch;
                                                             //Globle declaration of variable

/*-----<< Function Definition to accept 1st sparse matrix >>-----*/

void readsparse1()
 {
  printf("\n\nEnter no of rows and no. of columns and no. of non zero element==>\n\n");
  scanf("%d%d%d",&m,&n,&x);
                          //accept no. of rows,columns & non zero elements from user
  a[0][0]=m;
  a[0][1]=n;
  a[0][2]=x;

  printf("\nEnter the element==>\n\n");

  for(i=1;i<=x;i++)
   {
    scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]);
                           //accept non zero elements of 1st sparse matrix from user
   }
  printf("\nrow=%d\tcol=%d\tval=%d\n",m,n,x);

  for(i=1;i<=x;i++)
   {
    printf("\n%d\t%d\t%d",a[i][0],a[i][1],a[i][2]);//display accepted elements
   }
 }

/*-------<< Function Definition to accept 2nd sparse matrix >>-----*/

void readsparse2()
 {
  printf("\n\nEnter no of rows and no. of columns and no. of non zero element==>\n\n");
  scanf("%d%d%d",&m,&n,&y);
                        //accept no. of rows,columns & non zero elements from user
  b[0][0]=m;
  b[0][1]=n;
  b[0][2]=y;

  printf("\nEnter the element==>\n\n");

  for(j=1;j<=y;j++)
   {
    scanf("%d%d%d",&b[j][0],&b[j][1],&b[j][2]);
                         //accept non zero elements of 2nd sparse matrix from user
   }
  printf("\nrow=%d\tcol=%d\tval=%d\n",m,n,y);

  for(j=1;j<=y;j++)
   {
    printf("\n%d\t%d\t%d",b[j][0],b[j][1],b[j][2]);//display accepted elements
   }
 }

/*-----<< Function Definition for addition of sparse matrix >>-----*/

void addsparse()
 {
  i=j=k=0;
  while(i<=x && j<=y) //test condition till i becomes x and j becomes y
   {
    if(a[i][0]<b[j][0])
     {
      c[k][0]=a[i][0];
      c[k][1]=a[i][1];
      c[k][2]=a[i][2];
      k++;
      i++;
      goto last;
     }
    if(a[i][0]>b[j][0])
     {
      c[k][0]=b[j][0];
      c[k][1]=b[j][1];
      c[k][2]=b[j][2];
      k++;
      j++;
      goto last;
     }
    if(a[i][1]<b[j][1])
     {
      c[k][0]=a[i][0];
      c[k][1]=a[i][1];
      c[k][2]=a[i][2];
      k++;
      i++;
      goto last;
     }
    if(a[i][1]>b[j][1])
     {
      c[k][0]=b[j][0];
      c[k][1]=b[j][1];
      c[k][2]=b[j][2];
      k++;
      j++;
      goto last;
     }
    else
     {
      c[k][0]=a[i][0];
      c[k][1]=a[i][1];
      c[k][2]=a[i][2]+b[j][2];
      k++;
      i++;
      j++;
     }
   }

last:
     while(i<=x)
      {
       c[k][0]=a[i][0];
       c[k][1]=a[i][1];
       c[k][2]=a[i][2];
       k++;
       i++;
      }
     while(j<=y)
      {
       c[k][0]=b[j][0];
       c[k][1]=b[j][1];
       c[k][2]=b[j][2];
       k++;
       j++;
      }
     c[0][2]=k-1;

  printf("\n\n\nAddition of sparse matrix==>\n");

  for(k=1;k<=x;k++)
   {
    printf("\n%d\t%d\t%d",c[k][0],c[k][1],c[k][2]);
                                                      //display addition of sparse matrix
   }
 }

/*-----<<Function Definition for transpose of sparse matrix>>------*/

void simpletrans()
 {
  b[0][0]=a[0][1];
  b[0][1]=a[0][0];
  b[0][2]=a[0][2];

  k=1;
  for(i=0;i<=a[0][1];i++)
   {
    for(j=0;j<=a[0][2];j++)
     {
      if(i==a[j][1])
       {
                b[k][0]=a[j][1];
                b[k][1]=a[j][0];
                b[k][2]=a[j][2];
                k++;
       }
     }
   }
  printf("\n\n\nTranspose of sparse matrix==>\n");
  for(k=1;k<=x;k++)
   {
    printf("\n%d\t%d\t%d",b[k][0],b[k][1],b[k][2]);
   }
 }

/*-------------<<  Main Function Starts  >>---------------*/

void main()
 {
  char ans;  //Variable declaration
do
 {
  clrscr(); //Clear the text mode of window
  printf("\n\t\t\t---------<<  MENU  >>---------\n");
  printf("\n\t\t\t1.Addition of sparse matrix");
  printf("\n\t\t\t2.Transpose of sparse matrix");
  printf("\n\t\t\t3.Exit");
  printf("\n\n\t\t\t-----------------------------------");

  printf("\n\n\t\t\tEnter your choice==>");
  scanf("%d",&ch); //accept choice from user

  switch(ch)
   {
    case 1 :
                    readsparse1(); //function call
                    readsparse2(); //function call
                    addsparse();   //function call
                    break;

    case 2 :
                    readsparse1();
                    simpletrans();
                    break;

    case 3 :
                    exit(0);
                    break;

   default :
                    printf("\n\nInvalid choice");
                    break;
   }
   printf("\n\nDo you want to continue(Y/N)?==>");
   ans=getch();
  }while(ans=='y'||ans=='Y');
   getch();
 }

/*--------------<<  End Of Main Function  >>------------*/



/*-----------------<< OUTPUT SCREEN  >>-------------------*/

                        ---------<<  MENU  >>---------
                        1.Addition of sparse matrix
                        2.Transpose of sparse matrix
                        3.Exit
                        ------------------------------------

                        Enter your choice==>1

Enter no of rows and no. of columns and no. of non zero element==>

3
4
5

Enter the element==>
0
2
5
1
0
5
1
2
3
2
1
1
2
3
2

row=3   col=4   val=5

0       2       5
1       0       5
1       2       3
2       1       1
2       3       2

Enter no of rows and no. of columns and no. of non zero element==>
3
4
5

Enter the element==>
0
2
2
1
0
1
1
2
5
2
1
1
2
3
3

row=3   col=4   val=5

0       2       2
1       0       1
1       2       5
2       1       1
2       3       3


Addition of sparse matrix==>

0       2       7
1       0       6
1       2       8
2       1       2
2       3       5

Do you want to continue(Y/N)?==>Y


                        ---------<<  MENU  >>---------
                        1.Addition of sparse matrix
                        2.Transpose of sparse matrix
                        3.Exit
                        -------------------------------------

                        Enter your choice==>2


Enter no of rows and no. of columns and no. of non zero element==>

4
4
6

Enter the element==>

0
2
9
1
1
1
2
0
5
2
2
9
3
0
8
3
3
6

row=4   col=4   val=6

0       2       9
1       1       1
2       0       5
2       2       9
3       0       8
3       3       6


Transpose of sparse matrix==>

0       2       5
0       3       8
1       1       1
2       0       9
2       2       9
3       3       6

Do you want to continue(Y/N)?==>Y


                        ---------<<  MENU  >>---------
                        1.Addition of sparse matrix
                        2.Transpose of sparse matrix
                        3.Exit
                        -------------------------------------

                        Enter your choice==>3







No comments:

Post a Comment