Advertisement
BrightOS

Вариант 8

Jul 8th, 2020
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int n, i, j;
  8.  
  9. // Вывод двумерного массива
  10. void output_an_array(int **array) {
  11.     for (i = 0; i < n; i++) {
  12.         for (j = 0; j < n; j++)
  13.             cout << array[i][j] << " ";
  14.         cout << endl;
  15.     }
  16.     cout << endl;
  17. }
  18.  
  19. // Копирование данных из массива "a" в массив "b"
  20. void copy_array(int **a, int **b) {
  21.     for (i = 0; i < n; i++)
  22.         for (j = 0; j < n; j++)
  23.             b[i][j] = a[i][j];
  24. }
  25.  
  26. // Изменение столбцов по порядку со строками в обратном порядке
  27. int **columns_to_rows(int **a) {
  28.     int **sub = new int *[n];
  29.     for (i = 0; i < n; i++) {
  30.         sub[i] = new int[n];
  31.     }
  32.     copy_array(a, sub);
  33.     for (i = 0; i < n; i++)
  34.         for (j = 0; j < n; j++)
  35.             sub[j][i] = a[n - i - 1][j];
  36.  
  37.     return sub;
  38. }
  39.  
  40. int main(int argc, char *argv[]) {
  41.     srand(time(0));
  42.  
  43.     cout << "Enter the size of the array" << endl;
  44.     cin >> n;
  45.     cout << endl;
  46.  
  47.     int **m = new int *[n];
  48.     int **mul = new int *[n];
  49.     int *v = new int[n];
  50.     for (i = 0; i < n; i++) {
  51.         m[i] = new int[n];
  52.         mul[i] = new int[n];
  53.     }
  54.  
  55.     for (i = 0; i < n; i++)
  56.         for (j = 0; j < n; j++)
  57.             m[i][j] = rand() % 90 + 10;
  58.  
  59.     cout << "Output of the initial array:" << endl;
  60.     output_an_array(m);
  61.  
  62.     // Задание "а"
  63.     for (i = 0; i < n; i++)
  64.         v[i] = m[i][i] * m[i][i];
  65.  
  66.     cout << "Output of the \"Vk\" array:" << endl;
  67.     for (i = 0; i < n; i++)
  68.         cout << v[i] << " ";
  69.     cout << endl;
  70.  
  71.     // Задание "б"
  72.     cout << endl << "Array where columns are replaced with rows in reverse order:" << endl;
  73.     output_an_array(columns_to_rows(m));
  74.  
  75.     // Задание "в"
  76.     cout << "M*V:" << endl;
  77.  
  78.     for (i = 0; i < n; i++)
  79.         for (j = 0; j < n; j++)
  80.             mul[i][j] = m[i][j] * v[j];
  81.  
  82.     output_an_array(mul);
  83.     system("pause");
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement