Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main(){
  9.     int x;
  10.     int size = 2;          
  11.     int *listDyn;
  12.     listDyn = new int[size];       
  13.     int count = 0;
  14.  
  15.  
  16.     do{
  17.        
  18.         cout << "Please enter a value to store in the array, or enter -1 to finish: ";
  19.         cin >> x;
  20.        
  21.         if (x == -1){  //user input check for finish
  22.             break;
  23.         }
  24.         if (count == size){
  25.            
  26.             int *newDyn;                //initializing temporary dynamic array
  27.             newDyn = new int[size];
  28.            
  29.             for (int i = 0; i < size; i++){  //allocating initial array values to temporary array
  30.                 listDyn[i] = newDyn[i];
  31.                
  32.             }
  33.             delete listDyn;                 // deleting inital dynamic array
  34.             size = size * 2;                // doubling size of array
  35.             listDyn = new int[size];        //initializing "new" initial array
  36.             for (int i = 0; i < size; i++){
  37.                 newDyn[i] = listDyn[i];     //allocating temporary array values to "new" initial array
  38.  
  39.             }
  40.             delete newDyn;                  // deleting temporary dynamic array
  41.            
  42.  
  43.  
  44.         }else{
  45.             listDyn[count] = x;     //user input for elements of array
  46.             count++;
  47.            
  48.         }
  49.  
  50.     } while (true);
  51.    
  52.     cout << "The elements of the finished array are: " << endl;         //printing elements of array
  53.     for (int i = 0; i < count; i++){
  54.         cout << listDyn[i] << endl;
  55.     }
  56.  
  57.     system("pause");
  58.     return 0;  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement