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

Background Process in ORACLE Database

Background Process in ORACLE Database Database Writer Process (DBWn) Log Writer Process (LGWR) Checkpoint Process (CKPT) System Monitor Process (SMON) Process Monitor Process (PMON) Recoverer Process (RECO) Job Queue Processes Archiver Processes (ARCn) Queue Monitor Processes (QMNn) Database Writer Process (DBWn) The database writer process (DBWn) writes the contents of buffers to datafiles. The DBWn processes are responsible for writing modified (dirty) buffers in the database buffer cache to disk. Although one database writer process (DBW0) is adequate for most systems, you can configure additional processes (DBW1 through DBW9 and DBWa through DBWj) to improve write performance if your system modifies data heavily. These additional DBWn processes are not useful on uniprocessor systems. When a buffer in the database buffer cache is modified, it is marked dirty. A cold buffer is a buffer that has not been recently used according to the least recently used (LRU) algorith...

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 ...

A Hit Count Using Java Servlet's Session Tracking

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionTracker extends HttpServlet {         public void doGet(HttpServletRequest req,HttpServletResponse res)         throws ServletException,IOException         {                res.setContentType("text/html");                PrintWriter out=res.getWriter();                HttpSession session=req.getSession(true);                Integer count=(Integer)session.getValue("tracker.count");                if(count==null)       ...