Pages

Ads 468x60px

Monday, November 17, 2008

Sparse Matrix Addition(With out using pointers)

Sparse Matrix Addition(With out using pointers)


/***************************************************

-> This programm is to implement sparse matrix
addition , with out using pointers to store
the matrix
-> This programm works for matrices which have
less than 100 elements (U can just change)
-> This program works in microsoft vc++ 6.0 in
windows xp
-> For simplicity it is assumed that there
are no duplicates (U should not enter two
elements with same row no. and column no. ie the
the program can not detect the error)

****************************************************/

#include
#define MAX 100

class sparse_matrix
{
private:
int r1,c1,r2,c2;
int n1,n2; // no of elements in matrices
int a1[MAX][3];
int a2[MAX][3];
public:
void read_matrix1();
void read_matrix2();
void display_matrix1();
void display_matrix2();
void add_matrices();
void function();
};

void sparse_matrix::read_matrix1()
{
cout<<”\nEnter values for matrix1 ::\n”;
cout<<”Enter the no. of rows and columns ::”;
cin>>r1>>c1;

int m,n,d;
int i=0;
cout<<”\nEnter integer matrix elements for the matrix1::\n”;
cout<<”\n(Enter -1 -1 for row no. or column no. to end)\n”;
cout<<”Enter row no. , column no. ,and data ::”;

cin>>m>>n>>d;

while(m!=-1 && n!=-1 )
{
a1[i][0]=m;
a1[i][1]=n;
a1[i][2]=d;
i++;
cout<<”\nEnter row no. , column no. ,and data ::”;
cin>>m>>n>>d;
}
n1=i;
//step1 :: sorting by row no.

int j,k;
for(j=0;j<=n1-2;j++)
{
for(k=0;k<=n1-j-2;k++)
{
if(a1[k][0]>a1[k+1][0])
{
int t=a1[k][0];
a1[k][0]=a1[k+1][0];
a1[k+1][0]=t;

t=a1[k][1];
a1[k][1]=a1[k+1][1];
a1[k+1][1]=t;

t=a1[k][2];
a1[k][2]=a1[k+1][2];
a1[k+1][2]=t;
}
}
}

// step2 :: sorting by column no.
for(j=0;j<=n1-2;j++)
{
for(k=0;k<=n1-j-2;k++)
{
if(a1[k][0]==a1[k+1][0] && a1[k][1]>a1[k+1][1])
{
int t;

t=a1[k][1];
a1[k][1]=a1[k+1][1];
a1[k+1][1]=t;

t=a1[k][2];
a1[k][2]=a1[k+1][2];
a1[k+1][2]=t;
}
}
}

}

void sparse_matrix::read_matrix2()
{
cout<<”\nEnter values for matrix2 ::\n”;
cout<<”Enter the no. of rows and columns ::”;
cin>>r2>>c2;

int m,n,d;
int i=0;
cout<<”\nEnter integer matrix elements for the matrix::\n”;
cout<<”\n( Enter -1 -1 for row no. or column no. to end )\n”;
cout<<”Enter row no. , column no. ,and data ::”;

cin>>m>>n>>d;

while(m!=-1 && n!=-1 )
{
a2[i][0]=m;
a2[i][1]=n;
a2[i][2]=d;
i++;
cout<<”Enter row no. , column no. ,and data ::\n”;
cin>>m>>n>>d;
}
n2=i;

// step1 :: sorting by row no.
int j,k;
for(j=0;j<=n2-2;j++)
{
for(k=0;k<=n2-j-2;k++)
{
if(a2[k][0]>a2[k+1][0])
{
int t=a2[k][0];
a2[k][0]=a2[k+1][0];
a2[k+1][0]=t;

t=a2[k][1];
a2[k][1]=a2[k+1][1];
a2[k+1][1]=t;

t=a2[k][2];
a2[k][2]=a2[k+1][2];
a2[k+1][2]=t;
}
}
}

// step2 :: sorting by column no.
for(j=0;j<=n2-2;j++)
{
for(k=0;k<=n2-j-2;k++)
{
if(a2[k][0]==a2[k+1][0] && a2[k][1]>a2[k+1][1])
{
int t;
t=a2[k][1];
a2[k][1]=a2[k+1][1];
a2[k+1][1]=t;

t=a2[k][2];
a2[k][2]=a2[k+1][2];
a2[k+1][2]=t;
}
}
}
}

void sparse_matrix::display_matrix1()
{
for(int i=0;i cout< <}

void sparse_matrix::display_matrix2()
{
for(int i=0;i cout< <}

void sparse_matrix::add_matrices()
{
if(r1!=r2 || c1!=c2)
{
cout<<”addition is not possible”;
return;
}
int i=0,j=0;
cout<<”The elements in the resultant matrix are ::\n”;
while(i {
if(a1[i][0]==a2[j][0] &&
a1[i][1]==a2[j][1])
{
cout< < i++;j++;
}
else if(a1[i][0] == a2[j][0])
{
if(a1[i][1] {
cout< < i++;
}
else
{
cout< < j++;
}
}
else
{
if(a1[i][0] < a2[j][0])
{
cout< < i++;
}
else
{
cout< < j++;
}
}
}
while(i++ cout< while(j++ cout<}

void sparse_matrix::function()
{
read_matrix1();
display_matrix1();
read_matrix2();
display_matrix2();
add_matrices();
}

void main()
{
sparse_matrix obj;
obj.function();
}

0 comments: