Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- template <typename T>
- T calculateAverage(const std::vector<T>& arr) {
- T sum = 0;
- for (const auto& element : arr) {
- sum += element;
- }
- return sum / static_cast<T>(arr.size());
- }
- int main() {
- // Пример использования функции для массива целых чисел
- std::vector<int> intArray = {1, 2, 3, 4, 5};
- int averageInt = calculateAverage(intArray);
- std::cout << "Среднее арифметическое целых чисел: " << averageInt << std::endl;
- // Пример использования функции для массива чисел с плавающей запятой
- std::vector<double> doubleArray = {1.5, 2.5, 3.5, 4.5, 5.5};
- double averageDouble = calculateAverage(doubleArray);
- std::cout << "Среднее арифметическое чисел с плавающей запятой: " << averageDouble << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement