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