#include <iostream>
using namespace std;

int main()
{
	char more;
	int size=0;
	int *array = NULL;
	do
	{
		int pos, val, required_size;

		cout << "Enter a position: ";
		cin >> pos;
		cout << "Enter a value: ";
		cin >> val;

		required_size = pos +1;

		if (required_size > size)
		{
			/* Must resize the array */

			/* Step 1: Allocate a new array with sufficient size */
			int *new_array = new int[required_size];
			// Initialize all positions to "no value assigned"
			for (int i=0; i < required_size; i++)
				new_array[i] = -1; // -1 denotes "no value assigned"

			/* Step 2: Copy from old array to new array */
			for (int i=0; i < size; i++)
				new_array[i] = array[i];
			// Note on the first pass, "array" isn't initialized (it points to NULL)
			// However, the above for loop won't actually do anything when size==0
			// so we know there is no risk of accessing the unitialized array.

			/* Step 3: Free memory and update variables */
			delete array;
			array = new_array;
			size = required_size;
		}

		/* Do the assignment */
		array[pos] = val;

		/* Continue? */
		cout << "Do you want to make another assignment? ";
		cin >> more;
	} while (more == 'y' || more == 'Y');
        
	/* Print out the array */
	for (int i=0; i<size; i++)
	{
		cout << "array[" << i<< "] = ";
		if (array[i] == -1)
			cout << "[No value assigned]";
		else
			cout << array[i];
		cout << endl;
	}
}

