0% found this document useful (0 votes)
33 views11 pages

Sparse Code&Output

The document describes a C program to implement sparse matrix operations like transpose and addition. It includes functions to input a sparse matrix, transpose it, and add two sparse matrices. The main function uses a menu to allow the user to choose these operations and output the results.

Uploaded by

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

Sparse Code&Output

The document describes a C program to implement sparse matrix operations like transpose and addition. It includes functions to input a sparse matrix, transpose it, and add two sparse matrices. The main function uses a menu to allow the user to choose these operations and output the results.

Uploaded by

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

Shino Shamit

12th October 2020 CSE Gamma


RET19CS196

Data Structures Lab


Day 2
2. Write a menu driven C program to implement
sparse matrices using arrays and perform the
following operations
a. Sparse Matrix Addition
b. Sparse Matrix Transpose

Program Code
#include<stdio.h>
#include<stdlib.h>
int sparse(int a[10][10],int r, int c)
{
int count=0,i,j,k,sp[10][10];
printf("Enter the Elements of intial matrix\n");
k=1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]!=0)
{
sp[k][0]=i;
sp[k][1]=j;
sp[k][2]=a[i][j];
k++;
count++;
}
}
}
sp[0][0]=r;
sp[0][1]=c;
sp[0][2]=count;
printf("Sparsed Matrix\n");
for(i=0;i<count+1;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",sp[i][j]);
}
printf("\n");
}
return count;
}

void trans_sparse(int count,int sp[10][10],int c,int r)


{
int k,trans[10][10],i,j;
k=1;
trans[0][0]=c;
trans[0][1]=r;
trans[0][2]=count;
for(i=0;i<c;i++)
for(j=1;j<count+1;j++)
if(sp[j][1]==i)
{
trans[k][0]=sp[j][1];
trans[k][1]=sp[j][0];
trans[k][2]=sp[j][2];
k++;
}
printf("Transposed Sparse Matrix\n");
for(i=0;i<count+1;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",trans[i][j]);
}
printf("\n");
}
}

void add_sparse(int a[10][10],int b[10][10],int add[10][10],int


m,int n, int r1, int c1,int r2, int c2)
{
int i,j,k;
if((a[0][0]!=b[0][0]) && (a[0][1] != b[0][1]))
{
printf("Addition not possible\n");
exit(0);
}
else
{
add[0][0]=r1;
add[0][1]=c1;
k=1;
i=1;
j=1;
while(i<=m && j<=n)
{
if(a[i][0]<b[j][0])
{
add[k][0]=a[i][0];
add[k][1]=a[i][1];
add[k][2]=a[i][2];
k++;
i++;
}
else if(a[i][0]>b[j][0])
{
add[k][0]=b[j][0];
add[k][1]=b[j][1];
add[k][2]=b[j][2];
k++;
j++;
}
else if(a[i][1]<b[j][1])
{
add[k][0]=b[j][0];
add[k][1]=b[j][1];
add[k][2]=b[j][2];
k++;
j++;
}
else
{
add[k][0]=a[i][0];
add[k][1]=a[i][1];
add[k][2]=a[i][2]+b[i][2];
k++;
i++;
j++;
}
}
while(i<=m)
{
add[k][0]=a[i][0];
add[k][1]=a[i][1];
add[k][2]=a[i][2];
k++;
i++;
}
while(j<=n)
{
add[k][0]=b[j][0];
add[k][1]=b[j][1];
add[k][2]=b[j][2];
k++;
j++;
}
}
add[0][2]=k-1;

printf("The Added Matrix\n");


for(i=0;i<k;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",add[i][j]);
}
printf("\n");
}
}

void main()
{
Int
a[10][10],b[10][10],sum[10][10],trans[10][10],option,m,n,r1,c1,r
2,c2;
printf("\t\tMAIN MENU\n\n");
printf("Enter your option\n");
printf("1.Transpose of Sparse Matrix\n");
printf("2.Addition of Sparse Matrices\n");
printf("3.Exit the Program\n");
scanf("%d",&option);
switch(option)
{
case 1:printf("Sparse Matrix Transpose\n");
printf("Enter the rows and column of matrix\n");
scanf("%d%d",&r1,&c1);
m=sparse(a,r1,c1);
trans_sparse(m,a,c1,r1);
break;

case 2:printf("Sparse Matrix Addition\n");


printf("Enter the rows and columns of 1st matrix\n");
scanf("%d%d",&r1,&c1);
m=sparse(a,r1,c1);

printf("Enter the rows and columns of 2nd


matrix\n");
scanf("%d%d",&r2,&c2);
n=sparse(b,r2,c2);

add_sparse(a,b,sum,m,n,r1,c1,r2,c2);
break;

case 3:printf("Thank you for your co-operation\n");


exit(0);
break;

default:printf("Exit\n");
break;
}
}
(My code wasn’t working properly, and I couldn’t find and didn’t get time to fix
them)

Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy