include iostream include math include time template class sred mas int

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <math.h>
#include <time.h>
template <class T> T sred(T *mas, int n)
{
T nn = 1;
int k = 0;
for ( int i = 0; i < n; i++){
if( mas[i] != 0)
nn *=mas[i];
else
k++;
}
double r = n;
if ( n == k)
return 0;
else
if (nn > 0)
return pow(abs(nn), 1/r);
else
return -1*pow(abs(nn), 1/r);
}
template <class T> void show(T *mas, int n){
for (int i = 0; i < n; i++)
std::cout << mas[i] << " ";
std::cout << std::endl;
}
template <class T> void random(T *mas, int n)
{
srand(time(NULL));
for (int i=0;i<n;i++) mas[i]=50-rand()%100;
std::cout << "Текущий массив: ";
show(mas, n);
}
int main(){
setlocale(LC_ALL, "russian");
const int n = 2;
double mas[n];
random(mas, n);
std::cout << "Среднее геометрическое равно:" << std::endl;
std::cout << sred(mas, n) << std::endl;
}