Operations on Sparse Matrix
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void printsparse(int b[MAX][3]);
void readsparse(int b[MAX][3]);
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3]);
void transpose(int[MAX][3],int[MAX][3]);
int main()
{
setbuf(stdout,NULL);
int b1[MAX][3],b2[MAX][3],b3[MAX][3],b4[MAX][4];
readsparse(b1);
readsparse(b2);
printf("\n**Addition of Sparse Matrix**\n");
addsparse(b1,b2,b3);
printsparse(b3);
printf("\n**Transpose of Sparse Matrix 1**\n");
transpose(b1,b4);
printsparse(b4);
return 0;
}
void readsparse(int b[MAX][3])
{
int i,t,m,n;
printf("\nEnter no. of rows and columns:");
scanf("%d%d",&m,&n);
printf("No. of non-zero triples:");
scanf("%d",&t);
b[0][0]=m;
b[0][1]=n;
b[0][2]=t;
for(i=1;i<=t;i++)
{
printf("Enter the triples(row,column,value):");
scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
}
}
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
{
int t1,t2,i,j,k;
if(b1[0][0]!=b2[0][0]||b1[0][1]!=b2[0][1])
{
printf("\nYou have entered invalid matrix!!Size must be equal");
exit(0);
}
t1=b1[0][2];
t2=b2[0][2];
i=j=k=1;
b3[0][0]=b1[0][0];
b3[0][1]=b1[0][1];
while(i<=t1&&j<=t2)
{
if(b1[i][0]<b2[j][0]) //row numbers are not equal
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
else if(b1[i][0]>b2[j][0]) //row numbers are not equal
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
else if(b1[i][1]<b2[j][1]) //row numbers are equal, compare column
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
else if(b1[i][1]>b2[j][1]) //row numbers are equal, compare column
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
else
{
b3[k][0]=b1[i][0]; //row and column numbers are equal
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2]+b2[j][2];
k++;
i++;
j++;
}
}
while(i<=t1) //copy remaining terms from b1
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
i++;
k++;
}
while(j<=t2) //copy remaining terms from b2
{
b3[k][0]=b2[j][0];
b3[k][1]=b1[j][1];
b3[k][2]=b1[j][2];
j++;
k++;
}
b3[0][2]=k-1; //set numbe of terms in b3
}
void printsparse(int b[MAX][3])
{
int i,t;
t=b[0][2];
printf("\nrow \tcolumn \tvalue");
for(i=0;i<=t;i++)
{
printf("\n%d\t%d\t%d",b[i][0],b[i][1],b[i][2]);
}
}
void transpose(int b1[][3],int b2[][3])
{
int i,j,k,n;
b2[0][0]=b1[0][1];
b2[0][1]=b1[0][0];
b2[0][2]=b1[0][2];
k=1;
n=b1[0][2];
for(i=0;i<b1[0][1];i++)
for(j=1;j<=n;j++)
//if a column number of current triple==i then insert the current triple in b2
if(i==b1[j][1])
{
b2[k][0]=i;
b2[k][1]=b1[j][0];
b2[k][2]=b1[j][2];
k++;
}
}
#include<stdlib.h>
#define MAX 20
void printsparse(int b[MAX][3]);
void readsparse(int b[MAX][3]);
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3]);
void transpose(int[MAX][3],int[MAX][3]);
int main()
{
setbuf(stdout,NULL);
int b1[MAX][3],b2[MAX][3],b3[MAX][3],b4[MAX][4];
readsparse(b1);
readsparse(b2);
printf("\n**Addition of Sparse Matrix**\n");
addsparse(b1,b2,b3);
printsparse(b3);
printf("\n**Transpose of Sparse Matrix 1**\n");
transpose(b1,b4);
printsparse(b4);
return 0;
}
void readsparse(int b[MAX][3])
{
int i,t,m,n;
printf("\nEnter no. of rows and columns:");
scanf("%d%d",&m,&n);
printf("No. of non-zero triples:");
scanf("%d",&t);
b[0][0]=m;
b[0][1]=n;
b[0][2]=t;
for(i=1;i<=t;i++)
{
printf("Enter the triples(row,column,value):");
scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
}
}
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
{
int t1,t2,i,j,k;
if(b1[0][0]!=b2[0][0]||b1[0][1]!=b2[0][1])
{
printf("\nYou have entered invalid matrix!!Size must be equal");
exit(0);
}
t1=b1[0][2];
t2=b2[0][2];
i=j=k=1;
b3[0][0]=b1[0][0];
b3[0][1]=b1[0][1];
while(i<=t1&&j<=t2)
{
if(b1[i][0]<b2[j][0]) //row numbers are not equal
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
else if(b1[i][0]>b2[j][0]) //row numbers are not equal
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
else if(b1[i][1]<b2[j][1]) //row numbers are equal, compare column
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
else if(b1[i][1]>b2[j][1]) //row numbers are equal, compare column
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
else
{
b3[k][0]=b1[i][0]; //row and column numbers are equal
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2]+b2[j][2];
k++;
i++;
j++;
}
}
while(i<=t1) //copy remaining terms from b1
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
i++;
k++;
}
while(j<=t2) //copy remaining terms from b2
{
b3[k][0]=b2[j][0];
b3[k][1]=b1[j][1];
b3[k][2]=b1[j][2];
j++;
k++;
}
b3[0][2]=k-1; //set numbe of terms in b3
}
void printsparse(int b[MAX][3])
{
int i,t;
t=b[0][2];
printf("\nrow \tcolumn \tvalue");
for(i=0;i<=t;i++)
{
printf("\n%d\t%d\t%d",b[i][0],b[i][1],b[i][2]);
}
}
void transpose(int b1[][3],int b2[][3])
{
int i,j,k,n;
b2[0][0]=b1[0][1];
b2[0][1]=b1[0][0];
b2[0][2]=b1[0][2];
k=1;
n=b1[0][2];
for(i=0;i<b1[0][1];i++)
for(j=1;j<=n;j++)
//if a column number of current triple==i then insert the current triple in b2
if(i==b1[j][1])
{
b2[k][0]=i;
b2[k][1]=b1[j][0];
b2[k][2]=b1[j][2];
k++;
}
}
Enter no. of rows and columns:5 5
No. of non-zero triples:5
Enter the triples(row,column,value):1 2 10
Enter the triples(row,column,value):1 4 12
Enter the triples(row,column,value):3 3 5
Enter the triples(row,column,value):4 1 15
Enter the triples(row,column,value):4 2 12
Enter no. of rows and columns:5 5
No. of non-zero triples:5
Enter the triples(row,column,value):1 3 8
Enter the triples(row,column,value):2 4 23
Enter the triples(row,column,value):3 3 9
Enter the triples(row,column,value):4 1 20
Enter the triples(row,column,value):4 2 25
**Addition of Sparse Matrix**
row column value
5 5 7
1 2 10
1 3 8
1 4 12
2 4 23
3 3 14
4 1 35
4 2 37
**Transpose of Sparse Matrix 1**
row column value
5 5 5
1 4 15
2 1 10
2 4 12
3 3 5
4 1 12
No. of non-zero triples:5
Enter the triples(row,column,value):1 2 10
Enter the triples(row,column,value):1 4 12
Enter the triples(row,column,value):3 3 5
Enter the triples(row,column,value):4 1 15
Enter the triples(row,column,value):4 2 12
Enter no. of rows and columns:5 5
No. of non-zero triples:5
Enter the triples(row,column,value):1 3 8
Enter the triples(row,column,value):2 4 23
Enter the triples(row,column,value):3 3 9
Enter the triples(row,column,value):4 1 20
Enter the triples(row,column,value):4 2 25
**Addition of Sparse Matrix**
row column value
5 5 7
1 2 10
1 3 8
1 4 12
2 4 23
3 3 14
4 1 35
4 2 37
**Transpose of Sparse Matrix 1**
row column value
5 5 5
1 4 15
2 1 10
2 4 12
3 3 5
4 1 12
No comments:
Post a Comment