SlideShare a Scribd company logo
S2-2
Write a program in ‘C’ language to multiply two sparse matrices
#include<stdio.h>
#include<conio.h>
struct sp
{
int row,col;
int mat[10][10];
int sp[50][3];
};
/* to convert the entered matrix to sparce form,by eleminating zero values*/
int convsp(struct sp *M )
{
int e=1,i,j;
printf("enter number of rows and columns in the matrix");
scanf("%d%d",&M->row,&M->col);
printf("enter the matrix");
for(i=0;i<M->row;++i)
for(j=0;j<M->col;++j)
{
scanf("%d",&M->mat[i][j]);
if(M->mat[i][j]!=0)
{
M->sp[e][0]=i;
M->sp[e][1]=j;
M->sp[e][2]=M->mat[i][j];
e++;
}
}
M->sp[0][0]=M->row; //store number of rows to first(row,col) of sparse
M->sp[0][1]=M->col; //store number of cols to first row,second col
M->sp[0][2]=e-1; //store total number of non zero values to 3rd column
return M->sp[0][2]; //return total number of non zero elements
}
mult(struct sp M1,int e1, struct sp M2,int e2)
{
int sum[10][10],i,j,k1,k2;
for(i=0;i<10;++i)
for(j=0;j<10;++j)
sum[i][j]=0;
for(i=1;i<=e1;++i)
for(j=1;j<=e2;++j)
if(M1.sp[i][1]==M2.sp[j][0])
{
k1=M1.sp[i][0];
k2=M2.sp[j][1];
sum[k1][k2]+=M1.sp[i][2]*M2.sp[j][2];
}
printf("nproduct matrixn");
for(i=0;i<M1.row;++i)
{
for(j=0;j<M2.col;++j)
printf("%dt",sum[i][j]);
Page 1
S2-2
printf("n");
}
}
void printsp(int n,struct sp matx)
{
int i,j;
for(i=0;i<=n;++i)
{
for(j=0;j<3;++j)
printf("%dt",matx.sp[i][j]);
printf("n");
}
}
main()
{
int ele1,ele2;
struct sp m1,m2;
clrscr();
ele1=convsp(&m1);
printf("n SPARSE MATRIX1n");
printsp(ele1,m1);
ele2=convsp(&m2);
printf("n SPARSE MATRIX2n");
printsp(ele2,m2);
if(m1.row!=m2.col)
printf("matrices are not compatible for multiplication");
else
mult(m1,ele1,m2,ele2);
getch();
}
Page 2

More Related Content

What's hot (16)

PDF
Bcsl 033 data and file structures lab s3-1
Dr. Loganathan R
 
PDF
Bcsl 033 data and file structures lab s1-3
Dr. Loganathan R
 
PDF
Bcsl 033 data and file structures lab s3-2
Dr. Loganathan R
 
PDF
Bcsl 033 data and file structures lab s4-2
Dr. Loganathan R
 
PDF
C Programming Example
PRATHAMESH DESHPANDE
 
PPTX
C Programming Example
University of Potsdam
 
PPTX
Computer programing w
cexpertise
 
DOCX
1 (1)
Krish Na
 
PDF
Bcsl 033 data and file structures lab s1-4
Dr. Loganathan R
 
DOCX
Dti2143 lab sheet 9
alish sha
 
DOCX
Program for pyramid
nayakq
 
DOC
C program to add two numbers
mohdshanu
 
PDF
Insertion sort
Hitesh Kumar
 
Bcsl 033 data and file structures lab s3-1
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s1-3
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s3-2
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s4-2
Dr. Loganathan R
 
C Programming Example
PRATHAMESH DESHPANDE
 
C Programming Example
University of Potsdam
 
Computer programing w
cexpertise
 
1 (1)
Krish Na
 
Bcsl 033 data and file structures lab s1-4
Dr. Loganathan R
 
Dti2143 lab sheet 9
alish sha
 
Program for pyramid
nayakq
 
C program to add two numbers
mohdshanu
 
Insertion sort
Hitesh Kumar
 

Similar to Bcsl 033 data and file structures lab s2-2 (14)

PPTX
C Programming Language Part 8
Rumman Ansari
 
PDF
2D array
A. S. M. Shafi
 
PPTX
Cse 121 presentation on matrix [autosaved]
Kanis Fatema Shanta
 
PDF
Data Structure & Algorithms - Matrix Multiplication
babuk110
 
PPTX
sparse matrices in tress
pavialone
 
PPTX
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
LakshayBhardwaj39
 
PDF
C++ TUTORIAL 5
Farhan Ab Rahman
 
PDF
CP Handout#9
trupti1976
 
PPT
Array
Malainine Zaid
 
DOCX
Write a function called float dotproduct (links to an external site.)(float a...
JenniferBall48
 
PDF
2Darrays.pdf. data structure using c programming
maha280307
 
PDF
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Dr Sandeep Kumar Poonia
 
C Programming Language Part 8
Rumman Ansari
 
2D array
A. S. M. Shafi
 
Cse 121 presentation on matrix [autosaved]
Kanis Fatema Shanta
 
Data Structure & Algorithms - Matrix Multiplication
babuk110
 
sparse matrices in tress
pavialone
 
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
LakshayBhardwaj39
 
C++ TUTORIAL 5
Farhan Ab Rahman
 
CP Handout#9
trupti1976
 
Write a function called float dotproduct (links to an external site.)(float a...
JenniferBall48
 
2Darrays.pdf. data structure using c programming
maha280307
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Dr Sandeep Kumar Poonia
 
Ad

More from Dr. Loganathan R (17)

PDF
Ch 6 IoT Processing Topologies and Types.pdf
Dr. Loganathan R
 
PDF
IoT Sensing and Actuation.pdf
Dr. Loganathan R
 
PDF
Ch 4 Emergence of IoT.pdf
Dr. Loganathan R
 
PDF
Bcsl 033 data and file structures lab s4-3
Dr. Loganathan R
 
PDF
Bcsl 033 data and file structures lab s1-2
Dr. Loganathan R
 
PPT
Introduction to Information Security
Dr. Loganathan R
 
PDF
Mcs 012 computer organisation and assemly language programming- ignou assignm...
Dr. Loganathan R
 
PDF
Session 9 4 alp to display the current system time using dos int 21 h
Dr. Loganathan R
 
PDF
Session 9 1 alp to compute a grade using procedures
Dr. Loganathan R
 
PDF
IGNOU Assembly Language Programming
Dr. Loganathan R
 
PDF
Requirement engineering process
Dr. Loganathan R
 
PDF
MCS 012 computer organisation and assembly language programming assignment…
Dr. Loganathan R
 
PDF
Software requirements
Dr. Loganathan R
 
PDF
Software process
Dr. Loganathan R
 
PDF
Software engineering critical systems
Dr. Loganathan R
 
PDF
Software engineering socio-technical systems
Dr. Loganathan R
 
PDF
Software engineering introduction
Dr. Loganathan R
 
Ch 6 IoT Processing Topologies and Types.pdf
Dr. Loganathan R
 
IoT Sensing and Actuation.pdf
Dr. Loganathan R
 
Ch 4 Emergence of IoT.pdf
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s4-3
Dr. Loganathan R
 
Bcsl 033 data and file structures lab s1-2
Dr. Loganathan R
 
Introduction to Information Security
Dr. Loganathan R
 
Mcs 012 computer organisation and assemly language programming- ignou assignm...
Dr. Loganathan R
 
Session 9 4 alp to display the current system time using dos int 21 h
Dr. Loganathan R
 
Session 9 1 alp to compute a grade using procedures
Dr. Loganathan R
 
IGNOU Assembly Language Programming
Dr. Loganathan R
 
Requirement engineering process
Dr. Loganathan R
 
MCS 012 computer organisation and assembly language programming assignment…
Dr. Loganathan R
 
Software requirements
Dr. Loganathan R
 
Software process
Dr. Loganathan R
 
Software engineering critical systems
Dr. Loganathan R
 
Software engineering socio-technical systems
Dr. Loganathan R
 
Software engineering introduction
Dr. Loganathan R
 
Ad

Recently uploaded (20)

PPTX
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
DOCX
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PDF
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 

Bcsl 033 data and file structures lab s2-2

  • 1. S2-2 Write a program in ‘C’ language to multiply two sparse matrices #include<stdio.h> #include<conio.h> struct sp { int row,col; int mat[10][10]; int sp[50][3]; }; /* to convert the entered matrix to sparce form,by eleminating zero values*/ int convsp(struct sp *M ) { int e=1,i,j; printf("enter number of rows and columns in the matrix"); scanf("%d%d",&M->row,&M->col); printf("enter the matrix"); for(i=0;i<M->row;++i) for(j=0;j<M->col;++j) { scanf("%d",&M->mat[i][j]); if(M->mat[i][j]!=0) { M->sp[e][0]=i; M->sp[e][1]=j; M->sp[e][2]=M->mat[i][j]; e++; } } M->sp[0][0]=M->row; //store number of rows to first(row,col) of sparse M->sp[0][1]=M->col; //store number of cols to first row,second col M->sp[0][2]=e-1; //store total number of non zero values to 3rd column return M->sp[0][2]; //return total number of non zero elements } mult(struct sp M1,int e1, struct sp M2,int e2) { int sum[10][10],i,j,k1,k2; for(i=0;i<10;++i) for(j=0;j<10;++j) sum[i][j]=0; for(i=1;i<=e1;++i) for(j=1;j<=e2;++j) if(M1.sp[i][1]==M2.sp[j][0]) { k1=M1.sp[i][0]; k2=M2.sp[j][1]; sum[k1][k2]+=M1.sp[i][2]*M2.sp[j][2]; } printf("nproduct matrixn"); for(i=0;i<M1.row;++i) { for(j=0;j<M2.col;++j) printf("%dt",sum[i][j]); Page 1
  • 2. S2-2 printf("n"); } } void printsp(int n,struct sp matx) { int i,j; for(i=0;i<=n;++i) { for(j=0;j<3;++j) printf("%dt",matx.sp[i][j]); printf("n"); } } main() { int ele1,ele2; struct sp m1,m2; clrscr(); ele1=convsp(&m1); printf("n SPARSE MATRIX1n"); printsp(ele1,m1); ele2=convsp(&m2); printf("n SPARSE MATRIX2n"); printsp(ele2,m2); if(m1.row!=m2.col) printf("matrices are not compatible for multiplication"); else mult(m1,ele1,m2,ele2); getch(); } Page 2