Advertisement
Ansaid

Транспонирование квадратной матрицы

May 16th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <time.h>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     setlocale(LC_ALL, "Russian");
  12.     srand(time(NULL));
  13.     int SIZE;
  14.     cout << "Введите размерность матрицы: "; // ввод размерности
  15.     cin >> SIZE;
  16.  
  17.     vector <vector <double>> arr(SIZE, vector <double>(SIZE)); // создает двумерный вектор размера SIZE
  18.  
  19.     for (int i = 0; i < SIZE; i++) // присванивание значений матрице
  20.     {
  21.         for (int j = 0; j < SIZE; j++)
  22.         {
  23.             arr[i][j] = rand() % 10;
  24.         }
  25.     }
  26.  
  27.     for (int i = 0; i < SIZE; i++) // вывод матрицы
  28.     {
  29.         for (int j = 0; j < SIZE; j++)
  30.         {
  31.             cout << arr[i][j] << "\t";
  32.         }
  33.         cout << endl;
  34.     }
  35.  
  36.     for (int i = 0; i < SIZE - 1; i++) // транспонирование
  37.     {
  38.         for (int j = i + 1; j < SIZE; j++)
  39.         {
  40.             swap(arr[i][j], arr[j][i]);
  41.         }
  42.         cout << endl;
  43.     }
  44.  
  45.     for (int i = 0; i < SIZE; i++) // вывод транспонированной матрицы
  46.     {
  47.         for (int j = 0; j < SIZE; j++)
  48.         {
  49.             cout << arr[i][j] << "\t";
  50.         }
  51.         cout << endl;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement