Skip to main content

Advance Sparse Matrix Operations in C languages

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");
}
}

Comments

Popular posts from this blog

Sample : String Reverse In Java

import java.io.*; class test {     public static void main(String args[])     {         DataInputStream in=new DataInputStream(System.in);         try         {             String text;             System.out.println("\nEnter The Text");             text=in.readLine();             System.out.println("\nIn Reverse Order::\n");             for(int i=text.length()-1;i>=0;i--)             {                 System.out.print(text.charAt(i));             }          ...

Using GREP in UNIX

How To Use grep Command In Linux / UNIX by  VIVEK GITE  on  AUGUST 2, 2007  ·  147 COMMENTS H ow do I use grep command in Linux? grep command searches the given file for lines containing a match to the given strings or words. By default, grep prints the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p grep command syntax grep 'word' filename grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something' Use grep to search file Search /etc/passwd for boo user: $ grep boo /etc/passwd You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option: $ grep -i "boo" /etc/passwd Use grep recursively You can search recursively i.e. read all files under each ...