Question: Define a family of functions (a templated function) named insertAtEnd that inserts in a dynamically-allocated array of elements of any type another element at the
Define a family of functions (a templated function) named insertAtEnd that inserts in a dynamically-allocated array of elements of any type another element at the end (resize the array). The function should receive as parameters:
- the dynamically-allocated array
- the size of the array
- the element to insert
The function should return the resulted array.
Specialize the function for the type char. In this specialization the array must be null-terminated; the size parameter doesnt count the null-byte.
The client code listed below uses your templated function, and should not contain memory leaks. The comments next to each statement shows the content the array should have after the statement is executed.
Identify in English the operations/functions that a type must support if that type uses your design.
// assume all necessary headers have been included int main() { { int* arrI = nullptr; arrI = insertAtEnd(arrI, 0, 1); // 1 arrI = insertAtEnd(arrI, 1, 5); // 1, 5 arrI = insertAtEnd(arrI, 2, -3);// 1, 5, -3 delete[] arrI; } { double* arrD = nullptr; arrD = insertAtEnd(arrD, 0, 1.2);// 1.2 arrD = insertAtEnd(arrD, 1, 2.3);// 1.2, 2.3 arrD = insertAtEnd(arrD, 2, 3.4);// 1.2, 2.3, 3.4 delete[] arrD; } { char* arrC = nullptr; arrC = insertAtEnd(arrC, 0, 'a');// a\0 arrC = insertAtEnd(arrC, 1, 'b');// ab\0 arrC = insertAtEnd(arrC, 2, 'c');// abc\0 cout << arrC; delete[] arrC; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
