Другие языки программирования и технологии

Написать реализацию перегруженных функций: int func (int * arr, int length); int func (double * arr, int length);

Написать реализацию перегруженных функций:
int func (int * arr, int length);
int func (double * arr, int length);
Функция func проверяет в массиве чередуются знаки
МH
Мася! Haha!
138
Только функции возвращают bool (true -- если чередуются, false -- если не чередуются) :

#include <iostream>
#include <iomanip>

using namespace std;

template < class T >
bool posorneg(T x1, T x2) { return (x1 >= 0 && x2 >= 0) || (x1 < 0 && x2 < 0); }

template < class T >
bool cfunc(T *arr, int length) {
for (int c = 0; c < length - 2; ++c) {
if ( posorneg(arr[c], arr[c + 1]) ) {
return false;
}
}
return true;
}

bool func(int *arr, int length) { return cfunc(arr, length); }

bool func(double *arr, int length) { return cfunc(arr, length); }

int main() {
int ia1[] = { 1, -1, 2, -2, 3, -3, 4, -4 };
int ia2[] = { 1, -1, 2, 2, -1 };
double da1[] = { -1.1, 0.0, -2.2, 1.1 };
double da2[] = { -1.1, -2.2, 3.3 };
cout << boolalpha;
cout << "first int array: " << func(ia1, sizeof(ia1)/sizeof(int)) << endl;
cout << "second int array: " << func(ia2, sizeof(ia2)/sizeof(int)) << endl;
cout << "first double array: " << func(da1, sizeof(da1)/sizeof(double)) << endl;
cout << "second double array: " << func(da2, sizeof(da2)/sizeof(double)) << endl;
return 0;
}
SN
Sanek Not
84 557
Лучший ответ