用c程序将两个稀疏矩阵相乘 [英] c program to multiply two sparse matrices

查看:109
本文介绍了用c程序将两个稀疏矩阵相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用c语言编写的程序,用于使用双向链表将两个稀疏矩阵相乘.

program in c language for multiplication of two sparse matrices using doubly linked lists.

推荐答案

等等,请给我第二个...
那很简单:
克里希纳·尼森科想要拥有什么?"
伙计,我爱危险.
Wait, give me a second...
That''s an easy one:
"What is it, that krishna nisonko wants to have?"
Man, I love Jeopardy.


在这里无法正常工作.

这是询问者的期望:
1. 先尝试您要做什么!
2.制定看起来像问题/无法解决的问题.

尝试一下,告诉您是否遇到问题.
成员将很乐意为您提供帮助.
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.


请帮助我改善答案

please help me to improve the answer

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


这篇关于用c程序将两个稀疏矩阵相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆