-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAverageOfAnArray.cpp
45 lines (35 loc) · 984 Bytes
/
AverageOfAnArray.cpp
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
43
44
45
/**
* \file AverageOfAnArray.cpp
* \brief
*
* \review
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
template <typename T>
T
average(const T *array, const int length)
{
T sum {};
for (int i = 0; i < length; i++) {
sum += array[i];
}
sum /= length;
return sum;
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
const int array1[] { 5, 3, 2, 1, 4 };
std::cout << "Average of array1 = " << ::average(array1, 5) << std::endl;
const double array2[] { 3.12, 3.45, 9.23, 6.34 };
std::cout << "Average of array2 = " << ::average(array2, 4) << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
Average of array1 = 3
Average of array2 = 5.535
#endif