summaryrefslogtreecommitdiff
path: root/cs162/wk9/vectors/vectors.cc
diff options
context:
space:
mode:
Diffstat (limited to 'cs162/wk9/vectors/vectors.cc')
-rw-r--r--cs162/wk9/vectors/vectors.cc172
1 files changed, 172 insertions, 0 deletions
diff --git a/cs162/wk9/vectors/vectors.cc b/cs162/wk9/vectors/vectors.cc
new file mode 100644
index 0000000..8aad0fe
--- /dev/null
+++ b/cs162/wk9/vectors/vectors.cc
@@ -0,0 +1,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