fork download
  1. // Hudson price CS1A
  2.  
  3. /*******************************************************************************
  4.  * Shift Elements in Array
  5.  *______________________________________________________________________________
  6.  *this program will duplicate the array nd shit it by one
  7.  * The function will return the pointer of the new array.
  8.  *______________________________________________________________________________
  9.  * INPUT
  10.  * the arrayof origional pointers
  11.  *
  12.  * OUTPUT
  13.  * poibnter with tthe shifted elements
  14.  *
  15.  ******************************************************************************/
  16. #include <iostream>
  17.  
  18. using namespace std;
  19.  
  20. int* expandArray(const int* arr, int size);
  21.  
  22. int main()
  23. {
  24.  
  25. int arr[] = {1, 2, 3, 4, 5};
  26. int size = sizeof(arr) / sizeof(arr[0]);
  27.  
  28. int* expandedArr = expandArray(arr, size);
  29.  
  30. cout << "Expanded array:\n";
  31. for (int i = 0; i < 2 * size; ++i)
  32. {
  33. cout << expandedArr[i] << " ";
  34. }
  35. cout << endl;
  36.  
  37.  
  38. delete[] expandedArr;
  39.  
  40. return 0;
  41. }
  42.  
  43. int* expandArray(const int* arr, int size)
  44. {
  45.  
  46. int* newArr = new int[2 * size];
  47.  
  48. for (int i = 0; i < size; ++i)
  49. {
  50. newArr[i] = arr[i];
  51. }
  52.  
  53. for (int i = size; i < 2 * size; ++i)
  54. {
  55. newArr[i] = 0;
  56. }
  57.  
  58. return newArr;
  59. }
  60.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Expanded array:
1 2 3 4 5 0 0 0 0 0