Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined(_MSC_VER)
- #include <__msvc_all_public_headers.hpp>
- #elif defined(__GNUC__)
- #include <bits/stdc++.h>
- #else
- #error "Unsupported compiler"
- #endif
- using namespace std;
- template<typename T>
- T Add(const T& a, const T& b) {
- return a + b;
- }
- template<typename T>
- T ArraySum(const T* pArr, size_t arrSize) {
- T sum = T();
- for (int i = 0; i < arrSize; ++i) {
- sum = Add(sum, pArr[i]);
- }
- return sum;
- }
- template<typename T>
- T Max(const T* pArr, size_t arrSize) {
- if (arrSize == 0) {
- throw invalid_argument("Array is empty");
- }
- T max = pArr[0];
- for (int i = 1; i < arrSize; ++i) {
- if (pArr[i] > max) {
- max = pArr[i];
- }
- }
- return max;
- }
- int main() {
- int arr[] = { 1, 2, 3, 4, 5 };
- cout << ArraySum(arr, 5) << endl;
- cout << Max(arr, 5) << endl;
- string arr2[] = { "abc", "def", "ghi" };
- cout << ArraySum(arr2, 3) << endl;
- cout << Max(arr2, 3) << endl;
- double arr3[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
- cout << ArraySum(arr3, 5) << endl;
- cout << Max(arr3, 5) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement