Pages

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.

Friday, 29 March 2024

Chapter 4 // Exercises 1-10 - 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 4 - A Tour of C++: Containers and Algorithms
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. I didn't know that for_each() is classed as an algorithm. The more you know.

Exercise 2
List 5 standard library containers.

1. std::vector
2. std::array
3. std::map
4. std::deque
5. std::list

Exercise 3
List 5 standard library algorithms.

1. for_each()
2. find()
3. search()
4. copy()
5. remove()

Exercise 4
List 5 standard library headers.

1. #include <string>
2. #include <list>
3. #include <iostream>
4. #include <vector>
5. #include <random>

Exercise 5
Write a program that reads a name (a string) and an age (an int) from the standard input stream cin. Then output a message including the name and age to the standard output stream cout.


Exercise 6
Redo exercise 5, storing several (name, age) pairs in a class. Doing the reading and writing using your own >> and << operators.


Exercise 7
Initialise a vector<int> with the elements 5, 9, -1, 200 and 0. Print it. Sort it, and print it again.


Exercise 8
Repeat exercise 7 with a vector<string> initialised with "Kant", "Plato", "Aristotle", "Kierkegaard", and "Hume".


Exercise 9
Open a file for writing (as an ofstream) and write a few hundred integers to it.


Exercise 10
Open the file of integers from exercise 9 for reading (as an ifstream) and read it.













Thursday, 28 March 2024

Chapter 3 // Exercise 1, 2 - The C++ Programming Language

For this exercise I'm using Visual Studio Community 2022 and the header file std_lib_facilities:
https://github.com/l-paz91/TheCppProgrammingLanguage/blob/main/std_lib_facilities.h

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

Chapter 3 - A Tour of C++: Abstraction Mechanisms
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.

A concrete type means something that can be instantiated/created. Built-in is something directly supported by the language, therefore something like a Vector is a concrete but not built-in as it relies on the specific library you're using for it's definition. New comp sci words were learnt today.

Or built-in refers to primitive types, anything that becomes user-defined (even if it's using primitive types) becomes concrete but not built-in. This was actually quite confusing to wrap my head around.

Exercise 2
Give five examples of concrete types that are built-in types in C++. Give five examples of concrete types that are not built-in types in C++.

Concrete types that are built-in (no outside library/definitions needed):
1. Int
2. Double
3. Char
4. Bool
5. Float

Concrete types that are not built-in:
1. std::vector
2. std::string
3. enum
4. class/struct
5. std::list

Wednesday, 21 February 2024

Chapter 2 // Exercise 1, 2, 3, 4, 5, 6, 7 - The C++ Programming Language


I bought this book a long time ago. The current edition was printed in 2013 and features up to C++11. On his website, Bjarne said he's never felt a need to update it but with all the new features in C++17 and C++20, I feel a 5th edition should be on the horizon and I'm wondering if it'll get released when I'm halfway through this one.

But either way. Welcome to this post featuring the exercises starting at chapter 2. I've done it this way as the first chapter was just writing exercises. 

Unlike Principles & Practice, the exercises can be found online and are not actually in the book:

Chapter 2 - A Tour of C++: The Basics
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

Will do.

Exercise 2
What does a compiler do? What does a linker do?

A compiler processes the source text from a project and turns it into object files. The linker turns the object files into an executable.

Exercise 3
Get the ‘‘Hello, world!’’ program (§2.2.1) to run. This is not an exercise in programming. It is an exercise to test your use of your edit-compile-link-execute tool chain.


Hello, world my old friend. I'm using Visual Studio Community 2022 with C++20 enabled. I'm also using my modified version of "std_lib_facilities" that's given in Principles & Practice. It can be found here:

Exercise 4
List three (or more) C++ compilers

GNU GCC, Clang, Intel C++ Compiler

Exercise 5
Write out a bool, a char, an int, a double, and a string.


Exercise 6
Read in a bool, a char, an int, a double, and a string.


Exercise 7
What is an invariant and what good might it do?

From what I can understand from Section 2.4.3.2 an invariant is a way of checking or asserting that some logic on class member variables is correct. So if you have an int member variable and that can only be initialised with positive numbers, the constructor would throw an error if you passed a negative number to it. There are other ways to enforce these "conditions" such as subscript operators checking that the value passed to it is within range of the size of the container.

Friday, 9 February 2024

Dad's Demo Disc Collection // Episode 1 - Planet PC Issue 6


So my dad passed away over a year ago now unexpectedly from a heart attack. I was very close to him and the past year has been tough, especially since my dad was a hoarder. It's taken me just over a year to finally clear out his house and get it sold. I lived with him for a long time before finally moving out in 2019 and after that the hoarding got exponentially worse without me throwing things out.

Amongst the hoard there was a lot of crap but there was also a lot of very interesting things. Dad was very big on PCs throughout the 80s and 90s and would make a lot of his own programs in DBase or Clipper. He was very much sold on DOS and by the time Windows 3.1 came around he was too set in his ways and hated it but kept buying PC Magazines until the early 2000s when he eventually lost interest in computing all together. 

Back then, PC Magazines always came with a disk each month with software demos on it, or fonts, clip art, games etc. I remember some of these disks as he would keep them in his shed and I would pick up a handful and go through them one by one on the family computer that was kept in the dining room.

I've found more boxes of disks than I can remember him having, and there's a lot of interesting things on these disks and I thought it would be a nice project to archive every single disk and upload the contents to internet archive and document that journey.

So welcome, to the first episode of Dad's Demo Disc Collection. I started off with a random box and took out the first disk which just so happened to be:
  • Planet PC Issue 6 - 19th April 2000
I eventually got the software working by using PCem: 

A full blown pc emulator. I ended up emulating a Pentium II computer running Voodoo 3 and Windows98. And I did that by following this great tutorial by PhilsComputerLab: 

You can find a zip of the disc here on Internet Archive: 

I especially enjoyed my first encounter with Sierra Home. I look forward to finding other Sierra Software.

Enjoy.