Question: program will enter data into a dynamically allocated single dimension array program will remove all but the first occurrence of a specific value from the
program will enter data into a dynamically allocated single dimension array
program will remove all but the first occurrence of a specific value from the array and store in second array
program will display the results
All array manipulation will be by pointers only; NO Subscripts permitted*/
void input_data(short *data, short size);
void display_data(short *data, short size);
void removeSV(short *data1, short size1, short *data2, short &size2, short searchValue);
#include
using namespace std;
int main()
{
short *data1, size1, // pointer to the first data set and size
*data2, size2, // pointer to the second data set and size
searchValue, // contains value to be removed from data
loop_again; // variable to control inner loop
cout<<"Program will remove all but the first occurrence of a specific value ";
cout<<"enter the number of values to store in the array or zero to terminate the program ";
cin>>size1;
while(size1) // loop to permit user to test many data sets
{
data1 = new(nothrow) [size1]; // dynamic allocation of memory
// test pointer to verify memory was allocated
if(!data1) { cout<<"Memory allocation error, program will terminate "; system("pause"); exit(0); }
data2 = new(nothrow) short[size1];
// test pointer to verify memory was allocated
if(!data2) { cout<<"Memory allocation error, program will terminate "; system("pause"); exit(0); }
input_data(data1, size1);
// print the contents of the array
cout<<" there are "< display_data(data1, size1); loop_again = 1; while(loop_again) { cout<<"enter a specific value to remove from the array: "; cin>>searchValue; removeSV(data1, size1, data2, size2, searchValue); // *********** function you are to write ******** cout<<" there are "< display_data(data1, size1); if(size2 < size1) { cout<<" there are "< display_data(data2, size2); } else cout<<"no values were removed from the data array "; memcpy(data1, data2, size2*sizeof(short)); // copy data2 to data1 size1 = size2; cout<<"To remove another value from the data array enter 1 else enter 0 to stop removing values: "; cin>>loop_again; } delete [] data1; delete [] data2; cout<<"enter the number of values to store in the array or zero to terminate the program "; cin>>size1; } // pause the program to see the results system("pause"); return 0; } void input_data(short *data, short size) // I/O function converted to pointers { short *i=data, *end = data+size; cout<<"enter "< while(i < end) { //cout<<"enter value "< cin>>*i++; } } void display_data(short *data, short size) { short *end = data+size; while(data < end) { // display the numbers in the array separated by 1 blank all on the same line cout<<*data++<<' '; } cout< } Write a function, removeSV, to copy all values from data1 to data2 except for duplicate copies of a specific value passed to the function. The first occurrence of the search value would be copied to the second array but any additional occurrences of the search value would not be copied to the second array. All values in the second array will be in the same order as the first array. Use the prototype before main to complete the function definition for this program. Be sure that the argument size2 contains the number of values in the second array before the function terminates. All loops must be terminated by using comparison of pointers. The input and output functions for this program have been converted to pointer access for the dynamically allocated arrays. Use the I/O functions as an example to write the removeSV function. Test your program first by entering data from the keyboard. Once you are sure it is working properly, generate random numbers in the input_data function to store in the array and enter a size up to 1,000 to test your program further. Look up the function rand to generate the pseudo random numbers.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
