Advance Sparse Matrix Operations in C languages
by DIBYENDU SWAR (rajamax1987@yahoo.com)
-----------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
int isSparse(int [3][3]);
void getSparse(int [3][3]);
void dispSparse(int [3][3]);
void sumSparse(int [3][3],int [3][3],int [3][3]);
void multiSparse(int [3][3],int [3][3],int [3][3]);
void main()
{
int mat[3][3],mat1[3][3],rslt[3][3];
int i;
do{
clrscr();
printf("\n\t\tDATA STRUCTUTE OVER 2D ARRAY [DIBYENDU]\n\t\t------------------------------\n");
printf("\n\t\tSPARSE MATRIX OPERATIONS\n\n\t\t1. LOAD SPARSE [GET DATA]\n\n\t\t2. SUM SPARSE\n\nt\t\t3. MULTIPLY SPARSE\n\n\t\t4. DISPLAY MATRIXes\n\n\t\t5. DISPLAY SPARSEes\n\n\t\t6. EXIT\n\n\t\tENTER CHOICE :: ");
scanf("%d",&i);
switch(i)
{
case 1:
getSparse(mat);
getSparse(mat1);
break;
case 2:
sumSparse(mat,mat1,rslt);
getch();
break;
case 3:
multiSparse(mat,mat1,rslt);
getch();
break;
case 4:
dispSparse(mat);
dispSparse(mat1);
getch();
break;
case 5:
if(isSparse(mat)==1)
{
printf("\n\nFirse Matrix as SPARSE");
dispSparse(mat);
}
if(isSparse(mat1)==1)
{
printf("\n\nSecond Matrix as SPARSE");
dispSparse(mat1);
}
getch();
break;
case 6:
exit();
default:
printf("\nInvalid Choise");
break;
}
}while(1);
}
void getSparse(int m[3][3])
{
int i,j;
printf("\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter Value of matrix_1[%d][%d] :: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
}
int isSparse(int m[3][3])
{
int i,j,f,zr=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(m[i][j]==0)
{
zr++;
}
}
}
if(zr>=((3*3)/2))
{
f=1;
}else
{
f=0;
}
return(f);
}
void dispSparse(int m[3][3])
{
int i,j;
printf("\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("\t%d",m[i][j]);
}
}
printf("\n\n");
}
void sumSparse(int m[3][3],int n[3][3],int r[3][3])
{
int i,j;
if(isSparse(m)==1 && isSparse(n)==1)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
r[i][j]=m[i][j]+n[i][j];
}
}
dispSparse(m);
dispSparse(n);
printf("\nSummation of Two Sparse Matrix\n------------\n");
dispSparse(r);
}else{
printf("\nOne or Both of the MATRIX es are not Sparse");
}
}
void multiSparse(int m[3][3],int n[3][3],int r[3][3])
{
int i,j,k;
if(isSparse(m)==1 && isSparse(n)==1)
{
for(i=0;i<3;i++)
{
for(j=0;j< 3;j++)
{
r[i][j] = 0;
for(k=0;k<3;k++)
{
r[i][j] = r[i][j] + m[i][k] * n[k][j];
}
}
}
dispSparse(m);
dispSparse(n);
printf("\nMultiplication of Two Sparse Matrix\n------------\n");
dispSparse(r);
}else{
printf("\nOne or Both of the MATRIX es are not Sparse");
}
}
by DIBYENDU SWAR (rajamax1987@yahoo.com)
-----------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
int isSparse(int [3][3]);
void getSparse(int [3][3]);
void dispSparse(int [3][3]);
void sumSparse(int [3][3],int [3][3],int [3][3]);
void multiSparse(int [3][3],int [3][3],int [3][3]);
void main()
{
int mat[3][3],mat1[3][3],rslt[3][3];
int i;
do{
clrscr();
printf("\n\t\tDATA STRUCTUTE OVER 2D ARRAY [DIBYENDU]\n\t\t------------------------------\n");
printf("\n\t\tSPARSE MATRIX OPERATIONS\n\n\t\t1. LOAD SPARSE [GET DATA]\n\n\t\t2. SUM SPARSE\n\nt\t\t3. MULTIPLY SPARSE\n\n\t\t4. DISPLAY MATRIXes\n\n\t\t5. DISPLAY SPARSEes\n\n\t\t6. EXIT\n\n\t\tENTER CHOICE :: ");
scanf("%d",&i);
switch(i)
{
case 1:
getSparse(mat);
getSparse(mat1);
break;
case 2:
sumSparse(mat,mat1,rslt);
getch();
break;
case 3:
multiSparse(mat,mat1,rslt);
getch();
break;
case 4:
dispSparse(mat);
dispSparse(mat1);
getch();
break;
case 5:
if(isSparse(mat)==1)
{
printf("\n\nFirse Matrix as SPARSE");
dispSparse(mat);
}
if(isSparse(mat1)==1)
{
printf("\n\nSecond Matrix as SPARSE");
dispSparse(mat1);
}
getch();
break;
case 6:
exit();
default:
printf("\nInvalid Choise");
break;
}
}while(1);
}
void getSparse(int m[3][3])
{
int i,j;
printf("\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter Value of matrix_1[%d][%d] :: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
}
int isSparse(int m[3][3])
{
int i,j,f,zr=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(m[i][j]==0)
{
zr++;
}
}
}
if(zr>=((3*3)/2))
{
f=1;
}else
{
f=0;
}
return(f);
}
void dispSparse(int m[3][3])
{
int i,j;
printf("\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("\t%d",m[i][j]);
}
}
printf("\n\n");
}
void sumSparse(int m[3][3],int n[3][3],int r[3][3])
{
int i,j;
if(isSparse(m)==1 && isSparse(n)==1)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
r[i][j]=m[i][j]+n[i][j];
}
}
dispSparse(m);
dispSparse(n);
printf("\nSummation of Two Sparse Matrix\n------------\n");
dispSparse(r);
}else{
printf("\nOne or Both of the MATRIX es are not Sparse");
}
}
void multiSparse(int m[3][3],int n[3][3],int r[3][3])
{
int i,j,k;
if(isSparse(m)==1 && isSparse(n)==1)
{
for(i=0;i<3;i++)
{
for(j=0;j< 3;j++)
{
r[i][j] = 0;
for(k=0;k<3;k++)
{
r[i][j] = r[i][j] + m[i][k] * n[k][j];
}
}
}
dispSparse(m);
dispSparse(n);
printf("\nMultiplication of Two Sparse Matrix\n------------\n");
dispSparse(r);
}else{
printf("\nOne or Both of the MATRIX es are not Sparse");
}
}
Comments
Post a Comment