#include <iostream>
using namespace std;
// Class template for a generic array
template <class T>
class GenericArray {
private:
T arr[100]; // Fixed-size array
int size; // Size of the array
public:
// Constructor
GenericArray(T elements[], int n) {
size = n;
for (int i = 0; i < size; i++) {
arr[i] = elements[i];
}
}
// Function to calculate the summation of all elements
T findSum() {
T sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
// Display elements of the array
void display() {
cout << "[ ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << "]" << endl;
}
};
int main() {
// Test with integers
int intArr[] = {1, 2, 3, 4, 5};
GenericArray<int> intArray(intArr, 5);
cout << "Integer Array: ";
intArray.display();
cout << "Sum: " << intArray.findSum() << endl;
// Test with doubles
double doubleArr[] = {1.1, 2.2, 3.3, 4.4, 5.5};
GenericArray<double> doubleArray(doubleArr, 5);
cout << "\nDouble Array: ";
doubleArray.display();
cout << "Sum: " << doubleArray.findSum() << endl;
// Test with characters
char charArr[] = {'a', 'b', 'c', 'd', 'e'};
GenericArray<char> charArray(charArr, 5);
cout << "\nCharacter Array: ";
charArray.display();
cout << "Sum: " << static_cast<int>(charArray.findSum()) << " (ASCII Sum)" << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gQ2xhc3MgdGVtcGxhdGUgZm9yIGEgZ2VuZXJpYyBhcnJheQp0ZW1wbGF0ZSA8Y2xhc3MgVD4KY2xhc3MgR2VuZXJpY0FycmF5IHsKcHJpdmF0ZToKICAgIFQgYXJyWzEwMF07IC8vIEZpeGVkLXNpemUgYXJyYXkKICAgIGludCBzaXplOyAgIC8vIFNpemUgb2YgdGhlIGFycmF5CgpwdWJsaWM6CiAgICAvLyBDb25zdHJ1Y3RvcgogICAgR2VuZXJpY0FycmF5KFQgZWxlbWVudHNbXSwgaW50IG4pIHsKICAgICAgICBzaXplID0gbjsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemU7IGkrKykgewogICAgICAgICAgICBhcnJbaV0gPSBlbGVtZW50c1tpXTsKICAgICAgICB9CiAgICB9CgogICAgLy8gRnVuY3Rpb24gdG8gY2FsY3VsYXRlIHRoZSBzdW1tYXRpb24gb2YgYWxsIGVsZW1lbnRzCiAgICBUIGZpbmRTdW0oKSB7CiAgICAgICAgVCBzdW0gPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZTsgaSsrKSB7CiAgICAgICAgICAgIHN1bSArPSBhcnJbaV07CiAgICAgICAgfQogICAgICAgIHJldHVybiBzdW07CiAgICB9CgogICAgLy8gRGlzcGxheSBlbGVtZW50cyBvZiB0aGUgYXJyYXkKICAgIHZvaWQgZGlzcGxheSgpIHsKICAgICAgICBjb3V0IDw8ICJbICI7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICAgICAgY291dCA8PCBhcnJbaV0gPDwgIiAiOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8ICJdIiA8PCBlbmRsOwogICAgfQp9OwoKaW50IG1haW4oKSB7CiAgICAvLyBUZXN0IHdpdGggaW50ZWdlcnMKICAgIGludCBpbnRBcnJbXSA9IHsxLCAyLCAzLCA0LCA1fTsKICAgIEdlbmVyaWNBcnJheTxpbnQ+IGludEFycmF5KGludEFyciwgNSk7CiAgICBjb3V0IDw8ICJJbnRlZ2VyIEFycmF5OiAiOwogICAgaW50QXJyYXkuZGlzcGxheSgpOwogICAgY291dCA8PCAiU3VtOiAiIDw8IGludEFycmF5LmZpbmRTdW0oKSA8PCBlbmRsOwoKICAgIC8vIFRlc3Qgd2l0aCBkb3VibGVzCiAgICBkb3VibGUgZG91YmxlQXJyW10gPSB7MS4xLCAyLjIsIDMuMywgNC40LCA1LjV9OwogICAgR2VuZXJpY0FycmF5PGRvdWJsZT4gZG91YmxlQXJyYXkoZG91YmxlQXJyLCA1KTsKICAgIGNvdXQgPDwgIlxuRG91YmxlIEFycmF5OiAiOwogICAgZ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