Advertisement
35657

Untitled

Jan 27th, 2024
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void Fill(int** arr, int lines, int cols) {
  6.     for (int i = 0; i < lines; i++) {
  7.         for (int j = 0; j < cols; j++) {
  8.             arr[i][j] = rand() % 100;
  9.         }
  10.     }
  11. }
  12.  
  13. void Print(int** arr, int lines, int cols) {
  14.     for (int i = 0; i < lines; i++) {
  15.         for (int j = 0; j < cols; j++) {
  16.             cout << arr[i][j] << '\t';
  17.         }
  18.         cout << endl;
  19.     }
  20.     cout << endl;
  21. }
  22.  
  23.  
  24. void Rotate(int**& arr, int& lines, int& cols) {
  25.     int** temp = new int* [cols];
  26.     for (int i = 0; i < cols; i++) {
  27.         temp[i] = new int[lines];
  28.         for (int j = 0; j < lines; j++) {
  29.             temp[i][j] = arr[lines - j - 1][i];
  30.         }
  31.     }
  32.     for (int i = 0; i < lines; i++) {
  33.         delete[] arr[i];
  34.     }
  35.     delete[] arr;
  36.     arr = temp;
  37.     int buf = lines;
  38.     lines = cols;
  39.     cols = buf;
  40. }
  41.  
  42. int main() {
  43.  
  44.     srand(time(NULL));
  45.  
  46.     int lines = 3, cols = 4;
  47.  
  48.     int** arr = new int* [lines];
  49.  
  50.     for (int i = 0; i < lines; i++) {
  51.         arr[i] = new int[cols];
  52.     }
  53.  
  54.     Fill(arr, lines, cols);
  55.  
  56.     Print(arr, lines, cols);
  57.  
  58.     Rotate(arr, lines, cols);
  59.  
  60.     Print(arr, lines, cols);
  61.  
  62.     for (int i = 0; i < lines; i++) {
  63.         delete[] arr[i];
  64.     }
  65.     delete[] arr;
  66.  
  67. }
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement