fork download
  1. //Andrew Alspaugh CS1A Chapter 9. P. 539 #12
  2.  
  3. /*****************************************************************************
  4. Shift Elements in Array
  5. ______________________________________________________________________________
  6. The purpose of this program is to use the function shift elements to shift
  7. all of the elements from any given array
  8. ____________________________________________________________________________
  9. Input
  10. int size
  11. int *array[]
  12.  
  13. Output
  14. int *shifted[]
  15.  
  16. *****************************************************************************/
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. int *ShiftElements(int *array, int& size);
  21. int main()
  22. {
  23. //DATA DICTIONARY
  24. int size;
  25.  
  26. //INPUT
  27. cout << "Enter size of array:" << endl;
  28. cin >> size;
  29.  
  30. int *array = new int [size];
  31.  
  32. cout << "enter " << size << " elements:" << endl;
  33. for(int count = 0; count < size; count++)
  34. cin >> *(array + count);
  35.  
  36. //PROCESS
  37. int *shifted = ShiftElements(array, size);
  38.  
  39. //OUTPUT
  40. cout << "Updated Array is:" << endl;
  41. for(int count = 0; count < size; count++)
  42. {
  43. cout << *(shifted + count) << " ";
  44. }
  45. delete [] array;
  46. delete[] shifted;
  47.  
  48. return 0;
  49. }
  50.  
  51. int *ShiftElements(int *array, int& size)
  52. {
  53.  
  54. size += 1;
  55. int *shifted = new int [size];
  56.  
  57. *(shifted + 0) = 0;
  58.  
  59. for(int count = 0; count < size - 1; count++)
  60. *(shifted + (count + 1)) = *(array + count);
  61.  
  62. return shifted;
  63. }
Success #stdin #stdout 0.01s 5316KB
stdin
3
1
2
3
stdout
Enter size of array:
enter 3 elements:
Updated Array is:
0  1  2  3