35657

Untitled

Jun 12th, 2023
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. // добавление столбца по указанному индексу
  10. void AddColumn(int** arr, int lines, int& cols, int index) {
  11.     for (int i = 0; i < lines; i++) {
  12.         int* temp = new int[cols + 1];
  13.         for (int j = 0; j < index; j++) {
  14.             temp[j] = arr[i][j];
  15.         }
  16.         temp[index] = 0;
  17.         for (int j = index; j < cols; j++) {
  18.             temp[j + 1] = arr[i][j];
  19.         }
  20.         delete[] arr[i];
  21.         arr[i] = temp;
  22.     }
  23.     cols++;
  24. }
  25.  
  26. // удаление столбца по указанному индексу
  27. void DelColumn(int** arr, int lines, int& cols, int index) {
  28.     for (int i = 0; i < lines; i++) {
  29.         int* temp = new int[cols - 1];
  30.         for (int j = 0; j < index; j++) {
  31.             temp[j] = arr[i][j];
  32.         }
  33.         for (int j = index + 1; j < cols; j++) {
  34.             temp[j - 1] = arr[i][j];
  35.         }
  36.         delete[] arr[i];
  37.         arr[i] = temp;
  38.     }
  39.     cols--;
  40. }
  41.  
  42.  
  43. // замена столбцов по указанным индексам
  44. void ChangeColumn(int** arr, int lines, int cols, int index1, int index2) {
  45.     for (int i = 0; i < lines; i++) {
  46.         int temp = arr[i][index1];
  47.         arr[i][index1] = arr[i][index2];
  48.         arr[i][index2] = temp;
  49.     }
  50. }
  51.  
  52.  
  53. // транспонирование матрицы - строки становятся столбцами, а столбцы строками
  54. void TransposeArray(int**& arr, int& lines, int& cols) {
  55.     int** temp = new int* [cols];
  56.     for (int i = 0; i < cols; i++) {
  57.         temp[i] = new int[lines];
  58.         for (int j = 0; j < lines; j++) {
  59.             temp[i][j] = arr[j][i];
  60.         }
  61.     }
  62.     for (int i = 0; i < lines; i++) {
  63.         delete[] arr[i];
  64.     }
  65.     delete[] arr;
  66.     arr = temp;
  67.     int buf = lines;
  68.     lines = cols;
  69.     cols = buf;
  70. }
  71.  
  72.  
  73.  
  74. void Print(int** arr, int lines, int cols) {
  75.     for (int i = 0; i < lines; i++) {
  76.         for (int j = 0; j < cols; j++) {
  77.             cout << setw(4) << arr[i][j];
  78.         }
  79.         cout << endl;
  80.     }
  81.     cout << endl;
  82. }
  83.  
  84. int main() {
  85.     int lines = 5;
  86.     int cols = 5;
  87.  
  88.     int** arr = new int*[lines];
  89.     for (int i = 0; i < lines; i++) {
  90.         arr[i] = new int[cols];
  91.     }
  92.  
  93.     for (int i = 0; i < lines; i++) {
  94.         for (int j = 0; j < cols; j++) {
  95.             arr[i][j] = j + i * cols;
  96.         }
  97.     }
  98.  
  99.     Print(arr, lines, cols);
  100.  
  101.     AddColumn(arr, lines, cols, 3);
  102.  
  103.     Print(arr, lines, cols);
  104.  
  105.     DelColumn(arr, lines, cols, 3);
  106.  
  107.     Print(arr, lines, cols);
  108.  
  109.     ChangeColumn(arr, lines, cols, 2, 4);
  110.  
  111.     Print(arr, lines, cols);
  112.  
  113.     TransposeArray(arr, lines, cols);
  114.  
  115.     Print(arr, lines, cols);
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment