Advertisement
eldieck

Untitled

Apr 6th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int * arrExpand(int*, int );
  6. int * arrShift(int*, int);
  7. void printArr(int*, int);
  8.  
  9. int main(){
  10.     int *firstArr, *secArr, *arr, arrSize;
  11.  
  12.     cout<<"What is the size of the array? ";
  13.     cin >> arrSize;
  14.  
  15.     for(int i = 0; i < arrSize; i++){
  16.         cout<<"Enter element #" << i+1<< ": ";
  17.         cin>>arr[i];
  18.     }
  19.  
  20.     firstArr = arrExpand(arr, arrSize);
  21.     secArr = arrShift(arr, arrSize);
  22.  
  23.     printArr(firstArr, (arrSize * 2));
  24.  
  25.     printArr(secArr, (arrSize + 1));
  26.  
  27.  
  28.  
  29.     delete [] arr;
  30. }
  31.  
  32. int * arrExpand(int *arr, int arrSize){
  33.  
  34.     int *newArr = new int [arrSize * 2];
  35.  
  36.     if(newArr == NULL)
  37.         cout<<"No more memory"<<endl;
  38.  
  39.     for(int i = 0; i < arrSize; i++){
  40.         newArr[i] = arr[i];
  41.     }
  42.  
  43.     for(int j = arrSize; j < (arrSize * 2); j++){
  44.         newArr[j] = 0;
  45.     }
  46.  
  47.     return newArr;
  48. }
  49.  
  50. int * arrShift(int *arr, int arrSize){
  51.  
  52.     int *newArr = new int [arrSize + 1];
  53.    
  54.     newArr[0] = 0;
  55.     for(int i = 1; i < (arrSize + 1); i++){
  56.         newArr[i] = arr[i-1];
  57.     }
  58.     return newArr;
  59. }
  60.  
  61. void printArr(int *arr, int size){
  62.     for(int i = 0; i < size; i++)
  63.         cout << arr[i] << " ";
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement