fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cout << " Enter the number of variables : ";
  7. cin >>n;
  8. double v[10][10];
  9. double id[10][10];
  10. for(int i=0;i<10;i++)
  11. {
  12. for(int j=0;j<10;j++)
  13. {
  14. if(i==j)
  15. id[i][j]=1;
  16. else
  17. id[i][j]=0;
  18. }
  19. }
  20. cout <<"Enter matrix a\n";
  21. for(int i=0;i<n;i++)
  22. {
  23. for(int j=0;j<n;j++)
  24. cin >> v[i][j];
  25. }
  26. cout <<"Enter matrix b\n";
  27. for(int i=0;i<n;i++)
  28. {
  29. cin >> v[i][n];
  30. }
  31. double c;
  32.  
  33.  
  34.  
  35. int k=0;
  36. while(k<n)
  37. {
  38.  
  39. if(v[k][k]!=1)
  40. {
  41.  
  42. c=v[k][k];
  43. if(c==0) c=1;
  44. for(int j=0;j<=n;j++)
  45. {
  46. v[k][j]/=c;
  47. }
  48. }
  49.  
  50.  
  51. cout << "Matrix a is :\n";
  52. for(int i=0;i<n;i++)
  53. {
  54. for(int j=0;j<=n;j++)
  55. cout << v[i][j]<< " ";
  56. cout <<"\n";
  57. }
  58.  
  59. for(int i=0;i<n;i++)
  60. {
  61.  
  62. c=v[i][k]/v[k][k];
  63. if(i!=k)
  64. {
  65. for(int j=0;j<=n;j++)
  66. {
  67. v[i][j]-=c*v[k][j];
  68. }
  69. }
  70.  
  71.  
  72. }
  73. k++;
  74.  
  75.  
  76.  
  77. cout << "Matrix a is :\n";
  78. for(int i=0;i<n;i++)
  79. {
  80. for(int j=0;j<=n;j++)
  81. cout << v[i][j]<< " ";
  82. cout <<"\n";
  83. }
  84. }
  85.  
  86. return 0;
  87. }
Success #stdin #stdout 0.01s 5548KB
stdin
3
1 1 1 
2 -3 4
3 4 5
9 13 40
stdout
 Enter the number of variables :  Enter matrix a
Enter matrix b
Matrix a is :
1 1 1 9 
2 -3 4 13 
3 4 5 40 
Matrix a is :
1 1 1 9 
0 -5 2 -5 
0 1 2 13 
Matrix a is :
1 1 1 9 
-0 1 -0.4 1 
0 1 2 13 
Matrix a is :
1 0 1.4 8 
-0 1 -0.4 1 
0 0 2.4 12 
Matrix a is :
1 0 1.4 8 
-0 1 -0.4 1 
0 0 1 5 
Matrix a is :
1 0 0 1 
0 1 0 3 
0 0 1 5