4DM3M

Возведение квадратной матрицы в степень DEV

Dec 27th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void identity(int N)
  6. {
  7.     int i, j;
  8.     int a[10][10];
  9.     for (i = 0; i < N; i++)
  10.     {
  11.         for (j = 0; j < N; j++)
  12.         {
  13.             if (i == j) a[i][j] = 1;
  14.             else a[i][j] = 0;
  15.             cout << a[i][j] << " ";
  16.         }
  17.         cout << endl;
  18.     }
  19. }
  20.  
  21. void main()
  22. {
  23.     ifstream input;
  24.     ofstream report;
  25.     input.open("input.txt");
  26.     if (!input.is_open()) cout << "Ошибка при открытии файла!\t\n";
  27.     report.open("output.txt");
  28.  
  29.     setlocale(LC_ALL, "");
  30.     const int nn = 10;
  31.     int a[nn][nn], b[nn][nn], c[nn][nn];
  32.     int n, p, k, i, j, st;
  33.     cout << "Введите ранг матрицы: ";
  34.     //cin >> n;
  35.     n = 4; cout << "3\n";
  36.  
  37.     cout << "Введите матрицу:\n";
  38.     /*
  39.     for (i = 0; i < n; i++)
  40.         for ( j = 0; j < n; j++)
  41.             cin >> a[i][j];                 //Ввод матрицы A
  42.     cout << endl;
  43.     */
  44.     for (i=0; i<n; i++) { for (j = 0; j < n; j++) { input >> a[i][j]; cout << a[i][j] << " "; } cout << endl; } cout << endl;
  45.  
  46.     for (i = 0; i < n; i++)
  47.         for (j = 0; j < n; j++) b[i][j] = a[i][j];  //Создание матрицы B
  48.  
  49.     cout << "Введите степень: ";
  50.     //cin >> st;
  51.     cout << "3\n"; st = 3;
  52.  
  53.     switch (st)
  54.     {
  55.     case 0:
  56.         identity(n);
  57.         break;
  58.  
  59.     case 1:
  60.         for (i = 0; i < n; i++)
  61.         {
  62.             for (j = 0; j < n; j++) cout << a[i][j] << " ";
  63.             cout << endl;
  64.         }
  65.         break;
  66.  
  67.     default:
  68.         while (--st != 0)
  69.         {
  70.             for (i = 0; i < n; i++)
  71.                 for (j = 0; j < n; j++)
  72.                 {
  73.                     int s = 0;
  74.                     for (int k = 0; k < n; k++)
  75.                     {
  76.                         int ss = a[i][k] * b[k][j];
  77.                         report << "ss= " << a[i][k] << " * " << b[k][j] << endl;
  78.                         s += ss;
  79.                         report << "i=" << i + 1 << " j=" << j + 1 << " k=" << k + 1 << " сумма в строке=" << ss + 1 << endl; //report
  80.                     }
  81.                     c[i][j] = s;
  82.                     report << endl; //report
  83.                 }
  84.  
  85.             for (i = 0; i < n; i++)
  86.                 for (j = 0; j < n; j++) a[i][j] = c[i][j];
  87.             report << "\t\t\t\t\t\t\t\tST=" << st << endl;
  88.         }
  89.     }
  90.     for (i=0; i<n; i++)
  91.     {
  92.         for (j = 0; j < n; j++) cout << a[i][j] << " ";
  93.         cout << endl;
  94.     }
  95.     report.close();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment