Write a program that reads an arbitrary number of integers from user, until he/she choose to stop the input. The numbers should be stored in a vector1 which is dynamically allocated (ie.
a pointer to a vector space). The program shall read and handle each number in a loop, and then allocate new space for it, by increasing the vector one element at a time. (Pay attention to the Tips below!) Take use of the following simplified algorithm:initialize amount to 0 set a pointer to an integer vector to nullptr. read number as long as number is not a stop-value
reserve memory for a temporary vector, which is an element larger than the old one
copy all data from the old to the new vector – no special cases are needed for the first value
release memory space that the old pointer was pointing to
assign pointer (vector), the address value for the new vector save the number in the new larger vector
increase amount read number end
print the numbers deallocate the vector and reset it all to 0's
//code
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int size;
int* newVector,newSize = size+1;
int temp;
int* mVector = NULL;
int currentSize = 0;
bool expand(int* &vector, int& size)
{
try
{
newVector = new int[newSize];
}
catch(std::bad_alloc)
{
return false;
}
int i;
for (i = 0; i<size; i++)
newVector[i] = vector[i];