Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
program in c language for multiplication of two sparse matrices using doubly linked lists.
Posted
Comments
Nuri Ismail 16-Aug-10 11:01am    
OK, what you have tried so far and what is your SPECIFIC problem?
R. Giskard Reventlov 16-Aug-10 11:07am    
Reason for my vote of 1
Homework?
Smithers-Jones 16-Aug-10 11:22am    
Reason for my vote of 1
Yours is not even a full sentence, let alone a meaningful question.
Sandeep Mewara 16-Aug-10 11:32am    
Reason for my vote of 1
No Effort
Homework
Dylan Morley 16-Aug-10 12:24pm    
Reason for my vote of 1
Homework

Wait, give me a second...
That's an easy one:
"What is it, that krishna nisonko wants to have?"
Man, I love Jeopardy.
 
Share this answer
 
It does not work like this here.

Here is what is expected by enquirers:
1. TRY first what you want to do!
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.
 
Share this answer
 
please help me to improve the answer

C#
Sparse Matrix
#include <iostream.h>
#include <process.h>
#include <conio.h>
//This program implements the linked list representation of Sparse matrix
// for multiplying two Sparse Matrices
class Sparse
{
int i,j,k;
public:
Sparse *next;
Sparse()
{
i=j=k=0;
}
Sparse(int i,int j)
{
this->i=i;
this->j=j;
}
void SetNonZeroElements(Sparse *head,int k)
{
head->k=k;
}
void accept(int i,int j,int k)
{
this->i=i;
this->j=j;
this->k=k;
}
void display()
{
cout<<i<<" "<<j<<" "<<k<<endl;
}
int validateSparseMultiplication(Sparse *head1,Sparse *head2)
{
if (head1->j!=head2->i)
return 0;
else
return 1;
}
int MaxRow(Sparse *head)
{
if (head!=NULL)
return head->i;
else
return 0;
}
int MaxCol(Sparse *head)
{
if (head!=NULL)
return head->j;
else
return 0;
}
int RCValueExists(int ,int ,Sparse *);
void AddToElementIJ(int,int,int,Sparse *);
};
int Sparse::RCValueExists(int i,int j,Sparse *head)
{ Sparse *ptr=head->next;
for(;ptr!=NULL;ptr=ptr->next)
{
if ((ptr->i==i) && (ptr->j==j))
return ptr->k;
}
return 0;
TOP
}
void Sparse::AddToElementIJ(int i,int j,int k,Sparse *head)
{
Sparse *ptr=head->next;
for(;ptr!=NULL;ptr=ptr->next)
{
if ((ptr->i==i) && (ptr->j==j))
ptr->k=ptr->k+k;
return;
}
return;
}
void main()
{
int r,c,i,j,k,ctr;
Sparse *A,*B,*C,*start1,*start2,*start3,*ptr1,*ptr2;
clrscr();
//Accept Details Regarding First Matrix & Its Elements
cout<<endl<<"Enter no of rows in first sparse matrix : ";
cin>>r;
cout<<endl<<"Enter no of columns in first sparse matrix : ";
cin>>c;
cout<<endl<<"\t"<<"Enter elements of matrix A"<<endl<<endl;
ctr=0;
A=new Sparse(r,c);
start1=A;
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
cout<<"Enter element of "<<i<<"th row and "<<j<<"th column : ";
cin>>k;
if (k!=0)
{
A->next=new Sparse();
A=A->next;
A->accept(i,j,k);
ctr++;
}
}
A->next=NULL;
A->SetNonZeroElements(start1,ctr);
//Accept Details Regarding Second Matrix B & Its Elements
cout<<endl<<"Enter no of rows in second sparse matrix : ";
cin>>r;
cout<<endl<<"Enter no of columns in second sparse matrix : ";
cin>>c;
cout<<endl<<"\t"<<"Enter elements of matrix B"<<endl<<endl;
ctr=0;
B=new Sparse(r,c);
start2=B;
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
cout<<"Enter element of "<<i<<"th row and "<<j<<"th column : ";
cin>>k;
if (k!=0)
{
B->next=new Sparse();
B=B->next;
B->accept(i,j,k);
ctr++;
}
}
B->next=NULL;
B->SetNonZeroElements(start2,ctr);
clrscr();
cout<<endl<<"\t"<<"Sparse Matrix A"<<endl;
//Display stored elements of Matrix A
for(ptr1=start1;ptr1!=NULL;ptr1=ptr1->next)
ptr1->display();
cout<<endl<<"\t"<<"Sparse Matrix B"<<endl;
//Display stored elements of Matrix B
for(ptr1=start2;ptr1!=NULL;ptr1=ptr1->next)
ptr1->display();
//Validate Matrix Multiplication
if (A->validateSparseMultiplication(start1,start2)==0)
{
cout<<"Number of columns in Matrix A should be equal to"<<endl;
cout<<"Number of rows in Matrix B"<<endl;
exit(1);
}
C=new Sparse(r,B->MaxCol(start2));
start3=C;
ctr=0;
for(i=1;i<=A->MaxRow(start1);i++)
for(j=1;j<=A->MaxCol(start1);j++)
for(k=1;k<=B->MaxCol(start2);k++)
{
if (A->RCValueExists(i,j,start1)!=0 && B->RCValueExists(j,k,start2)!=0)
{
if (C->RCValueExists(i,k,start3)==0)
{
C->next=new Sparse(i,k);
C=C->next;
C->accept(i,k,A->RCValueExists(i,j,start1)*B->RCValueExists(j,k,start2));
}
else
{
C->AddToElementIJ(i,k,A->RCValueExists(i,j,start1)*B->RCValueExists(j,k,start2),start3);
}
ctr++;
}
}
C->next=NULL;
C->SetNonZeroElements(start3,ctr);
cout<<endl<<"\t"<<"Resultant Matrix"<<endl;
for(ptr2=start3;ptr2!=NULL;ptr2=ptr2->next)
ptr2->display();
}
 
Share this answer
 
v2
Comments
HimanshuJoshi 17-Aug-10 16:31pm    
Does this work? If not what is the error? Where specifically is the error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900