fork download
  1. //Cecilia Rangel CSC5 Chapter 9, P.539, #12
  2. //
  3. /**************************************************************
  4.  *
  5.  * SHIFT ELEMENT ARRAY
  6.  * ____________________________________________________________
  7.  * This program creates an array of intergers, then creates a
  8.  * shifted array of elements thats shifted one space to the
  9.  * right changing the first number to 0.
  10.  *
  11.  *_____________________________________________________________
  12.  *
  13.  * OUTPUT
  14.  * Array : OUTPUT a array shifted once
  15.  * to the right
  16.  * ***************************************************************/
  17. #include <iostream>
  18. #include <iomanip>
  19. using namespace std;
  20.  
  21. //Prototype functions
  22. int *duplicateArray(const int*, int);
  23. void displayArray(int[], int);
  24.  
  25. int main()
  26. {
  27. const int SIZE = 5;
  28.  
  29. int array[SIZE] = {1, 2, 3, 4, 5};
  30. int *duplicate;
  31. duplicate = duplicateArray(array, SIZE);
  32.  
  33. cout << "Orginal Array: " << endl;
  34. displayArray(array, SIZE);
  35.  
  36. cout << endl << "Duplicate Array: " << endl;
  37. displayArray(duplicate, SIZE);
  38.  
  39. delete[] duplicate;
  40. duplicate = 0;
  41.  
  42. return 0;
  43. }
  44. /****************************************************************************
  45.  * duplicateArray
  46.  * __________________________________________________________________________
  47.  * This function takes the orginal array and then shifts the elements the left
  48.  * and changes element[0] to 0.
  49.  * **************************************************************************/
  50. int *duplicateArray(const int *array, int SIZE)
  51. {
  52. int *newArray;
  53. if(SIZE <= 0)
  54. {
  55. return NULL;
  56. }
  57. newArray = new int[SIZE + 1];
  58. newArray[0] = 0;
  59.  
  60. for (int i = 0; (i + 1) < SIZE; i++)
  61. {
  62. newArray[i + 1] = array[i];
  63. }
  64. return newArray;
  65. }
  66. /****************************************************************************
  67.  * displayArray
  68.  * _________________________________________________________________________
  69.  * A function that will be used in main to display the orginal array and the
  70.  * new duplicate array.
  71.  * *************************************************************************/
  72. void displayArray(int array[], int SIZE)
  73. {
  74. for (int i = 0; i < SIZE; i++)
  75. {
  76. cout << array[i];
  77. }
  78. }
Success #stdin #stdout 0s 5344KB
stdin
Standard input is empty
stdout
Orginal Array: 
12345
Duplicate Array: 
01234