Advertisement
1k6j01

Возведение в степень n матрицы m x m

Oct 15th, 2015
1,477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5. void create(int **a, int m)
  6. {
  7.     for (int i(0); i < m; i++)
  8.     {
  9.         for (int j(0); j < m; j++)
  10.         {
  11.             std::cout << "a[" << i << "]["<<j<<"] = ";
  12.             std::cin >> a[i][j];
  13.         }
  14.     }
  15. }
  16.  
  17. void show(int **a, int m)
  18. {
  19.     for (int i(0); i < m; i++)
  20.     {
  21.         for (int j(0); j < m; j++)
  22.         {
  23.             std::cout << a[i][j] << " ";
  24.         }
  25.         std::cout << std::endl;
  26.     }
  27. }
  28.  
  29. int **matrix(int **a, int m, int n)
  30. {
  31.     int **c = new int *[m];
  32.     for (int i(0); i < m; i++)
  33.     {
  34.         c[i] = new int[m];
  35.     }
  36.     for (int i(0); i < m; i++)
  37.     {
  38.         for (int j(0); j < m; j++)
  39.         {
  40.             c[i][j] = 0;
  41.         }
  42.     }
  43.     for (int k(1); k < n + 1; k++)
  44.     {
  45.         for (int i(0); i < m; i++)
  46.         {
  47.             for (int j(0); j < m; j++)
  48.             {
  49.                 for (int h(0); h < m; h++)
  50.                 {
  51.                     c[i][j] += a[i][h] * a[h][j];
  52.                 }
  53.             }
  54.         }
  55.     }
  56.     return c;
  57. }
  58.  
  59. int main(int argc, char* argv[])
  60. {
  61.     setlocale(0, "");
  62.     std::cout << "Введите размер матрицы: ";
  63.     int m;
  64.     std::cin >> m;
  65.     std::cout << "Введите степень матрицы: ";
  66.     int n;
  67.     std::cin >> n;
  68.     int **a = new int *[m];
  69.     for (int i(0); i < m; i++)
  70.     {
  71.         a[i] = new int[m];
  72.     }
  73.     create(a, m);
  74.     show(a, m);
  75.     std::cout << std::endl;
  76.  
  77.     int **с = new int *[m];
  78.     for (int i(0); i < m; i++)
  79.     {
  80.         с[i] = new int[m];
  81.     }
  82.  
  83.     с = matrix(a, m, n);
  84.     show(с, m);
  85.  
  86.     _getch();
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement