Лаб. 5 ООП (Вариант 18)
.docx
#include <iostream>
#include <random>
using namespace std;
template<typename T>
void input_array(T* arr, int size) {
for (int i = 0; i < size; i++) {
cout << "Добавьте элемент " << i << " : ";
cin >> arr[i];
}
}
//template<>
void input_array(double* arr, int size) {
srand(time(NULL));
for (int i = 0; i < size; i++) {
cout << "Элемент double " << i << " : ";
arr[i]= -5 + (double)rand() / (RAND_MAX / 10 - -10);
cout << arr[i]<<"\n";
}
}
template<typename T>
T sum_bigger_n(T* arr, int size, T n) {
T sum = 0;
for (int i = 0; i < size; i++) {
if (arr[i] < n) {
sum += arr[i];
}
}
cout <<"Сумма элементов меньше "<<n<<" : ";
return sum;
}
//template<>
char sum_bigger_n(char* arr, int size, char n) {
cout<<"Суммирование не проводится";
return 0;
}
int main() {
setlocale(LC_ALL, "Russian");
system("chcp 1251");
const int size = 5;
int int_arr[size];
char char_arr[size];
double double_arr[size];
cout << "--------int--------" << "\n";
input_array(int_arr, size);
cout <<sum_bigger_n(int_arr, size, 6) << "\n";
cout << "--------double--------" << "\n";
input_array(double_arr, size);
cout << sum_bigger_n(double_arr, size, 3.287) << "\n";
cout << "--------char--------" << "\n";
input_array(char_arr, size);
cout << sum_bigger_n(char_arr, size, 'A')<<"\n";
return 0;
}