Write a template function to calculate the sum for all elements in an array of numerical values (e.g., int, double). The function should work for any size of array. Use const parameter when appropriate.
You should write a driver to test your code, but you don't need to submit it.
Assistant
Here is a template function that calculates the sum of all elements in an array of numerical values:
cpp
#include <iostream>
template<typename T, size_t N>
T calculateSum(const T (&arr)[N]) {
T sum = 0;
for (size_t i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int intArr[] = {1, 2, 3, 4, 5};
double doubleArr[] = {1.5, 2.5, 3.5, 4.5, 5.5};
int intSum = calculateSum(intArr);
std::cout << "Sum of intArr: " << intSum << std::endl;
double doubleSum = calculateSum(doubleArr);
std::cout << "Sum of doubleArr: " << doubleSum << std::endl;
return 0;
}
User
What are the pre-condition and post-condition of the template function to calculate the sum for all elements in an array of numerical values (e.g., int, double) where the function should work for any size of array along with using the const parameter when appropriate and write a driver to test your code ?
Assistant
The pre-condition of the template function to calculate the sum for all elements in an array of numerical values is that the array must be a valid array of numerical values (e.g., int, double). The array should be passed to the function as a constant reference parameter to avoid unnecessary copies and to prevent modifications to the original array.
The post-condition of the function is that it returns the sum of all elements in the array. The sum is of the same type as the elements of the array (e.g., int, double). The original array is not modified by the function.
Here is the driver code to test the template function:
cpp
In this code, the calculateSum function is a template function that takes an array arr of type T and size N as a constant reference parameter. The T template parameter represents the type of the array elements. Inside the function, a variable sum of type T is initialized to 0. Then, it iterates through the array using a for loop and adds each element to the sum variable. Finally, it returns the calculated sum.
In the main function, we test the calculateSum function by passing two different arrays: intArr of type int and doubleArr of type double. We store the results in intSum and doubleSum variables, respectively, and then print them to the console.
Note that since the calculateSum function takes a constant reference parameter, it does not modify the original array.
#include <iostream>
template<typename T, size_t N>
T calculateSum(const T (&arr)[N]) {
T sum = 0;
for (size_t i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int intArr[] = {1, 2, 3, 4, 5};
double doubleArr[] = {1.5, 2.5, 3.5, 4.5, 5.5};
int intSum = calculateSum(intArr);
std::cout << "Sum of intArr: " << intSum << std::endl;
double doubleSum = calculateSum(doubleArr);
std::cout << "Sum of doubleArr: " << doubleSum << std::endl;
return 0;
}
In this driver code, we create two arrays: intArr of type int and doubleArr of type double. We pass these arrays to the calculateSum function and store the results in intSum and doubleSum variables, respectively. Finally, we print the sums to the console to verify the correctness of the function.
Both the pre-condition and post-condition are satisfied by the template function and the driver code.