Monday 1 April 2024

Chapter 5 // Exercises 1-5 - The C++ Programming Language

For this exercise I'm using Visual Studio Community 2022 and the header file std_lib_facilities:

The exercises can be found online and are not actually in the book:

Chapter 5 - A Tour of C++: Concurrency and Utilities
Exercise 1
When first reading this chapter, keep a record of information that was new or surprising to you. Later, use that list to focus your further studies.

1. Threading is still an area I don't really do all that much with. I'm aware of race conditions (and the need to ensure game code is secure for multi-threaded code) but I don't touch on it every day.
2. Async sounds particularly useful. I've never used that in my own projects yet.

Exercise 2
Write a program with two threads: one that writes hello every second and one that writes world! every second.


It's a bit mangled but does what the task asks.

Exercise 3
Time a loop. Write out the time in milliseconds. Do this for the default setting of your compiler and for a setting using an optimiser (e.g., -O2 or "release"). Be careful not to have the optimiser eliminate your whole loop as dead code because you did not use a result.


I used a function from an exercise from P&P (there's a link in the code) where it adds things to a map and keeps it ordered. Debug took 8272ms and Release took 740ms. I'm always impressed at just how good the compiler is at optimising code.

Exercise 4
Repeat the histogram drawing from example from section 5.6.3 for a normal_distribution and 30 rows.


This was a sneaky one. Normal distribution will produce negative numbers. Also, you can only use float, double or real with a normal_distribution. It also taught me about standard deviations.

Exercise 5
Use a regex to find all decimal numbers in a file.

No comments:

Post a Comment