Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "conio.h"
  4. using namespace std;
  5.  
  6. // Matrix Class
  7. class matrix
  8. {
  9. public:
  10. matrix(int n, string shape);
  11. ~matrix();
  12. void transpose();
  13. void print();
  14.  
  15. private:
  16. double *mymatrix;
  17. string matrixshape;
  18. int matrixsize;
  19. int newlinenumber;
  20. };
  21.  
  22. matrix::matrix(int n, string shape = "square")
  23. {
  24. matrixshape = shape;
  25.  
  26. if(!matrixshape.compare("square"))
  27. {
  28. matrixsize = n * n;
  29. newlinenumber = n;
  30. }
  31. else
  32. {
  33. matrixsize = n * 2;
  34. newlinenumber = 2;
  35. }
  36.  
  37. mymatrix = new (nothrow) double[matrixsize];
  38. if (mymatrix == nullptr)
  39. cout << "Memory not Allocated";
  40. else
  41. {
  42. for (int i = 0; i < matrixsize; i++)
  43. {
  44. mymatrix[i] = i;
  45. }
  46. }
  47. }
  48.  
  49. matrix::~matrix()
  50. {
  51. }
  52.  
  53. void matrix::print()
  54. {
  55. int tempcounter = 0;
  56.  
  57. for (int i = 0; i < matrixsize; i++)
  58. {
  59. cout << mymatrix[i] << '\t';
  60. tempcounter ++;
  61. if(tempcounter == newlinenumber)
  62. {
  63. tempcounter = 0;
  64. cout << endl;
  65. }
  66. }
  67. }
  68.  
  69. void matrix::transpose()
  70. {
  71. for(int i = 0; i < newlinenumber; ++i)
  72. for(int j = i+1; j < newlinenumber; ++j)
  73. swap(mymatrix[newlinenumber*i + j], mymatrix[newlinenumber*j + i]);
  74.  
  75. if(matrixshape.compare("square"))
  76. newlinenumber = matrixsize / 2;
  77. }
  78.  
  79.  
  80.  
  81. // Main Program
  82. void main()
  83. {
  84. // Test Rectangle Matrix
  85. matrix m1 = matrix(5,"rect");
  86. cout << "First Matrix : " << endl;
  87. m1.print();
  88. cout << endl << "Transposed Matrix : " << endl;
  89. m1.transpose();
  90. m1.print();
  91.  
  92. // Test square Matrix
  93. cout << endl;
  94. matrix m2 = matrix(4,"square");
  95. cout << "First Matrix : " << endl;
  96. m2.print();
  97. cout << endl << "Transposed Matrix : " << endl;
  98. m2.transpose();
  99. m2.print();
  100.  
  101. // stay in console
  102. int stay;
  103. cin >> stay;
  104. return;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement