Wednesday, 27 August 2014

SPARSE MATRIX class work

#include<stdio.h>
#include<conio.h>
int main()
{
 int a[10][10],b[10][10],m,n,s=0,i,j;
 printf("\nEnter the order of m*n of the sparse matrix");
 scanf("%d%d",&m,&n);
 printf("Enter the elements in aparse matrix mostly zero\n");
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     printf("\n%d row %d column",i,j);
     scanf("%d",&a[i][j]);              
    }              
  }
 
  printf("\nThe given matrix is\n");
  for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     if(a[i][j]!=0)
     {
      b[s][0]=a[i][j];
      b[s][1]=a[i][j];
      b[s][2]=a[i][j];
      s++;          
     }              
    }              
  }
 
  printf("\nThe sparse matrix is given by\n");
 for(i=0;i<s;i++)
  {
   for(j=0;j<s;j++)
    {
     printf("%d\t",b[i][j]);              
    }              
    printf("\n");
  }
  return 0;
}

2 comments:

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