Advertisement
Hydrase

Sparse Add

Aug 6th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. class sparse
  4. {
  5. int a[10][3],b[10][3];
  6. public:void add(void);
  7. };
  8. void sparse::add(void)
  9. {
  10. int i,j,n1,n2;
  11. cout<<"\n enter the no of rows of the first matrix:";
  12. cin>>n1;
  13. cout<<"\n enter the elements of the first matrix:"<<endl;
  14. for(i=0;i<n1;i++)
  15. {
  16. for(j=0;j<3;j++)
  17. cin>>a[i][j];
  18. }
  19. cout<<"\enter the no of rows of the second matrix:";
  20. cin>>n2;
  21. cout<< "\n enter the elements of the second matrix:";
  22. for(i=0;i<n2;i++)
  23. {
  24. for(j=0;j<3;j++)
  25. {
  26. cin>>b[i][j];
  27. } }
  28. cout<<"\n the resultanat first matrix:"<<endl;
  29. for(i=0;i<n1;i++)
  30. {
  31. cout<<"\n";
  32. for (j=0;j<3;j++)
  33. cout<<a[i][j]<<"\t";
  34. }
  35. cout<<"\n the resultant second matrix:"<<endl;
  36. for(i=0;i<n2;i++)
  37. {
  38. cout<<"\n";
  39. for(j=0;j<3;j++)
  40. {
  41. cout<<endl;
  42. cout<<b[i][j]<<"\t";
  43. }
  44. }
  45. i=0;
  46. j=0;
  47.  
  48. while((i<n1)&&(j<n2))
  49. {
  50. if(a[1][0]==b[j][0]&&a[i][1]==b[j][1])
  51. {
  52. cout<<endl;
  53. cout<<a[i][0]<<"\t"<<a[i][1]<<"\t"<<a[i][2]+b[i][2];
  54. i++;
  55. j++;
  56. }
  57. else if(a[i][0]==b[j][0])
  58. {
  59. if(a[i][1]<b[j][1])
  60. {
  61. cout<<endl;
  62. cout<<a[i][0]<<"\t"<<a[i][1]<<"\t"<<a[1][2];
  63. i++;
  64. }
  65. else
  66. {
  67. cout<<endl;
  68. cout<<b[j][0]<<"\t"<<b[j][1]<<"\t"<<b[j][2];
  69. j++;
  70. }
  71. }
  72. else if(a[i][0]<b[j][0])
  73. {
  74. cout<<endl;
  75. cout<<a[i][0]<<"\t"<<a[i][1]<<"\t"<<a[i][2];
  76. i++;
  77. }
  78. else
  79. {
  80. cout<<endl;
  81. cout<<b[j][0]<<"\t"<<b[j][1]<<"\t"<<b[j][2];
  82. j++;
  83. }
  84.  
  85.  
  86. }
  87. if (i>=n1)
  88. {
  89. while(j<n2)
  90. { cout<<endl;
  91. cout<<b[j][0]<<b[j][1]<<b[j][2];
  92. j++;
  93. }
  94. }
  95. else
  96. {
  97. for(i=0;i<n1;i++)
  98. {
  99. cout<<endl;
  100. cout<<a[i][0]<<a[i][1]<<a[i][2];
  101. }
  102. }
  103. }
  104. int main()
  105. {
  106. clrscr();
  107. sparse obj;
  108. obj.add();
  109. getch();
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement