Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
448 B
3
Indexable
#include <iostream>
using namespace std;

template <class T>
T sum(T a[], int size) {
    T s = 0;
    for (int i = 0; i < size; i++) {
        s = s + a[i];
    }
    return s;
}

int main() {
    int x[5] = {1, 2, 3, 4, 5};
    float y[3] = {1.1, 2.2, 3.3};
    
    // Corrected function calls
    cout << sum(x, 5) << endl; // Output: 15 (sum of integers in x)
    cout << sum(y, 3) << endl; // Output: 6.6 (sum of floats in y)
    return 0;
}