C++ For the assignment, we will write a program that uses a function called dele
ID: 3878521 • Letter: C
Question
C++ For the assignment, we will write a program that uses a function called deleteRepeats that has an array of characters as a formal parameter, and that deletes all of the corresponding repeated letters from the array. The number of array positions used will be found in a constant aptly named SIZE, and is not being passed as a parameter. The function will return a unique pointer.
Have the driver (main) program call the function deleteRepeats using the above test array and values. Call the function and use the returned (smart) pointer to display the new array and also display the difference (how many repeats).
Create a function called deleteRepeats: this function removes corresponding repeats from the original array that is passed as a parameter (pointer to a c-string), and eventually creates a dynamic array of only the non-repeating corresponding characters. It returns the pointer to that array.
Print out the values of the dynamic array and the total number of repeats. If you want to put a separate function together for this, you may encounter issues passing the smart pointer from the driver to the function. One way of passing the smart pointer as an argument is to use the get method. Get will produce a raw pointer and so actually creates a temporary copy, that is, moves the smart pointer to a local. Why is this? B/C 2 instances of a unique pointer cannot manage the same (memory).
Note hereon when we refer to the “driver” program, we are simply talking about good old main().
TECHNICAL NOTES:
Returns a (unique) pointer to a heap array that this program takes ownership of.
Program acquires ownership of a uniquely-owned array with dynamic lifetime from the deleteRepeats function.
The use of std::unique_ptr for this program is mainly of intellectual interest.
Include this test case as part of your driver code:
char originalArray[SIZE];
originalArray [0] = 'a';
originalArray [1] = 'b';
originalArray [2] = 'b';
originalArray [3] = 'c';
originalArray [4] = 'a';
originalArray [5] = 'c';
originalArray [6] = 'a';
originalArray [7] = 'c';
originalArray [8] = 'b';
originalArray [9] = 'a';
noRepeats = deleteRepeats(originalArray);
After this function is executed, the index values would be that the value at [0] is now 'b', the value at [1] is 'c', the value at [2] is 'a', the value at [3] is 'c', the value at [4] is 'a', and the value at [5] is 'c',. (The value of [8-9] and [0-1] are no longer included in the new array since they were corresponding repeats). Also, note that noRepeats is a unique pointer.
Explanation / Answer
char * deleteRepeats( char* ptr_to_array) {
// deletes the duplicates from the array and create a new dynamic
// arrray and returns the pointer to it
int no_repeats_size =0; // holds the size of the norepeats array
for (int i =0 ; i <SIZE ;i++) {
char c = ptr_to_array[i];
int flag =0;
for(j=0;j<i;j++) {
if ( ptr_to_array[j] == c)
flag =1;
}
if(flag ==0) {
no_repeats_size++;
}
}
char *after_deltion_array = new char[no_repeats_size];
int id =0;
for(int i=0;i<SIZE;i++) {
char c = ptr_to_array[i];
int flag =0;
for(int j=0;j<i;j++) {
if( c == ptr_to_array[j]) {
flag =1;
}
}
if( flag ==0) {
after_deletion_array[id] = c;
id++;
}
}
return after_deletiom_array;
}
hope it helps if any queries please do comment.
thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.