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

Java Sample: While loop

public class whilechk1 {     public static void main(String args[])     {         int row,column,y;         System.out.print("\nMultiplication Table\n");         row=1;         do         {             column=1;             do             {                 y=row*column;                 System.out.print("\t"+y);                 column++;             }while(column<=10);             System.out.println("\n");             row++;         }while(row<=10);     } }

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

Hi! I'm Java...

Java is a computer programming language. It enables programmers to write computer instructions using English based commands, instead of having to write in numeric codes. It’s known as a “high-level” language because it can be read and written easily by humans. Like English, Java has a set of rules that determine how the instructions are written. These rules are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute. Who Created Java? In the early nineties, Java was created by a team led by James Gosling for Sun Microsystems. It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated webpages . Over the years it has evolved as a successful language for ...