Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. // function to reduce matrix to reduced
  6. // row echelon form.
  7. int PerformOperation(float **a, int n)
  8. {
  9. int i, j, k = 0, c, flag = 0, m = 0;
  10. float pro = 0;
  11.  
  12. // Performing elementary operations
  13. for (i = 0; i < n; i++) {
  14. if (a[i][i] == 0) {
  15. c = 1;
  16. while (a[i + c][i] == 0 && (i + c) < n)
  17. c++;
  18. if ((i + c) == n) {
  19. flag = 1;
  20. break;
  21. }
  22. for (j = i, k = 0; k <= n; k++)
  23. swap(a[j][k], a[j+c][k]);
  24. }
  25.  
  26. for (j = 0; j < n; j++) {
  27.  
  28. // Excluding all i == j
  29. if (i != j) {
  30.  
  31. // Converting Matrix to reduced row
  32. // echelon form(diagonal matrix)
  33. float pro = a[j][i] / a[i][i];
  34.  
  35. for (k = 0; k <= n; k++)
  36. a[j][k] = a[j][k] - (a[i][k]) * pro;
  37. }
  38. }
  39. }
  40. return flag;
  41. }
  42.  
  43. // Function to print the desired result
  44. // if unique solutions exists, otherwise
  45. // prints no solution or infinite solutions
  46. // depending upon the input given.
  47. void PrintResult(float **a, int n, int flag) {
  48. cout << "Result is : "<< endl;
  49.  
  50. if (flag == 2)
  51. cout << "Infinite Solutions Exists" << endl;
  52. else if (flag == 3)
  53. cout << "No Solution Exists" << endl;
  54.  
  55.  
  56. // Printing the solution by dividing constants by
  57. // their respective diagonal elements
  58. else {
  59. for (int i = 0; i < n; i++) {
  60. a[i][n]=a[i][n]/ a[i][i];
  61. a[i][i]=a[i][i]/ a[i][i];
  62.  
  63. for (int j = 0; j < n+1; j++)
  64. cout << a[i][j] << " ";
  65. cout << endl;
  66. }
  67. //for (int i = 0; i < n; i++)
  68. //cout << a[i][n] / a[i][i] << " ";
  69. }
  70. }
  71.  
  72. // To check whether infinite solutions
  73. // exists or no solution exists
  74. int CheckConsistency(float **a, int n, int flag) {
  75. int i, j;
  76. float sum;
  77.  
  78. // flag == 2 for infinite solution
  79. // flag == 3 for No solution
  80. flag = 3;
  81. for (i = 0; i < n; i++) {
  82. sum = 0;
  83. for (j = 0; j < n; j++)
  84. sum = sum + a[i][j];
  85. if (sum == a[i][j])
  86. flag = 2;
  87. }
  88. return flag;
  89. }
  90.  
  91. // Driver code
  92. int main() {
  93.  
  94. float **a;
  95. int m, n, flag = 0;
  96. cout << "矩陣大小為m*n"<<"\nm:";
  97. cin>>m;
  98. cout<<"n:";
  99. cin>>n;
  100. cout <<"矩陣:\n";
  101.  
  102. a = new float*[m];
  103. for(int i = 0; i < m; i++)
  104. a[i] = new float[n];
  105.  
  106. for (int i=0; i<m; i++) {
  107. for (int j=0; j<n+1; j++) {
  108. cin >> a[i][j];
  109. }
  110. }
  111.  
  112. // Performing Matrix transformation
  113. flag = PerformOperation(a, n);
  114.  
  115. if (flag == 1)
  116. flag = CheckConsistency(a, n, flag);
  117.  
  118. // Printing Solutions(if exist)
  119. PrintResult(a, n, flag);
  120.  
  121. for(int i = 0; i < m; i++)
  122. delete [] a[i];
  123. delete [] a;
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement