fork download
  1. // Torrez, Elaine CS1A Chapter 9, P. 539, #12
  2.  
  3. /********************************************************************************************
  4.  *
  5.  * ELEMENT SHIFTER
  6.  *
  7.  * ------------------------------------------------------------------------------------------
  8.  * This program demonstrates a function that accepts an integer pointer and its size and
  9.  * returns a NEW dynamically allocated array that is one element larger. The original array’s
  10.  * elements are shifted right by one position, and a 0 is stored at the first index.
  11.  *
  12.  * ------------------------------------------------------------------------------------------
  13.  * INPUT
  14.  * size : Number of integers
  15.  * arr : Original integers entered by the user
  16.  *
  17.  * OUTPUT
  18.  * New array with 0 at index 0 and all original elements shifted to the right
  19.  *
  20.  ********************************************************************************************/
  21.  
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. // FUNCTION PROTOTYPE
  26. int *shiftArray(int *arr, int size);
  27.  
  28. int main()
  29. {
  30. int size;
  31. int *arr = nullptr;
  32. int *shifted = nullptr;
  33.  
  34. // INPUT
  35. cout << "Enter number of integers: ";
  36. cin >> size;
  37.  
  38. while (size <= 0)
  39. {
  40. cout << "Error: Enter a number greater than 0: ";
  41. cin >> size;
  42. }
  43.  
  44. arr = new int[size];
  45.  
  46. for (int i = 0; i < size; i++)
  47. {
  48. cout << "Enter value #" << (i + 1) << ": ";
  49. cin >> arr[i];
  50. }
  51.  
  52. // Shift the array
  53. shifted = shiftArray(arr, size);
  54.  
  55. // OUTPUT
  56. cout << "\nShifted Array (size = " << size + 1 << "):\n";
  57. for (int i = 0; i < size + 1; i++)
  58. {
  59. cout << "Element [" << i << "] = " << shifted[i] << endl;
  60. }
  61.  
  62. delete [] arr;
  63. delete [] shifted;
  64. arr = nullptr;
  65. shifted = nullptr;
  66.  
  67. return 0;
  68. }
  69.  
  70. //*******************************************************************************
  71. // shiftArray
  72. //------------------------------------------------------------------------------
  73. // Creates a new array one element larger.
  74. // Stores 0 in the first element and shifts the rest of the values right by one.
  75. //*******************************************************************************
  76. int *shiftArray(int *arr, int size)
  77. {
  78. int *newArr = new int[size + 1];
  79.  
  80. newArr[0] = 0; // Insert 0 at beginning
  81.  
  82. for (int i = 0; i < size; i++)
  83. newArr[i + 1] = arr[i]; // Shift values
  84.  
  85. return newArr;
  86. }
  87.  
Success #stdin #stdout 0s 5320KB
stdin
 5
3
6
9
12
15
stdout
Enter number of integers: Enter value #1: Enter value #2: Enter value #3: Enter value #4: Enter value #5: 
Shifted Array (size = 6):
Element [0] = 0
Element [1] = 3
Element [2] = 6
Element [3] = 9
Element [4] = 12
Element [5] = 15