Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <array>
- using namespace std;
- int main(){
- int x;
- int size = 2;
- int *listDyn;
- listDyn = new int[size];
- int count = 0;
- do{
- cout << "Please enter a value to store in the array, or enter -1 to finish: ";
- cin >> x;
- if (x == -1){ //user input check for finish
- break;
- }
- if (count == size){
- int *newDyn; //initializing temporary dynamic array
- newDyn = new int[size];
- for (int i = 0; i < size; i++){ //allocating initial array values to temporary array
- listDyn[i] = newDyn[i];
- }
- delete listDyn; // deleting inital dynamic array
- size = size * 2; // doubling size of array
- listDyn = new int[size]; //initializing "new" initial array
- for (int i = 0; i < size; i++){
- newDyn[i] = listDyn[i]; //allocating temporary array values to "new" initial array
- }
- delete newDyn; // deleting temporary dynamic array
- }else{
- listDyn[count] = x; //user input for elements of array
- count++;
- }
- } while (true);
- cout << "The elements of the finished array are: " << endl; //printing elements of array
- for (int i = 0; i < count; i++){
- cout << listDyn[i] << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement