fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. const int ARRAY_SIZE = 7;
  6.  
  7.  
  8. int check_shift_position(int array_size, int m);
  9. void shift_array(int array[ARRAY_SIZE], int m);
  10. void print_array (int array[ARRAY_SIZE]);
  11.  
  12. int main() {
  13.  
  14. int array[ARRAY_SIZE];
  15.  
  16. for(int i =0; i < ARRAY_SIZE; i++) {
  17. cin >> array[i];
  18. }
  19.  
  20. int m = 0;
  21. cin >> m;
  22.  
  23. m = check_shift_position(ARRAY_SIZE,m);
  24.  
  25. cout << "Initial array = ";
  26. print_array(array);
  27.  
  28. shift_array(array, m);
  29.  
  30.  
  31. return 0;
  32. }
  33.  
  34. int check_shift_position(int array_size, int m)
  35. {
  36. if (array_size < m) {
  37. return m%array_size;
  38. }
  39. return m;
  40. }
  41.  
  42. void shift_array(int array[ARRAY_SIZE], int m) {
  43.  
  44. // Init shifted array
  45. int shifted_array[ARRAY_SIZE];
  46.  
  47. // Shift elements for [m + 1..n] to start;
  48. for (int i = m, k = 0; i < ARRAY_SIZE ; i++,k++) {
  49. shifted_array[k] = array[i];
  50. }
  51.  
  52. // Shift elements for [0..m] to the end;
  53. for (int i = 0, k = ARRAY_SIZE - m; i < m ; i++,k++) {
  54. shifted_array[k]= array[i];
  55. }
  56.  
  57. cout << "Shifted array = ";
  58. print_array(shifted_array);
  59. }
  60.  
  61. void print_array (int array[ARRAY_SIZE]) {
  62. for (int i = 0; i < ARRAY_SIZE ; i++) {
  63. cout << array[i] << " ";
  64. }
  65. cout << endl;
  66. }
Success #stdin #stdout 0s 3344KB
stdin
1 2 3 4 5 6 7
5
stdout
Initial array = 1 2 3 4 5 6 7 
Shifted array = 6 7 1 2 3 4 5