Add to Favorites    Make Home Page 1850 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home » Add and subtract two sparse matrices

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Add and subtract two sparse matrices
Author Arun Vishnu
Author Email arun_vishnu [at] hotmail.com
Description To add and subtract 2 sparse matrices. Give two matrices in its simple form.
Category C++ » Data Structures
Hits 369086
Code Select and Copy the Code
#include<stdio.h> #include<iostream.h> #include<conio.h> int main() { clrscr(); int sparse1[10][3],sparse2[10][3],sum[10][3],diff[10][3]; int m,n,p,q,t1,t2,s,d,element; int i,j; cout<<"Enter the number of rows and columns : "; cin>>m>>n; t1=t2=0; cout<<" Enter the first matrix("<<m<<"*"<<n<<"): "; for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { cin>>element; if(element!=0) { t1=t1+1; sparse1[t1][1]=i; sparse1[t1][2]=j; sparse1[t1][3]=element; } } } sparse1[0][1]=m; sparse1[0][2]=n; sparse1[0][3]=t1; cout<<" Enter the second matrix("<<m<<"*"<<n<<"): "; for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { cin>>element; if(element!=0) { t2=t2+1; sparse2[t2][1]=i; sparse2[t2][2]=j; sparse2[t2][3]=element; } } } sparse2[0][1]=m; sparse2[0][2]=n; sparse2[0][3]=t2; // displaying the first sparse matrix cout<<" The first sparse matrix is : Row Column Element"; cout<<" ----------------------- "; for(i=0;i<=t1;i++) { cout<<sparse1[i][1]<<" "<<sparse1[i][2]<<" "<<sparse1[i][3]<<" "; } // displaying the second sparse matrix cout<<" The second sparse matrix is : Row Column Element"; cout<<" ----------------------- "; for(i=0;i<=t2;i++) { cout<<sparse2[i][1]<<" "<<sparse2[i][2]<<" "<<sparse2[i][3]<<" "; } // Addition and subtraction i=j=s=d=1; while((i<=t1)&&(j<=t2)) { if(sparse1[i][1]==sparse2[j][1]) // if rows are equal { if(sparse1[i][2]==sparse2[j][2]) // if columns are equal { sum[s][1]=diff[d][1]=sparse1[i][1]; sum[s][2]=diff[d][2]=sparse1[i][2]; sum[s][3]=sparse1[i][3]+sparse2[j][3]; diff[d][3]=sparse1[i][3]-sparse2[j][3]; i++; j++; if(sum[s][3]!=0) s++; if(diff[d][3]!=0) d++; } else // if columns are not equal { if(sparse1[i][2]<sparse2[j][2]) { sum[s][1]=diff[d][1]=sparse1[i][1]; sum[s][2]=diff[d][2]=sparse1[i][2]; sum[s][3]=diff[d][3]=sparse1[i][3]; i++; s++; d++; } else { sum[s][1]=diff[d][1]=sparse2[j][1]; sum[s][2]=diff[d][2]=sparse2[j][2]; sum[s][3]=sparse2[j][3]; diff[d][3]=0-sparse2[j][3]; j++; d++; s++; } } } else // if rows are not equal { if(sparse1[i][1]<sparse2[j][1]) { sum[s][1]=diff[d][1]=sparse1[i][1]; sum[s][2]=diff[d][2]=sparse1[i][2]; sum[s][3]=diff[d][3]=sparse1[i][3]; i++; d++; s++; } else { sum[s][1]=diff[d][1]=sparse2[j][1]; sum[s][2]=diff[d][2]=sparse2[j][2]; sum[s][3]=sparse2[j][3]; diff[d][3]=0-sparse2[j][3]; j++; s++; d++; } } } // end of while if(i<=t1) { for(p=i;p<=t1;p++) { sum[s][1]=diff[d][1]=sparse1[p][1]; sum[s][2]=diff[d][2]=sparse1[p][2]; sum[s][3]=diff[d][3]=sparse1[p][3]; s++; d++; } } else if(j<=t2) { for(p=j;p<=t2;p++) { sum[s][1]=diff[d][1]=sparse2[p][1]; sum[s][2]=diff[d][2]=sparse2[p][2]; sum[s][3]=sparse2[p][3]; diff[d][3]=0-sparse2[j][3]; s++; d++; } } // end of addition and subtraction sum[0][1]=diff[0][1]=m; sum[0][2]=diff[0][2]=n; sum[0][3]=s-1; diff[0][3]=d-1; // displaying the sum matrix cout<<" The sum is : Row Column Element"; cout<<" ----------------------- "; for(i=0;i<s;i++) { cout<<sum[i][1]<<" "<<sum[i][2]<<" "<<sum[i][3]<<" "; } // displaying the difference matrix cout<<" The difference is : Row Column Element"; cout<<" ----------------------- "; for(i=0;i<d;i++) { cout<<diff[i][1]<<" "<<diff[i][2]<<" "<<diff[i][3]<<" "; } getch(); return 0; }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=833


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review