From aa0c4781377d6d00ebe14d59692c4d4b1a1ae335 Mon Sep 17 00:00:00 2001 From: Sandipan Mohanty <s.mohanty@fz-juelich.de> Date: Mon, 22 May 2023 12:21:42 +0200 Subject: [PATCH] Collect examples for chapter 6 --- {day4 => chapter_06}/examples/CMakeLists.txt | 0 chapter_06/examples/K.cc | 37 +++++++++++++++++++ {day4 => chapter_06}/examples/advent.cc | 0 {day4 => chapter_06}/examples/any_demo.cc | 0 .../examples/cards_problem.cc | 0 {day4 => chapter_06}/examples/chrono_demo.cc | 0 .../examples}/dangling0.cc | 0 .../examples}/dangling1.cc | 0 {day5 => chapter_06}/examples/dist.cc | 0 {day4 => chapter_06}/examples/feb.cc | 0 {day5 => chapter_06}/examples/format1.cc | 0 .../ranges => chapter_06/examples}/gerund.cc | 0 .../ranges => chapter_06/examples}/iota.cc | 0 .../examples/normal_distribution.cc | 0 {day4 => chapter_06}/examples/opt_qsolve.cc | 0 {day4 => chapter_06}/examples/pass1.cc | 0 {day4 => chapter_06}/examples/poisson.cc | 0 .../polymorphic/1_virtual/CMakeLists.txt | 0 .../examples/polymorphic/1_virtual/Circle.cc | 0 .../examples/polymorphic/1_virtual/Circle.hh | 0 .../examples/polymorphic/1_virtual/Point.cc | 0 .../examples/polymorphic/1_virtual/Point.hh | 0 .../examples/polymorphic/1_virtual/Polygon.hh | 0 .../examples/polymorphic/1_virtual/Shape.hh | 0 .../polymorphic/1_virtual/Triangle.cc | 0 .../polymorphic/1_virtual/Triangle.hh | 0 .../examples/polymorphic/1_virtual/main.cc | 0 .../examples/polymorphic/2_any/CMakeLists.txt | 0 .../examples/polymorphic/2_any/Circle.cc | 0 .../examples/polymorphic/2_any/Circle.hh | 0 .../examples/polymorphic/2_any/Point.cc | 0 .../examples/polymorphic/2_any/Point.hh | 0 .../examples/polymorphic/2_any/Polygon.hh | 0 .../examples/polymorphic/2_any/Triangle.cc | 0 .../examples/polymorphic/2_any/Triangle.hh | 0 .../examples/polymorphic/2_any/main.cc | 0 .../polymorphic/3_variant/CMakeLists.txt | 0 .../examples/polymorphic/3_variant/Circle.cc | 0 .../examples/polymorphic/3_variant/Circle.hh | 0 .../examples/polymorphic/3_variant/Point.cc | 0 .../examples/polymorphic/3_variant/Point.hh | 0 .../examples/polymorphic/3_variant/Polygon.hh | 0 .../polymorphic/3_variant/Triangle.cc | 0 .../polymorphic/3_variant/Triangle.hh | 0 .../examples/polymorphic/3_variant/main.cc | 0 .../polymorphic/4_type_erasure/CMakeLists.txt | 0 .../polymorphic/4_type_erasure/Circle.cc | 0 .../polymorphic/4_type_erasure/Circle.hh | 0 .../polymorphic/4_type_erasure/Point.cc | 0 .../polymorphic/4_type_erasure/Point.hh | 0 .../polymorphic/4_type_erasure/Polygon.hh | 0 .../polymorphic/4_type_erasure/Triangle.cc | 0 .../polymorphic/4_type_erasure/Triangle.hh | 0 .../polymorphic/4_type_erasure/main.cc | 0 {day4 => chapter_06}/examples/seqops.cc | 0 .../examples}/seqops_range.cc | 0 {day5 => chapter_06}/examples/spans/main.cc | 0 .../examples/spans/spanfunc.cc | 0 .../examples/spans/spanfunc.hh | 0 {day4 => chapter_06}/examples/test.dat | 0 .../examples}/trig_views.cc | 0 .../examples}/trig_views2.cc | 0 {day4 => chapter_06}/examples/variant_0.cc | 0 {day4 => chapter_06}/examples/variant_1.cc | 0 {day4 => chapter_06}/examples/variant_2.cc | 0 {day4 => chapter_06}/examples/variant_3.cc | 0 {day4 => chapter_06}/examples/variant_4.cc | 0 .../examples}/views_and_span.cc | 0 {day4 => chapter_06}/examples/weighted_die.cc | 0 {day4 => chapter_06}/examples/word_count.cc | 0 .../examples/word_count_fmt.cc | 0 {day4/examples => chapter_06/solutions}/K.cc | 0 chapter_06/solutions/normal_distribution.cc | 30 +++++++++++++++ chapter_06/solutions/weighted_die.cc | 15 ++++++++ 74 files changed, 82 insertions(+) rename {day4 => chapter_06}/examples/CMakeLists.txt (100%) create mode 100644 chapter_06/examples/K.cc rename {day4 => chapter_06}/examples/advent.cc (100%) rename {day4 => chapter_06}/examples/any_demo.cc (100%) rename {day4 => chapter_06}/examples/cards_problem.cc (100%) rename {day4 => chapter_06}/examples/chrono_demo.cc (100%) rename {day5/examples/ranges => chapter_06/examples}/dangling0.cc (100%) rename {day5/examples/ranges => chapter_06/examples}/dangling1.cc (100%) rename {day5 => chapter_06}/examples/dist.cc (100%) rename {day4 => chapter_06}/examples/feb.cc (100%) rename {day5 => chapter_06}/examples/format1.cc (100%) rename {day5/examples/ranges => chapter_06/examples}/gerund.cc (100%) rename {day5/examples/ranges => chapter_06/examples}/iota.cc (100%) rename {day4 => chapter_06}/examples/normal_distribution.cc (100%) rename {day4 => chapter_06}/examples/opt_qsolve.cc (100%) rename {day4 => chapter_06}/examples/pass1.cc (100%) rename {day4 => chapter_06}/examples/poisson.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/CMakeLists.txt (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Circle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Circle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Point.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Point.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Polygon.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Shape.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Triangle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/Triangle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/1_virtual/main.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/CMakeLists.txt (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/Circle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/Circle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/Point.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/Point.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/Polygon.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/Triangle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/Triangle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/2_any/main.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/CMakeLists.txt (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/Circle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/Circle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/Point.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/Point.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/Polygon.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/Triangle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/Triangle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/3_variant/main.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/CMakeLists.txt (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/Circle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/Circle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/Point.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/Point.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/Polygon.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/Triangle.cc (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/Triangle.hh (100%) rename {day5 => chapter_06}/examples/polymorphic/4_type_erasure/main.cc (100%) rename {day4 => chapter_06}/examples/seqops.cc (100%) rename {day5/examples/ranges => chapter_06/examples}/seqops_range.cc (100%) rename {day5 => chapter_06}/examples/spans/main.cc (100%) rename {day5 => chapter_06}/examples/spans/spanfunc.cc (100%) rename {day5 => chapter_06}/examples/spans/spanfunc.hh (100%) rename {day4 => chapter_06}/examples/test.dat (100%) rename {day5/examples/ranges => chapter_06/examples}/trig_views.cc (100%) rename {day5/examples/ranges => chapter_06/examples}/trig_views2.cc (100%) rename {day4 => chapter_06}/examples/variant_0.cc (100%) rename {day4 => chapter_06}/examples/variant_1.cc (100%) rename {day4 => chapter_06}/examples/variant_2.cc (100%) rename {day4 => chapter_06}/examples/variant_3.cc (100%) rename {day4 => chapter_06}/examples/variant_4.cc (100%) rename {day5/examples/ranges => chapter_06/examples}/views_and_span.cc (100%) rename {day4 => chapter_06}/examples/weighted_die.cc (100%) rename {day4 => chapter_06}/examples/word_count.cc (100%) rename day5/examples/word_count.cc => chapter_06/examples/word_count_fmt.cc (100%) rename {day4/examples => chapter_06/solutions}/K.cc (100%) create mode 100644 chapter_06/solutions/normal_distribution.cc create mode 100644 chapter_06/solutions/weighted_die.cc diff --git a/day4/examples/CMakeLists.txt b/chapter_06/examples/CMakeLists.txt similarity index 100% rename from day4/examples/CMakeLists.txt rename to chapter_06/examples/CMakeLists.txt diff --git a/chapter_06/examples/K.cc b/chapter_06/examples/K.cc new file mode 100644 index 0000000..9eeec5c --- /dev/null +++ b/chapter_06/examples/K.cc @@ -0,0 +1,37 @@ +#include <functional> +#include <iostream> +#include <random> +#include <vector> + +inline auto power(double x, unsigned N) -> double +{ + double ans = 1; + for (unsigned u = 0; u < N; ++u) + ans *= x; + return ans; +} +auto main() -> int +{ + std::vector d(1000000, 0.); + double mu{ 1.0 }; + double sigma{ 35.8 }; + + // Fill d with random numbers distributed as a normal + // distribution with mu and sigma as given above. + + for (auto & el : d) el = gen(); + // /////// + + double mn = 0; + for (auto r : d) + mn += r; + mn /= d.size(); + double kn = 0; + for (auto r : d) + kn += power(r - mn, 4); + double kd = 0; + for (auto r : d) + kd += power(r - mn, 2); + std::cout << d.size() * kn / (kd * kd) << "\n"; +} + diff --git a/day4/examples/advent.cc b/chapter_06/examples/advent.cc similarity index 100% rename from day4/examples/advent.cc rename to chapter_06/examples/advent.cc diff --git a/day4/examples/any_demo.cc b/chapter_06/examples/any_demo.cc similarity index 100% rename from day4/examples/any_demo.cc rename to chapter_06/examples/any_demo.cc diff --git a/day4/examples/cards_problem.cc b/chapter_06/examples/cards_problem.cc similarity index 100% rename from day4/examples/cards_problem.cc rename to chapter_06/examples/cards_problem.cc diff --git a/day4/examples/chrono_demo.cc b/chapter_06/examples/chrono_demo.cc similarity index 100% rename from day4/examples/chrono_demo.cc rename to chapter_06/examples/chrono_demo.cc diff --git a/day5/examples/ranges/dangling0.cc b/chapter_06/examples/dangling0.cc similarity index 100% rename from day5/examples/ranges/dangling0.cc rename to chapter_06/examples/dangling0.cc diff --git a/day5/examples/ranges/dangling1.cc b/chapter_06/examples/dangling1.cc similarity index 100% rename from day5/examples/ranges/dangling1.cc rename to chapter_06/examples/dangling1.cc diff --git a/day5/examples/dist.cc b/chapter_06/examples/dist.cc similarity index 100% rename from day5/examples/dist.cc rename to chapter_06/examples/dist.cc diff --git a/day4/examples/feb.cc b/chapter_06/examples/feb.cc similarity index 100% rename from day4/examples/feb.cc rename to chapter_06/examples/feb.cc diff --git a/day5/examples/format1.cc b/chapter_06/examples/format1.cc similarity index 100% rename from day5/examples/format1.cc rename to chapter_06/examples/format1.cc diff --git a/day5/examples/ranges/gerund.cc b/chapter_06/examples/gerund.cc similarity index 100% rename from day5/examples/ranges/gerund.cc rename to chapter_06/examples/gerund.cc diff --git a/day5/examples/ranges/iota.cc b/chapter_06/examples/iota.cc similarity index 100% rename from day5/examples/ranges/iota.cc rename to chapter_06/examples/iota.cc diff --git a/day4/examples/normal_distribution.cc b/chapter_06/examples/normal_distribution.cc similarity index 100% rename from day4/examples/normal_distribution.cc rename to chapter_06/examples/normal_distribution.cc diff --git a/day4/examples/opt_qsolve.cc b/chapter_06/examples/opt_qsolve.cc similarity index 100% rename from day4/examples/opt_qsolve.cc rename to chapter_06/examples/opt_qsolve.cc diff --git a/day4/examples/pass1.cc b/chapter_06/examples/pass1.cc similarity index 100% rename from day4/examples/pass1.cc rename to chapter_06/examples/pass1.cc diff --git a/day4/examples/poisson.cc b/chapter_06/examples/poisson.cc similarity index 100% rename from day4/examples/poisson.cc rename to chapter_06/examples/poisson.cc diff --git a/day5/examples/polymorphic/1_virtual/CMakeLists.txt b/chapter_06/examples/polymorphic/1_virtual/CMakeLists.txt similarity index 100% rename from day5/examples/polymorphic/1_virtual/CMakeLists.txt rename to chapter_06/examples/polymorphic/1_virtual/CMakeLists.txt diff --git a/day5/examples/polymorphic/1_virtual/Circle.cc b/chapter_06/examples/polymorphic/1_virtual/Circle.cc similarity index 100% rename from day5/examples/polymorphic/1_virtual/Circle.cc rename to chapter_06/examples/polymorphic/1_virtual/Circle.cc diff --git a/day5/examples/polymorphic/1_virtual/Circle.hh b/chapter_06/examples/polymorphic/1_virtual/Circle.hh similarity index 100% rename from day5/examples/polymorphic/1_virtual/Circle.hh rename to chapter_06/examples/polymorphic/1_virtual/Circle.hh diff --git a/day5/examples/polymorphic/1_virtual/Point.cc b/chapter_06/examples/polymorphic/1_virtual/Point.cc similarity index 100% rename from day5/examples/polymorphic/1_virtual/Point.cc rename to chapter_06/examples/polymorphic/1_virtual/Point.cc diff --git a/day5/examples/polymorphic/1_virtual/Point.hh b/chapter_06/examples/polymorphic/1_virtual/Point.hh similarity index 100% rename from day5/examples/polymorphic/1_virtual/Point.hh rename to chapter_06/examples/polymorphic/1_virtual/Point.hh diff --git a/day5/examples/polymorphic/1_virtual/Polygon.hh b/chapter_06/examples/polymorphic/1_virtual/Polygon.hh similarity index 100% rename from day5/examples/polymorphic/1_virtual/Polygon.hh rename to chapter_06/examples/polymorphic/1_virtual/Polygon.hh diff --git a/day5/examples/polymorphic/1_virtual/Shape.hh b/chapter_06/examples/polymorphic/1_virtual/Shape.hh similarity index 100% rename from day5/examples/polymorphic/1_virtual/Shape.hh rename to chapter_06/examples/polymorphic/1_virtual/Shape.hh diff --git a/day5/examples/polymorphic/1_virtual/Triangle.cc b/chapter_06/examples/polymorphic/1_virtual/Triangle.cc similarity index 100% rename from day5/examples/polymorphic/1_virtual/Triangle.cc rename to chapter_06/examples/polymorphic/1_virtual/Triangle.cc diff --git a/day5/examples/polymorphic/1_virtual/Triangle.hh b/chapter_06/examples/polymorphic/1_virtual/Triangle.hh similarity index 100% rename from day5/examples/polymorphic/1_virtual/Triangle.hh rename to chapter_06/examples/polymorphic/1_virtual/Triangle.hh diff --git a/day5/examples/polymorphic/1_virtual/main.cc b/chapter_06/examples/polymorphic/1_virtual/main.cc similarity index 100% rename from day5/examples/polymorphic/1_virtual/main.cc rename to chapter_06/examples/polymorphic/1_virtual/main.cc diff --git a/day5/examples/polymorphic/2_any/CMakeLists.txt b/chapter_06/examples/polymorphic/2_any/CMakeLists.txt similarity index 100% rename from day5/examples/polymorphic/2_any/CMakeLists.txt rename to chapter_06/examples/polymorphic/2_any/CMakeLists.txt diff --git a/day5/examples/polymorphic/2_any/Circle.cc b/chapter_06/examples/polymorphic/2_any/Circle.cc similarity index 100% rename from day5/examples/polymorphic/2_any/Circle.cc rename to chapter_06/examples/polymorphic/2_any/Circle.cc diff --git a/day5/examples/polymorphic/2_any/Circle.hh b/chapter_06/examples/polymorphic/2_any/Circle.hh similarity index 100% rename from day5/examples/polymorphic/2_any/Circle.hh rename to chapter_06/examples/polymorphic/2_any/Circle.hh diff --git a/day5/examples/polymorphic/2_any/Point.cc b/chapter_06/examples/polymorphic/2_any/Point.cc similarity index 100% rename from day5/examples/polymorphic/2_any/Point.cc rename to chapter_06/examples/polymorphic/2_any/Point.cc diff --git a/day5/examples/polymorphic/2_any/Point.hh b/chapter_06/examples/polymorphic/2_any/Point.hh similarity index 100% rename from day5/examples/polymorphic/2_any/Point.hh rename to chapter_06/examples/polymorphic/2_any/Point.hh diff --git a/day5/examples/polymorphic/2_any/Polygon.hh b/chapter_06/examples/polymorphic/2_any/Polygon.hh similarity index 100% rename from day5/examples/polymorphic/2_any/Polygon.hh rename to chapter_06/examples/polymorphic/2_any/Polygon.hh diff --git a/day5/examples/polymorphic/2_any/Triangle.cc b/chapter_06/examples/polymorphic/2_any/Triangle.cc similarity index 100% rename from day5/examples/polymorphic/2_any/Triangle.cc rename to chapter_06/examples/polymorphic/2_any/Triangle.cc diff --git a/day5/examples/polymorphic/2_any/Triangle.hh b/chapter_06/examples/polymorphic/2_any/Triangle.hh similarity index 100% rename from day5/examples/polymorphic/2_any/Triangle.hh rename to chapter_06/examples/polymorphic/2_any/Triangle.hh diff --git a/day5/examples/polymorphic/2_any/main.cc b/chapter_06/examples/polymorphic/2_any/main.cc similarity index 100% rename from day5/examples/polymorphic/2_any/main.cc rename to chapter_06/examples/polymorphic/2_any/main.cc diff --git a/day5/examples/polymorphic/3_variant/CMakeLists.txt b/chapter_06/examples/polymorphic/3_variant/CMakeLists.txt similarity index 100% rename from day5/examples/polymorphic/3_variant/CMakeLists.txt rename to chapter_06/examples/polymorphic/3_variant/CMakeLists.txt diff --git a/day5/examples/polymorphic/3_variant/Circle.cc b/chapter_06/examples/polymorphic/3_variant/Circle.cc similarity index 100% rename from day5/examples/polymorphic/3_variant/Circle.cc rename to chapter_06/examples/polymorphic/3_variant/Circle.cc diff --git a/day5/examples/polymorphic/3_variant/Circle.hh b/chapter_06/examples/polymorphic/3_variant/Circle.hh similarity index 100% rename from day5/examples/polymorphic/3_variant/Circle.hh rename to chapter_06/examples/polymorphic/3_variant/Circle.hh diff --git a/day5/examples/polymorphic/3_variant/Point.cc b/chapter_06/examples/polymorphic/3_variant/Point.cc similarity index 100% rename from day5/examples/polymorphic/3_variant/Point.cc rename to chapter_06/examples/polymorphic/3_variant/Point.cc diff --git a/day5/examples/polymorphic/3_variant/Point.hh b/chapter_06/examples/polymorphic/3_variant/Point.hh similarity index 100% rename from day5/examples/polymorphic/3_variant/Point.hh rename to chapter_06/examples/polymorphic/3_variant/Point.hh diff --git a/day5/examples/polymorphic/3_variant/Polygon.hh b/chapter_06/examples/polymorphic/3_variant/Polygon.hh similarity index 100% rename from day5/examples/polymorphic/3_variant/Polygon.hh rename to chapter_06/examples/polymorphic/3_variant/Polygon.hh diff --git a/day5/examples/polymorphic/3_variant/Triangle.cc b/chapter_06/examples/polymorphic/3_variant/Triangle.cc similarity index 100% rename from day5/examples/polymorphic/3_variant/Triangle.cc rename to chapter_06/examples/polymorphic/3_variant/Triangle.cc diff --git a/day5/examples/polymorphic/3_variant/Triangle.hh b/chapter_06/examples/polymorphic/3_variant/Triangle.hh similarity index 100% rename from day5/examples/polymorphic/3_variant/Triangle.hh rename to chapter_06/examples/polymorphic/3_variant/Triangle.hh diff --git a/day5/examples/polymorphic/3_variant/main.cc b/chapter_06/examples/polymorphic/3_variant/main.cc similarity index 100% rename from day5/examples/polymorphic/3_variant/main.cc rename to chapter_06/examples/polymorphic/3_variant/main.cc diff --git a/day5/examples/polymorphic/4_type_erasure/CMakeLists.txt b/chapter_06/examples/polymorphic/4_type_erasure/CMakeLists.txt similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/CMakeLists.txt rename to chapter_06/examples/polymorphic/4_type_erasure/CMakeLists.txt diff --git a/day5/examples/polymorphic/4_type_erasure/Circle.cc b/chapter_06/examples/polymorphic/4_type_erasure/Circle.cc similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/Circle.cc rename to chapter_06/examples/polymorphic/4_type_erasure/Circle.cc diff --git a/day5/examples/polymorphic/4_type_erasure/Circle.hh b/chapter_06/examples/polymorphic/4_type_erasure/Circle.hh similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/Circle.hh rename to chapter_06/examples/polymorphic/4_type_erasure/Circle.hh diff --git a/day5/examples/polymorphic/4_type_erasure/Point.cc b/chapter_06/examples/polymorphic/4_type_erasure/Point.cc similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/Point.cc rename to chapter_06/examples/polymorphic/4_type_erasure/Point.cc diff --git a/day5/examples/polymorphic/4_type_erasure/Point.hh b/chapter_06/examples/polymorphic/4_type_erasure/Point.hh similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/Point.hh rename to chapter_06/examples/polymorphic/4_type_erasure/Point.hh diff --git a/day5/examples/polymorphic/4_type_erasure/Polygon.hh b/chapter_06/examples/polymorphic/4_type_erasure/Polygon.hh similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/Polygon.hh rename to chapter_06/examples/polymorphic/4_type_erasure/Polygon.hh diff --git a/day5/examples/polymorphic/4_type_erasure/Triangle.cc b/chapter_06/examples/polymorphic/4_type_erasure/Triangle.cc similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/Triangle.cc rename to chapter_06/examples/polymorphic/4_type_erasure/Triangle.cc diff --git a/day5/examples/polymorphic/4_type_erasure/Triangle.hh b/chapter_06/examples/polymorphic/4_type_erasure/Triangle.hh similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/Triangle.hh rename to chapter_06/examples/polymorphic/4_type_erasure/Triangle.hh diff --git a/day5/examples/polymorphic/4_type_erasure/main.cc b/chapter_06/examples/polymorphic/4_type_erasure/main.cc similarity index 100% rename from day5/examples/polymorphic/4_type_erasure/main.cc rename to chapter_06/examples/polymorphic/4_type_erasure/main.cc diff --git a/day4/examples/seqops.cc b/chapter_06/examples/seqops.cc similarity index 100% rename from day4/examples/seqops.cc rename to chapter_06/examples/seqops.cc diff --git a/day5/examples/ranges/seqops_range.cc b/chapter_06/examples/seqops_range.cc similarity index 100% rename from day5/examples/ranges/seqops_range.cc rename to chapter_06/examples/seqops_range.cc diff --git a/day5/examples/spans/main.cc b/chapter_06/examples/spans/main.cc similarity index 100% rename from day5/examples/spans/main.cc rename to chapter_06/examples/spans/main.cc diff --git a/day5/examples/spans/spanfunc.cc b/chapter_06/examples/spans/spanfunc.cc similarity index 100% rename from day5/examples/spans/spanfunc.cc rename to chapter_06/examples/spans/spanfunc.cc diff --git a/day5/examples/spans/spanfunc.hh b/chapter_06/examples/spans/spanfunc.hh similarity index 100% rename from day5/examples/spans/spanfunc.hh rename to chapter_06/examples/spans/spanfunc.hh diff --git a/day4/examples/test.dat b/chapter_06/examples/test.dat similarity index 100% rename from day4/examples/test.dat rename to chapter_06/examples/test.dat diff --git a/day5/examples/ranges/trig_views.cc b/chapter_06/examples/trig_views.cc similarity index 100% rename from day5/examples/ranges/trig_views.cc rename to chapter_06/examples/trig_views.cc diff --git a/day5/examples/ranges/trig_views2.cc b/chapter_06/examples/trig_views2.cc similarity index 100% rename from day5/examples/ranges/trig_views2.cc rename to chapter_06/examples/trig_views2.cc diff --git a/day4/examples/variant_0.cc b/chapter_06/examples/variant_0.cc similarity index 100% rename from day4/examples/variant_0.cc rename to chapter_06/examples/variant_0.cc diff --git a/day4/examples/variant_1.cc b/chapter_06/examples/variant_1.cc similarity index 100% rename from day4/examples/variant_1.cc rename to chapter_06/examples/variant_1.cc diff --git a/day4/examples/variant_2.cc b/chapter_06/examples/variant_2.cc similarity index 100% rename from day4/examples/variant_2.cc rename to chapter_06/examples/variant_2.cc diff --git a/day4/examples/variant_3.cc b/chapter_06/examples/variant_3.cc similarity index 100% rename from day4/examples/variant_3.cc rename to chapter_06/examples/variant_3.cc diff --git a/day4/examples/variant_4.cc b/chapter_06/examples/variant_4.cc similarity index 100% rename from day4/examples/variant_4.cc rename to chapter_06/examples/variant_4.cc diff --git a/day5/examples/ranges/views_and_span.cc b/chapter_06/examples/views_and_span.cc similarity index 100% rename from day5/examples/ranges/views_and_span.cc rename to chapter_06/examples/views_and_span.cc diff --git a/day4/examples/weighted_die.cc b/chapter_06/examples/weighted_die.cc similarity index 100% rename from day4/examples/weighted_die.cc rename to chapter_06/examples/weighted_die.cc diff --git a/day4/examples/word_count.cc b/chapter_06/examples/word_count.cc similarity index 100% rename from day4/examples/word_count.cc rename to chapter_06/examples/word_count.cc diff --git a/day5/examples/word_count.cc b/chapter_06/examples/word_count_fmt.cc similarity index 100% rename from day5/examples/word_count.cc rename to chapter_06/examples/word_count_fmt.cc diff --git a/day4/examples/K.cc b/chapter_06/solutions/K.cc similarity index 100% rename from day4/examples/K.cc rename to chapter_06/solutions/K.cc diff --git a/chapter_06/solutions/normal_distribution.cc b/chapter_06/solutions/normal_distribution.cc new file mode 100644 index 0000000..724bbf1 --- /dev/null +++ b/chapter_06/solutions/normal_distribution.cc @@ -0,0 +1,30 @@ +#include <iostream> +#include <map> +#include <random> + +auto main() -> int +{ + double mn{0.}, var{1.}; + std::cout << "Mean : "; + std::cin >> mn; + std::cout << "Variance: "; + std::cin >> var; + + std::mt19937_64 engine{ std::random_device{}() }; + // Above: First create an object of the type std::random_device. + // random_device objects can be called like functions. + // They give you non-deterministic random numbers, but + // not necessarily of the best mathematical quality. + // We create the object, and immediately call it: + // which is why we have the () after the empty initialiser + // braces {}. This gives us a random unsigned long + // integer, which we then use to seed the Mersenne Twister + // engine. + std::normal_distribution<> dist{mn, std::sqrt(var) }; + auto generator = [&]{ return dist(engine); }; + std::map<int, unsigned> H; + for (unsigned i = 0; i < 5000000; ++i) + H[static_cast<int>(std::floor(generator()))]++; + for (auto& i : H) + std::cout << i.first << " " << i.second << "\n"; +} diff --git a/chapter_06/solutions/weighted_die.cc b/chapter_06/solutions/weighted_die.cc new file mode 100644 index 0000000..f6980f4 --- /dev/null +++ b/chapter_06/solutions/weighted_die.cc @@ -0,0 +1,15 @@ +#include <array> +#include <iostream> +#include <random> + +auto main() -> int +{ + std::array freq{ 0.3, 0.2, 0.2, 0.1, 0.1, 0.1 }; + std::mt19937_64 engine{ std::random_device{}() }; + std::discrete_distribution dist{freq.begin(), freq.end()}; + auto gen = [&]{ return dist(engine); }; + for (unsigned long i = 0; i < 10'000'000; ++i) + freq[gen()]++; + for (unsigned f : freq) + std::cout << f << std::endl; +} -- GitLab