Advertisement
Guest User

Untitled

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