The Task
Define a family of functions (a templated function) namedpartSum
that calculates the partial sum values of two arrays ofdifferent sizes. The elements of tha arrays are all of the same template typeT
.
Your function should accept two arrays (e.g.arr1
andarr2
) asunmodifiablearguments along with their different sizes (both sizes defaulting to4u
) as separate arguments and return a new dynamically-allocated array of typeT
containing the partial sum values. The size of the returned array will be the same as the size of the larger input array.
The partial sum value stored at indexi
of the returned array is the sum of all the elements in botharr1
andarr2
up to and including indexi
. Make sure you don’t go out of bounds in either array; i.e., stop using an array in the computation if the index goes out of bounds for that array.
Assume that the+
operator is defined for the element’s type.
Specialize the function to compute the partial sum forchar
arrays differently. The partial sum value stored at indexi
of the returned array for this (specialized) case is simply the sum ofarr1[i]
andarr2[i]
minus the character'A'
. Again, make sure you don’t go out of bounds in either array.
Assume all parameters passed to the function are valid and the arrays are non-empty.
Write your solution in the textbox below.
Sample Driver
The client code listed below uses your template to produce the output shown.
// assume all necessary *standard* headers have been included
int main() {
int numArr1[]{10, 4, 8, 2};
int numArr2[]{2, -2, -5, 3};
int* numSum = partSumint>(numArr1, numArr2);
std::cout "Partial Sum for Numeric Array: ";
for (auto i = 0u; i 4u; i++)
std::cout " ";
std::cout
delete[] numSum;
std::string strArr1[]{"This ", "the "};
std::string strArr2[]{"is ", "OOP345 ", "midterm ", "test."};
std::string* strSum = partSumstring>(strArr1, strArr2, 2u);
std::cout "Partial Sum for String Array:\n";
for (auto i = 0u; i 4u; i++)
std::cout
delete[] strSum;
char chArr1[]{'X', '$', '8'};
char chArr2[]{'B', 'O', 'T'};
char* chSum = partSumchar>(chArr1, chArr2, 3u, 3u);
std::cout "Partial Sum for Character Array: ";
for (auto i = 0u; i 3u; i++)
std::cout
std::cout
delete[] chSum;
}
Expected Output
Partial Sum for Numeric Array: 12 14 17 22
Partial Sum for String Array:
This is
This is the OOP345
This is the OOP345 midterm
This is the OOP345 midterm test.
Partial Sum for Character Array: Y2K