fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. inline void OutputMatrix(vector< vector<double> > matrix, int nx, int ny)
  6. {
  7. for(int i = 0; i < nx; ++i){
  8. for(int j = 0; j < ny; ++j){
  9. cout << matrix[i][j] << (j == ny-1 ? "" : " ");
  10. }
  11. cout << endl;
  12. }
  13. }
  14.  
  15.  
  16.  
  17. int main(){
  18.  
  19. int n=4;
  20. vector<vector<double>> A={
  21. {9,2,0,0,2},
  22. {-2,4,-1,0,0},
  23. {0,-1,5,-2,1},
  24. {0,0,-2,6,9}
  25. };
  26.  
  27.  
  28. for(int k = 0; k < n-1; ++k){
  29. double akk = A[k][k];
  30. for(int i = k+1; i < n; ++i){
  31. double aik = A[i][k];
  32. for(int j = k; j < n+1; ++j){ // 確認のため左下要素が0になるようにj=kとしたが,実際にはj=k+1でよい
  33. A[i][j] = A[i][j]-aik*(A[k][j]/akk);
  34. }
  35. }
  36. cout << "k = " << k << endl;
  37. OutputMatrix(A, n, n+1);
  38. cout << endl;
  39. }
  40.  
  41. // 後退代入(back substitution)
  42. // - x_nの解はb_n/a_nn,x_nをさらにn-1行の式に代入することでx_(n-1)を求める.
  43. // - この作業を1行目まで続けることですべての解を得る.
  44. A[n-1][n] = A[n-1][n]/A[n-1][n-1];
  45. for(int i = n-2; i >= 0; --i){
  46. double ax = 0.0;
  47. for(int j = i+1; j < n; ++j){
  48. ax += A[i][j]*A[j][n];
  49. }
  50. A[i][n] = (A[i][n]-ax)/A[i][i];
  51.  
  52. cout << "i = " << i << endl;
  53. OutputMatrix(A, n, n+1);
  54. cout << endl;
  55. }
  56.  
  57. return 0;
  58. }
  59.  
  60.  
  61.  
  62.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:26: error: variable or field ‘OutputMatrix’ declared void
 inline void OutputMatrix(vector< vector<double> > matrix, int nx, int ny)
                          ^~~~~~
prog.cpp:5:26: error: ‘vector’ was not declared in this scope
prog.cpp:5:26: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
prog.cpp:2:1:
+#include <vector>
 using namespace std;
prog.cpp:5:26:
 inline void OutputMatrix(vector< vector<double> > matrix, int nx, int ny)
                          ^~~~~~
prog.cpp:5:34: error: ‘vector’ was not declared in this scope
 inline void OutputMatrix(vector< vector<double> > matrix, int nx, int ny)
                                  ^~~~~~
prog.cpp:5:34: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
prog.cpp:5:41: error: expected primary-expression before ‘double’
 inline void OutputMatrix(vector< vector<double> > matrix, int nx, int ny)
                                         ^~~~~~
prog.cpp:5:59: error: expected primary-expression before ‘int’
 inline void OutputMatrix(vector< vector<double> > matrix, int nx, int ny)
                                                           ^~~
prog.cpp:5:67: error: expected primary-expression before ‘int’
 inline void OutputMatrix(vector< vector<double> > matrix, int nx, int ny)
                                                                   ^~~
prog.cpp: In function ‘int main()’:
prog.cpp:20:5: error: ‘vector’ was not declared in this scope
     vector<vector<double>> A={
     ^~~~~~
prog.cpp:20:5: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
prog.cpp:20:19: error: expected primary-expression before ‘double’
     vector<vector<double>> A={
                   ^~~~~~
prog.cpp:29:16: error: ‘A’ was not declared in this scope
   double akk = A[k][k];
                ^
prog.cpp:37:3: error: ‘OutputMatrix’ was not declared in this scope
   OutputMatrix(A, n, n+1);
   ^~~~~~~~~~~~
prog.cpp:44:2: error: ‘A’ was not declared in this scope
  A[n-1][n] = A[n-1][n]/A[n-1][n-1];
  ^
prog.cpp:53:3: error: ‘OutputMatrix’ was not declared in this scope
   OutputMatrix(A, n, n+1);
   ^~~~~~~~~~~~
stdout
Standard output is empty