Showing posts with label chapter 26 drill. Show all posts
Showing posts with label chapter 26 drill. Show all posts

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.