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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <random>
#include <numeric>
using namespace std; // Just to make the code easier to read in the example...
// Template function to return the average of the elements in a vector
template <class T> T average(const vector<T>& vec)
{
static_assert(std::is_arithmetic<T>::value,
"Type parameter for average() must be arithmetic.");
T sum{};
for_each(cbegin(vec), cend(vec),
[&sum](const T& value) { sum += value; });
return sum / vec.size();
}
template <class T> T addition(const vector<T>& vec)
{
static_assert(std::is_arithmetic<T>::value,
"Type parameter for add() must be arithmetic.");
T sum{};
for_each(cbegin(vec), cend(vec),
[&sum](const T& value) {sum += value; });
return sum;
}
template <class T> void setValues(vector<T>& vec, T start, T increment) // Template function to set a vector to values beginning with start
{ // and incremented by increment
static_assert(std::is_arithmetic<T>::value,
"Type parameter for setValues() must be arithmetic.");
T current{ start };
generate(begin(vec), end(vec),
[increment, ¤t]() { T result{ current };
current += increment;
return result; });
}
template<class T> void randomValues(vector<T>& vec, T min_value, T max_value) // Template function to set a vector to random values between min and max
{
static_assert(std::is_arithmetic<T>::value,
"Type parameter for randomValues() must be arithmetic.");
random_device engine; // Random number source
auto max_rand = engine.max(); // Maximum random value
auto min_rand = engine.min(); // Minimum random value
generate(begin(vec), end(vec),
[&engine, max_rand, min_rand, min_value, max_value]
{ return static_cast<T>(static_cast<double>(engine()) /
max_rand*(max_value - min_value) + min_value); });
}
template<class T> void listVector(const vector<T>& vec) // Template function to list the values in a vector
{
int count{}; // Used to control outputs per line
const int valuesPerLine{ 5 };
for_each(cbegin(vec), cend(vec),
[&count, valuesPerLine](const T& n) {
cout << setw(5) << n << " ";
if (++count % valuesPerLine == 0)
cout << endl; });
}
int main()
{
vector<int> integerData(25);
randomValues(integerData, 1, 100); // Set random integer values
cout << "25 random generated numbers: " << endl;
listVector(integerData);
cout << "\nAverage value is " << average(integerData) << endl;
{
vector<int> integerData(5);
randomValues(integerData, 1, 100);
cout << "\nRow 1 " << setw(5) << endl;
listVector(integerData);
cout << "\nAverage = " << average(integerData) << endl;
cout << "Total = " << addition(integerData) << endl;
}
{
vector<int> randomData(5);
randomValues(randomData, 1, 100);
cout << "\nRow 2 " << setw(5) << endl;
listVector(randomData);
cout << "\nAverage = " << average(randomData) << endl;
cout << "Total = " << addition(randomData) << endl;
}
{
vector<int> randomData(5);
randomValues(randomData, 1, 100);
cout << "\nRow 3 " << setw(5) << endl;
listVector(randomData);
cout << "\nAverage = " << average(randomData) << endl;
cout << "Total = " << addition(randomData) << endl;
}
{
vector<int> randomData(5);
randomValues(randomData, 1, 100);
cout << "\nRow 4 " << setw(5) << endl;
listVector(randomData);
cout << "\nAverage = " << average(randomData) << endl;
cout << "Total = " << addition(randomData) << endl;
}
{
vector<int> randomData(5);
randomValues(randomData, 1, 100);
cout << "\nRow 5 " << setw(5) << endl;
listVector(randomData);
cout << "\nAverage = " << average(randomData) << endl;
cout << "Total = " << addition(randomData) << endl;
}
}
| |