blob: 8aad0fe6f6a6645b3efa1f9022709ce33e1905a3 (
plain) (
blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#include <iomanip>
#include <iostream>
#include <numeric>
#include <random>
#define DEBUG
#define FINITE
/**
* @brief vector related operations and types
* @see std::vector
*/
namespace vectors {
/**
* @brief A generic "real" number type
* @see double
*/
typedef double real_type;
/**
* @brief Get the largest value of a vector.
* @return real_type
* @see real_type
* @see std::vector
*/
real_type largest_vector_value(const std::vector<real_type> &);
/**
* @brief Get the smallest value of a vector.
* @return real_type
* @see real_type
* @see std::vector
*/
real_type smallest_vector_value(const std::vector<real_type> &);
/**
* @brief Get the average value of a vector.
* @return real_type
* @see real_type
* @see std::vector
*/
real_type average_vector_value(const std::vector<real_type> &);
} // namespace vectors
int main() {
// Settings and RNG-related producers and criteria
int user_choice;
int value_precision = 2;
vectors::real_type minimum_value = 0;
vectors::real_type maximum_value = 1000;
std::size_t random_values_size;
const std::string &rt = typeid(vectors::real_type).name();
const std::string &st = typeid(std::size_t).name();
std::vector<vectors::real_type> random_values;
std::random_device random_device;
std::default_random_engine random_engine{random_device()};
// Get the users input for settings and RNG criteria
#ifdef FINITE
std::cout << "minimum random value(" + rt + "): ";
std::cin >> minimum_value;
std::cout << "maximum random value(" + rt + "): ";
std::cin >> maximum_value;
#endif
std::cout << "number of initial random values(" + st + "): ";
std::cin >> random_values_size;
#ifdef FINITE
std::cout << "value precision(" + st + "): ";
std::cin >> value_precision;
#endif
// Set the precision of random number output based on the users choice.
std::cout << std::fixed << std::setprecision(value_precision);
// Set up the RNG producer based on the user's criteria
std::uniform_real_distribution<vectors::real_type> uniform_distribution{
minimum_value, maximum_value};
// Append the user's preference of maximum random values to the random value
// pool
for (std::size_t i = 0; i < random_values_size; ++i) {
random_values.push_back(uniform_distribution(random_engine));
}
std::cout << "what operation would you like to perform?\n"
" 1. find the largest value\n"
" 2. find the smallest value\n"
" 3. compute the average value\n"
" 4. emplace more random values into the value pool\n"
#ifdef DEBUG
" 5. number of random values in the value pool\n"
#endif
" 0. quit"
<< std::endl;
// Keep performing until the user would like
do {
// Get the user's operation choice
std::cout << "your choice(" + std::string(typeid(int).name()) + "): ";
std::cin >> user_choice;
// Perform the user's operation choice
switch (user_choice) {
case 0: {
// Say "goodbye." and free up any used resources.
std::cout << "goodbye." << std::endl;
return 0;
} // break;
case 1: {
// Output the largest value of the vector and run again.
std::cout << "largest value: "
<< vectors::largest_vector_value(random_values) << std::endl;
} break;
case 2: {
// Output the smallest value of the vector and run again.
std::cout << "smallest value: "
<< vectors::smallest_vector_value(random_values) << std::endl;
} break;
case 3: {
// Output the average value of the vector and run again.
std::cout << "average value: "
<< vectors::average_vector_value(random_values) << std::endl;
} break;
case 4: {
std::size_t append_maximum_random_values;
// Get the number of random values to append to the random value pool from
// the user.
std::cout
<< "number of random values to append to the random value pool: ";
std::cin >> append_maximum_random_values;
// Append the user's choice of extra random values to the random value
// pool
for (std::size_t i = 0; i < append_maximum_random_values; ++i) {
random_values.push_back(uniform_distribution(random_engine));
}
} break;
#ifdef DEBUG
case 5: {
// Output the number of values in the random value pool and run again.
std::cout << "number of values in the random value pool: "
<< random_values.size() << std::endl;
} break;
#endif
default: {
// Inform the user the operation is invalid and run gain.
std::cout << "you chose an invalid choice. please choice a valid choice "
"the next time around."
<< std::endl;
} break;
}
} while (user_choice != 0);
return 0;
}
namespace vectors {
real_type largest_vector_value(const std::vector<real_type> &v) {
return *std::max_element(v.begin(), v.end());
}
real_type smallest_vector_value(const std::vector<real_type> &v) {
return *std::min_element(v.begin(), v.end());
}
real_type average_vector_value(const std::vector<real_type> &v) {
return std::reduce(v.begin(), v.end()) / v.size();
}
} // namespace vectors
|