Monday 13 February 2023

Chapter 26 // Drill 4 - Principles & Practice Using C++

In this exercise I'm using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 26 // Drill 4
Repeat these tests for sequences of strings, such as { Bohr Darwin Einstein Lavoisier Newton Turing }.

Instead of writing out the tests myself (or getting a load of random strings and writing them out to a file), I googled " how to make random string" and used this code to create a randString() to go with randint().

They're garbled messes but whatever. I then made the original Test struct templated so the value could be any type. This required making all the functions using Test to also be templated. 

I also had to slightly change the istream operator >> as now we're reading in strings, reading '}' into a string won't cause the input stream to fail (as it's a valid character for a string). So, in that case, I put the character back into the stream and continue.

Along with the random tests, I made a small test file to see some working examples. Doing this showed me I forgot to sort the container of sequences before doing the binary search....

Sunday 12 February 2023

Chapter 26 // Drill 3 - Principles & Practice Using C++

In this exercise I'm using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 26 // Drill 3

Based on section 26.3.1.3, complete a program that generates
a. A very large sequence (what would you consider very large, and why?)
b. Ten sequences with a random number of elements.
c. Ten sequences with 0, 1, 2 ... 9 random elements (but still ordered).

I used the sample function from p1000 to create all the tests and wrote them out to a text file. I was surprised that all of my tests failed. I then realised that in his make_test function, he starts the value to search from is n which is an argument parameter (the one we pass in to determine the size of the sequence). So I changed the function up a bit. Another gotcha from Bjarne.