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. 

Saturday, 21 January 2023

Chapter 26 // Drill 2 - 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 2

Complete a file of tests for the sequences from section 26.3.
a. { 1 2 3 5 8 13 21 }                 // an ordinary sequence
b. { }                                          // an empty sequence
c. { 1 }                                       // just one element
d. { 1 2 3 4 }                             // even number of elements
e. { 1 2 3 4 5}                           // odd number of elements
f. {1 1 1 1 1 1 1  }                     // all elements equal
g. { 0 1 1 1 1 1 1 1 1 1 1 1 }     // different element at beginning
h. { 0 0 0 0 0 0 0 0 0 0 0 0 1 }   // different element at end

The one thing that tripped me up was the for loop in testAll. I forgot to clear the Test struct each time, so it's sequence vector always had the incorrect numbers in it!

Friday, 20 January 2023

Chapter 26 // Drill 1 - 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 1

Get the test of binary_search to run:
Implement the input operator for Test from section 26.3.2.2

This didn't take too long. I love stringstreams and I'm so glad someone on the C++ committee added them to the standard.

Monday, 5 December 2022

Chapter 25 // Exercise 17 - 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 25 // Exercise 17

In section 25.4.3-4 we provided a class Array_ref claimed to make access to elements of an array simpler and safer. In particular, we claimed to handle inheritance correctly. Try a variety of ways to get a Rectangle* into a vector<Circle*> using an Array_ref<Shape*> but no casts or other operations involving undefined behaviour. This ought to be impossible.

Github: N/A

Wow it's been a hot minute since I last looked at the FLTK code. I would avoid using arrays entirely but this little class is handy. There's no code for this one as none of it compiles anyway.

And with that I'm onto the second to last chapter of the book...it feels strange. What will I do without the exercises in this book hanging over my head?