Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <class array_type>
- array_type sum(array_type *a, const int N)
- {
- array_type sum = 0;
- for (int i = 0; i < N; i++) {
- sum += a[i];
- }
- return sum;
- }
- int main() {
- int arrayInt[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- int intArraySize = sizeof(arrayInt) / sizeof(arrayInt[0]);
- float arrayFloat[] = { 2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8, 10.9, 11.0 };
- int floatArraySize = sizeof(arrayFloat) / sizeof(arrayFloat[0]);
- double arrayDouble[] = { 2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8, 10.9, 11.0 };
- int doubleArraySize = sizeof(arrayDouble) / sizeof(arrayDouble[0]);
- cout << "Array Int Sum: " << sum(arrayInt, intArraySize) << endl;
- cout << "Array Float Sum: " << sum(arrayFloat, floatArraySize) << endl;
- cout << "Array Double Sum: " << sum(arrayDouble, doubleArraySize) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement