Advertisement
Mestima

Right Circular Shift С++

Nov 11th, 2021 (edited)
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. /*
  2.     <3
  3.     UPD:
  4.         Not the best solution.
  5. */
  6.  
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12.     int size;
  13.     cout << "Enter array size: ";
  14.     cin >> size;
  15.     int *arr = new int[size];
  16.     cout << "Enter " << size << " elements with a space: ";
  17.     for (int i = 0; i < size; i++)
  18.         cin >> arr[i];
  19.     int n;
  20.     cout << "Enter n by which the array will be shifted: ";
  21.     cin >> n;
  22.    
  23.     cout << "Old array: ";
  24.     for (int i = 0; i < size; i++)
  25.         cout << arr[i] << " ";
  26.  
  27.     for (int i = 0; i < n; i++) {
  28.         int *buffArr = new int[size];
  29.         for (int j = 1; j < size; j++) {
  30.             buffArr[j] = arr[j-1];
  31.         }
  32.         buffArr[0] = arr[size-1];
  33.  
  34.         for (int j = 0; j < size; j++) {
  35.             arr[j] = buffArr[j];
  36.         }
  37.         delete[] buffArr;
  38.     }  
  39.  
  40.     cout << endl << "New array: ";
  41.     for (int i = 0; i < size; i++)
  42.         cout << arr[i] << " ";
  43.        
  44.     delete[] arr;
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement