Advertisement
Alexandre_lsv

Untitled

Dec 22nd, 2016
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. int N=4;
  3. using namespace std;
  4. double **  f1(ifstream &fin){
  5.   fin >> N;
  6.   cout << "\tИсходная матрица размерностью " << N << "x" << N << endl;
  7.   double ** matr = new double*[N];
  8.   for(int i=0; i<N; i++){
  9.     matr[i] = new double[N];
  10.     for(int j=0; j<N; j++){
  11.       fin >> matr[i][j];
  12.       cout << matr[i][j] << " \n"[j==N-1];
  13.     }
  14.   }
  15.   cout << "\n";
  16.   return matr;
  17. }
  18.  
  19. void f2(double ** &matr, ofstream &fout){
  20.   for(int step=1; step<=N; step++){
  21.     cout << "шаг " << step << ":\n";
  22.     fout << "шаг " << step << ":\n";
  23.     for(int i=step; i<N; i++){
  24.       double cnst=1.*matr[i][step-1]/1./matr[step-1][step-1];
  25.       for(int j=step-1; j<N; j++)
  26.         matr[i][j]-=matr[step-1][j]*cnst;
  27.     }
  28.     for(int i=0; i<N; i++)
  29.       for(int j=0; j<N; j++){
  30.         cout << matr[i][j] << " \n"[j==N-1];
  31.         fout << matr[i][j] << " \n"[j==N-1];
  32.       }
  33.       cout << "\n";
  34.       fout << "\n";
  35.   }
  36. }
  37.  
  38. double f3(double ** &matr){
  39.   double res=1;
  40.   for(int i=0; i<N; i++)
  41.     res*=matr[i][i];
  42.   return res;
  43. }
  44.  
  45. int main(){
  46.   ifstream fin("input.txt");
  47.   ofstream fout("matrgause.txt");
  48.   cout << "\tметод Гаусса\n";
  49.   double ** matr = f1(fin);
  50.   f2(matr, fout);
  51.   cout << "Определитель равен " << f3(matr) << "\n";
  52.   return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement