Question
Define a family of functions (a templated function) named matrixProd that calculates the matrix product of two arrays of different sizes . Your function should
Define a family of functions (a templated function) named matrixProd that calculates the matrix "product" of two arrays of different sizes. Your function should accept as arguments:
- two unmodifiable arrays (e.g. arr1 and arr2) with elements of the same type
- the size of each array (both defaulting to 3u) as separate arguments
This template function should return a new dynamically-allocated array of the product values. The size of the returned array will be the same as the size of the smaller array. Each element at index i of the returned product array is calculated as follows:
product[i] = arr1[i] * (arr2[0] + arr2[1] + arr2[2] + ... last element in arr2)
Assume that the +, +=, *, and *= operators are defined for the element's type.
Specialize the function to compute the product for char arrays differently. The product value stored at position i of the returned array is simply the sum of the elements at position i in the input arrays divided by 2. The size of the returned array is the same as the size of the smaller array which therefore is the number of loop iterations you will need to perform for computing elements of the returned array.
Assume all parameters passed to this function are valid and that 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, 5, 12, 2}; int numArr2[]{2, 1, 3}; int* numProd = matrixProd(numArr1, numArr2, 4u); std::cout << "Product for Integer Array: "; for (auto i = 0u; i < 3u; i++) std::cout << numProd[i] << " "; std::cout << std::endl; delete[] numProd; double dbArr1[]{5.0, 10.5, 3.25}; double dbArr2[]{1.0, 2.0, 3.0}; double* dbProd = matrixProd(dbArr1, dbArr2); std::cout << "Product for Real Number Array: "; for (auto i = 0u; i < 3u; i++) std::cout << dbProd[i] << " "; std::cout << std::endl; delete[] dbProd; char chArr1[]{'A', '#', '1', 'J', '!'}; char chArr2[]{'Z', '_', 'u', 'L'}; char* chProd = matrixProd(chArr1, chArr2, 5u, 4u); std::cout << "Product for Character Array: "; for (auto i = 0u; i < 4u; i++) std::cout << chProd[i]; std::cout << std::endl; delete[] chProd; }
Expected Output
Product for Integer Array: 60 30 72 Product for Real Number Array: 30 63 19.5 Product for Character Array: MASK
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started