Charlie is an avid collector of double numbers. He likes numbers and goes on field trips collecting them. However, Charlie does not want to have more than one specimen of the same number in his collection. Yet, he is somewhat absent minded and occasionally tries to add a number to his collection even though he already has it. Your program should keep track of Charlie's collection and make sure there are no duplicates.
Use check() to check if the number is already present in the array. If yes, no further actions are required. If the number is not present, it needs to be added. The array cannot be enlarged to accommodate the number. Instead, this function needs to allocate a new array whose size is one more than the size of the old array, copy the contents of the old array to the new one, include the new number, deallocate the old array and then assign the address of the new array back to the original pointer.
Note that since the pointer is passed by reference, the call to this function results in effectively "enlarging" the array pointed to by the pointer.
Again, use check() to check if the number is present in the array. If the number is not present, no further actions are required. If the number is present, then the function should allocate a new array of smaller size and then copy all the numbers to it, except the one that needs to be erased. After copying is done, deallocate the old array and update the array pointer to point to the new array.
Copying and removing one element may be done in a single for-loop. Here is a pseudocode for the solution:
create a boolean variable "found", set it to false iterate over elements in the old array, if old array element equals to the number to be removed then set found to true else if not found then copy old array element to the new array element else // found copy old array element to the new array element with index one less than old array's (move them to the left)
Hint: double *p=new double[0]; works correctly: returns a pointer to zero-size array (array with no elements). This allows to avoid treating the array of size zero in the above functions as a special case.
Charlie may enter the number twice, in which case, the program should inform that it is a duplicate and the duplicate number should not be entered. Charlie may request to remove the number that is not in the collection, in which case, the program should inform the user and remove nothing.
Here is an example program dialog (Charlie's input is in bold):
Charlie, enter operation [a/r/q] and number: a 5.0 your numbers: 5, sum: 5 Charlie, enter operation [a/r/q] and number: a 8.0 your numbers: 5 8, sum: 13 Charlie, enter operation [a/r/q] and number: a 8.0 duplicate! your numbers: 5 8, sum: 13 Charlie, enter operation [a/r/q] and number: r 8.0 your numbers: 5, sum: 5 Charlie, enter operation [a/r/q] and number: r 10.0 not present! your numbers: 5, sum: 5 Charlie, enter operation [a/r/q] and number: a 3.3 your numbers: 5 3.3, sum: 8.3 Charlie, enter operation [a/r/q] and number: a 12.2 your numbers: 5 3.3 12.2, sum: 20.5 Charlie, enter operation [a/r/q] and number: q
The size of the user input can be arbitrarily large. For this program, to accommodate user input, you need to implement an array of doubles whose size varies as necessary. The numbers in the array should not be sorted.
Milestone: implement functions output() and value().
Make sure your programs adhere to proper programming style. Submit your projects to the subversion repository. Do not forget to verify your submission on the web.