Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- #include<conio.h>
- class sparse
- {
- int a[10][3],b[10][3];
- public:void add(void);
- };
- void sparse::add(void)
- {
- int i,j,n1,n2;
- cout<<"\n enter the no of rows of the first matrix:";
- cin>>n1;
- cout<<"\n enter the elements of the first matrix:"<<endl;
- for(i=0;i<n1;i++)
- {
- for(j=0;j<3;j++)
- cin>>a[i][j];
- }
- cout<<"\enter the no of rows of the second matrix:";
- cin>>n2;
- cout<< "\n enter the elements of the second matrix:";
- for(i=0;i<n2;i++)
- {
- for(j=0;j<3;j++)
- {
- cin>>b[i][j];
- } }
- cout<<"\n the resultanat first matrix:"<<endl;
- for(i=0;i<n1;i++)
- {
- cout<<"\n";
- for (j=0;j<3;j++)
- cout<<a[i][j]<<"\t";
- }
- cout<<"\n the resultant second matrix:"<<endl;
- for(i=0;i<n2;i++)
- {
- cout<<"\n";
- for(j=0;j<3;j++)
- {
- cout<<endl;
- cout<<b[i][j]<<"\t";
- }
- }
- i=0;
- j=0;
- while((i<n1)&&(j<n2))
- {
- if(a[1][0]==b[j][0]&&a[i][1]==b[j][1])
- {
- cout<<endl;
- cout<<a[i][0]<<"\t"<<a[i][1]<<"\t"<<a[i][2]+b[i][2];
- i++;
- j++;
- }
- else if(a[i][0]==b[j][0])
- {
- if(a[i][1]<b[j][1])
- {
- cout<<endl;
- cout<<a[i][0]<<"\t"<<a[i][1]<<"\t"<<a[1][2];
- i++;
- }
- else
- {
- cout<<endl;
- cout<<b[j][0]<<"\t"<<b[j][1]<<"\t"<<b[j][2];
- j++;
- }
- }
- else if(a[i][0]<b[j][0])
- {
- cout<<endl;
- cout<<a[i][0]<<"\t"<<a[i][1]<<"\t"<<a[i][2];
- i++;
- }
- else
- {
- cout<<endl;
- cout<<b[j][0]<<"\t"<<b[j][1]<<"\t"<<b[j][2];
- j++;
- }
- }
- if (i>=n1)
- {
- while(j<n2)
- { cout<<endl;
- cout<<b[j][0]<<b[j][1]<<b[j][2];
- j++;
- }
- }
- else
- {
- for(i=0;i<n1;i++)
- {
- cout<<endl;
- cout<<a[i][0]<<a[i][1]<<a[i][2];
- }
- }
- }
- int main()
- {
- clrscr();
- sparse obj;
- obj.add();
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement