fork download
  1. /*******************************************************************************
  2.  * Aiden Stannard // CS1A // Chapter 9 Homework: Challange 12# // 11/21/2026
  3.  * _____________________________________________________________________________
  4.  *
  5.  * Element Shifter
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program accepts an array and creates a copy of the array with every
  9.  * value of the new array moved up by a single element, assigning the first
  10.  * element of the new array to zero.
  11.  * _____________________________________________________________________________
  12.  *
  13.  * Input: result = shiftArray(arr, 3)
  14.  *
  15.  * Output: The new order of the values of the array are as follows:
  16.  * 0 10 20 30
  17.  *
  18.  ******************************************************************************/
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. int* shiftArray(int arr[], int size) { // moves element positionn of the passed
  23. // array values up by one element
  24. int* newArr = new int[size + 1];
  25. newArr[0] = 0;
  26.  
  27. for (int i = 0; i < size; i++) {
  28. newArr[i + 1] = arr[i];
  29. }
  30.  
  31. return newArr;
  32. }
  33.  
  34. int main() {
  35. int size = 3;
  36. int arr[size] = {10, 20, 30}; // Original array order
  37. int* result; // Pointer that will take on the value of the returned pointer
  38. // from the shiftarray function.
  39.  
  40. result = shiftArray(arr, size); // passes array and size to the function
  41.  
  42. cout << "The new order of the values of the array are as follows: " << endl;
  43.  
  44. for (int i = 0; i < 4; i++) { // Outputs the new array value positions
  45. cout << result[i] << " ";
  46. }
  47.  
  48. delete[] result; // frees the "borrowed" memory
  49. return 0;
  50. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
The new order of the values of the array are as follows: 
0 10 20 30