0% found this document useful (0 votes)
2 views

pl8

The document contains a C program that accepts conventional matrices and converts them into sparse matrices using a specific structure. It provides functionalities for matrix addition, simple transpose, and fast transpose. The program includes a menu-driven interface for user interaction and operations on the matrices.

Uploaded by

artisinghmpsagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

pl8

The document contains a C program that accepts conventional matrices and converts them into sparse matrices using a specific structure. It provides functionalities for matrix addition, simple transpose, and fast transpose. The program includes a menu-driven interface for user interaction and operations on the matrices.

Uploaded by

artisinghmpsagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*Accept conventional matrix and convert it into sparse matrix using structure and

perform
addition, simple and fast transpose*/
#include<stdio.h>
void accept(int x[10][10],int r,int c);
void display(int x[10][10],int r,int c);
void displays(int b[10][3]);
void convert(int x[10][10],int b[10][3],int r,int c);
void add(int b1[10][3],int b2[10][3],int b3[10][3]);
void ftranspose(int b1[10][3],int b2[10][3]);
void transpose(int b1[10][3],int b2[10][3]);
void main()
{
int mat1[10][10],mat2[10][10],b1[10][3],b2[10][3],b3[10][3];
int choice,row1,row2,col1,col2;
do
{
printf("\n Main Menu : \n 1. Accept and display matrix1 \n 2. Accept and display
matrix2 \n 3.Convert matrix 1 into sparse matrix \n 4.Convert matrix 2 into sparse
matrix \n 5. Addition \n 6.Simple Transpose \n 7.Fast Transpose \n 8. Exit \n Enter
your choice :\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n Enter the number of rows in matrix 1\n");
scanf("%d",&row1);
printf("\n Enter the number of columns in matrix 1 \n");
scanf("%d",&col1);
printf("\n Enter the elements -");
accept(mat1,row1,col1);
printf("\n Matrix 1 :- \n");
display(mat1,row1,col1);
break;
case 2:printf("\n Enter the number of rows in matrix 2 \n");
scanf("%d",&row2);
printf("\n Enter the number of columns in matrix 2 \n");
scanf("%d",&col2);
printf("\n Enter the elements -");
accept(mat2,row2,col2);
printf("\n Matrix 2 :- \n");
display(mat2,row2,col2);
break;
case 3:convert(mat1,b1,row1,col1);
printf("\n Sparse matrix 1-\n");
displays(b1);
break;
case 4:convert(mat2,b2,row2,col2);
printf("\n Sparse matrix 2-\n");
displays(b2);
break;
case 5:add(b1,b2,b3);
displays(b3);
break;
case 6:printf(" \n Simple Transpose \n \n Transpose of sparse matrix 1 - \n ");
transpose(b1,b3);
printf("\n Transpose of sparse matrix 2 - \n");
transpose(b2,b3);
break;
case 7:printf(" \n Fast Transpose \n \n Transpose of sparse matrix 1 - \n ");
ftranspose(b1,b3);
printf("\n Transpose of sparse matrix 2 - \n");
ftranspose(b2,b3);
break;break;
case 8:printf("\n Have a nice day !! \n");
break;
default:printf("\n Invalid choice!! Please enter again. \n");
break;
}
}while(choice!=8);
}

void accept(int x[10][10],int r,int c)


{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&x[i][j]);
}
}
}

void display(int x[10][10],int r,int c)


{
int i,j;
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%d ",x[i][j]);
}
}
}
void displays(int b[10][3])
{
int row,i;
row=b[0][2];
for(i=0;i<=row;i++)
{
printf("\n%d\t%d\t%d",b[i][0],b[i][1],b[i][2]);
}
}
void convert(int x[10][10],int b[10][3],int r,int c)
{
int i,j,k;
k=1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(x[i][j]!=0)
{
b[k][0]=i;
b[k][1]=j;
b[k][2]=x[i][j];
k++;
}
}
}
b[0][0]=r;
b[0][1]=c;
b[0][2]=k-1;
}
void add(int b1[10][3],int b2[10][3],int b3[10][3])
{
int i=1,j=1,k=1;

while(i<=b1[0][2] && j<=b2[0][2])


{
if(b1[i][0]<b2[j][0])
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
i++;
k++;
goto last;
}
if(b1[i][0]>b2[j][0])
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
j++;
k++;
goto last;
}
if(b1[i][1]<b2[j][1])
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
i++;
k++;
goto last;
}
if(b1[i][1]>b2[j][1])
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
j++;
k++;
goto last;
}

b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2]+b2[j][2];
i++;j++;k++;
last:;
}

while(i<=b1[0][2])
{

b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
i++;
k++;
}
while(j<=b2[0][2])
{

b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
j++;
k++;
}
b3[0][0]=b1[0][0];
b3[0][1]=b1[0][1];
b3[0][2]=k-1;

}
void transpose(int b[15][3],int b1[10][3])
{
int i,j,k,t;
k=1;
b1[0][0]=b[0][0];
b1[0][1]=b[0][1];
for(i=0;i<b[0][1];i++)
{
for(j=0;j<=b[0][2];j++)
{
if(i==b[j][1])
{
b1[k][0]=i;
b1[k][1]=b[j][0];
b1[k][2]=b[j][2];
k++;
}
}
}

b1[0][2]=k-1;
t=b1[0][2];

for(i=0;i<=t;i++)
{
printf("\n %d\t%d\t%d\n",b1[i][0],b1[i][1],b1[i][2]);
}
}

void ftranspose(int b[15][3],int b2[10][3])


{
int total[10],index[10],i,j,col=0,loc=0,t;
for(i=0;i<b[0][1];i++)
total[i]=0;

for(i=1;i<=b[0][2];i++)
{
col=b[i][1];
total[col]++;
}

index[0]=1;
for(i=1;i<b[0][2];i++)
{
index[i]=index[i-1]+total[i-1];
}

for(i=1;i<=b[0][2];i++)
{
col=b[i][1];
loc=index[col];
index[col]++;
b2[loc][0]=b[i][1];
b2[loc][1]=b[i][0];
b2[loc][2]=b[i][2];
loc++;

}
b2[0][2]=loc-1;
t=b2[0][2];
for(i=0;i<=t;i++)
{
printf("\n %d\t%d\t%d\n",b2[i][0],b2[i][1],b2[i][2]);
}
}

You might also like