Tuesday, 15 March 2016

Write a code fragment that asks the user to enter a positive integer and then creates a dynamic array of that many ints. Do this by using new, then again using a vector object.

Leave a Comment

Question:

Write a code fragment that asks the user to enter a positive integer and then creates a dynamic array of that many ints. Do this by using new, then again using a vector object.

Answer:

This assumes that the iostream and vector header files have been included and
that there is a using directive:
unsigned int size;
cout << "Enter a positive integer: ";
cin >> size;
int * dyn = new int [size];
vector<int> dv(size);

Source:

C++ Primer Plus Sixth Edition Chapter Review

If You Enjoyed This, Take 5 Seconds To Share It