Showing posts with label drills. Show all posts
Showing posts with label drills. Show all posts

Thursday, 20 July 2023

Chapter 27 // Drill 1, 2, 3 - Principles & Practice Using C++

In this exercise I'm using Visual Studio 2019 and ISO C11 Standard.

Chapter 27 // Drill 1

Write a "Hello, World!" program in C, compile it, and run it.

The last time I did this, I used Visual Studio Code and had to set up my own build script and call it on the command line. I'm much lazier now and I'm just using Visual Studio Community.


Chapter 27 // Drill 2

Define two variables holding "Hello" and "World!" respectively; concatenate them with a space in between; and output them as Hello World!

I was a bit embarrassed that it took me a good 25 minutes to do this exercise lol. I got to "define 2 variables" and was like "how do string again??" I also got caught out by the missing terminating nulls and using strcpy instead of strncpy.


Chapter 27 // Drill 3

Define a C function that takes a char* parameter p and an int parameter x and print out their values in this format: p is "foo" and x is 7. Call it with a few argument pairs.

A little bit more what I'm used to. I absolutely despise that Unreal Engine uses printf so prolifically. I love cout << and I'm not afraid to say it.





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, 20 June 2022

Chapter 25 // Drill 3, 4, 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Drill 3

Using hexadecimal literals, define short unsigned ints with:
  • Every bit set
  • The lowest (least significant bit) set
  • The highest (most significant bit) set
  • The lowest byte set
  • The highest byte set
  • Every second bit set (and the lowest bit 1)
  • Every second bit set (and the lowest bit 0)

Chapter 25 // Drill 4

Print each as a decimal and as a hexadecimal.

Chapter 25 // Drill 5

Do 3 and 4 using bit manipulation operations (|, &, <<) and (only) the literals 1 and 0.


I cheated with drill 3 and just wrote out the binary then converted it to hex using 

5 was annoying because you can directly type out binary literals in C++14 and assign them to things. The challenge came from only using 1 and 0. Like setting the highest byte can be done with 1 << 15 but 15 isn't 1 or 0.

I honestly had no idea how to do the last 2 using only 1 and 0, so I cheated and just used direct binary strings.


Sunday, 19 June 2022

Chapter 25 // Drill 1, 2 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Drill 1

Run this:
int v = 1; for(int i = 0; i < sizeof(v)*8; ++i) { cout << v << ' '; v << =1; }

I've never seen <<= before. But shifting to the left by 1 is the "cheapest" and "fastest" way to multiply by 2. In some very low-level engine code at work, I often see >> 1 to divide by 2 instead of / 2, however it's not 1995 anymore and Visual Studio will convert divides/multiplies that are powers of 2 to an equivalent shift for you.

The last number will wrap as it's 1 over the max limit of a signed int.

Chapter 25 // Drill 2

Run that again with v declared to be an unsigned int.


.

Monday, 6 June 2022

Chapter 24 // Drill 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 8

Read six ints into a Matrix<int,2> m(2,3) and print them out.


.

Sunday, 5 June 2022

Chapter 24 // Drill 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 7

Read ten complex<double>s from cin (yes, cin supports >>  for complex) and put them into a Matrix. Calculate and output the sum of the ten complex numbers.


Complex does have built in I/O support which is nice. Figuring out how to use cin was annoying though. I eventually discovered it here:

You need to input it as (num,num). 

Saturday, 4 June 2022

Chapter 24 // Drill 6 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 6

Compute a multiplication table for [0,n) * [0, m) and represent it as a 2D Matrix. Take n and from cin and print out the table nicely (assume that m is small enough that the results fit on a line).


.

Friday, 3 June 2022

Chapter 24 // Drill 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 5

Read ten floating-point values from input and put them into a Matrix<double>. Matrix has no push_back() so be careful to handle an attempt to enter a wrong number of doubles. Print out the Matrix.


The accompanying MatixIO.h file provides very simple I/O for 1D and 2D matrices. So
Matrix<double> a(4);
cin >> a;
cout << a;

Will read 4 whitespace-seperate doubles delimited by curly braces. For example with a 1D matrix:
{ 1.2 3.4 5.6 7.8 }

These functions will handle reading in the correct amount of numbers and throw errors if the structure isn't exact.

I also added my own method for fun.

Thursday, 2 June 2022

Chapter 24 // Drill 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 4

Write a program that takes ints from cin and outputs the sqrt() of each int, or "no square root" if sqrt(x) is illegal for some x (i.e., check your sqrt() return values).


So the std::sqrt function is explained here:

I didn't realise it returned floats, doubles or long doubles. It returns some error handles if something goes wrong defined here:


Wednesday, 1 June 2022

Chapter 24 // Drills 1, 2, 3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 1

Print the size of a char, a short, an int, a long, a float, a double, an int*, and a double* (use sizeof, not <limits>).

Chapter 24 // Drill 2

Print out the size as reported by sizeof of Matrix<int> a(10), Matrix<int> b(100), Matrix<double> c(10), Matrix<int, 2> d(10,10), Matrix<int, 3> e(10,10,10).

Chapter 24 // Drill 3

Print out the number of elements of each of the Matrixes from 2.


Dear god, reading this chapter almost sent me to sleep several times...there's just something about matrices that bores the shit out of me.

Matrix.h is a file made up by Bjarne. You can find a copy of it in here:
or here:

The ones with 11 on them I think are for the second edition of the book??




Thursday, 6 January 2022

Chapter 23 // Drills 1,2,3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Drill 1

Find out if regex is shipped as part of your standard library. Hint: Try std::regex and tr1::regex.


For this I used the example posted on ccp reference for std::regex:

I've set Visual Studio to use the ISO C++17 Standard.

Chapter 23 // Drill 2, 3

Get the little program from section 23.7 to work; that may involve figuring out how to set the project and/or command-line options to link to the regex library and use the regex headers.

Use the program from drill 2 to test the patterns from section 23.7.


Fortunately, I'm doing these exercises far in the future where it's been a part of the standard for many years.

Tuesday, 28 September 2021

Chapter 21 // Drill 3 - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 3.1

Read some floating-point values (at least 16 values) from a file into a vector<doubles> called vd.

Chapter 21 // Drill 3.2

Output vd to cout.

Chapter 21 // Drill 3.3

Make a vector vi of type vector<int> with the same number of elements as vd; copy the elements from vd into vi.

Chapter 21 // Drill 3.4

Output the pairs of (vd[i], vi[i]) to cout, one pair per line.

Chapter 21 // Drill 3.5

Output the sum of the elements of vd.

Chapter 21 // Drill 3.6

Output the difference between the sum of the elements of vd and the sum of the elements of vi.

Chapter 21 // Drill 3.7

There is a standard library algorithm called reverse that takes a sequence (pair of iterators) as arguments; reverse vd, and output vd to cout.

Chapter 21 // Drill 3.8

Compute the mean value of the elements in vd; output it.

Chapter 21 // Drill 3.9

Make a new vector<double> called vd2 and copy all elements of vd with values lower than (less than) the mean into vd2.

Chapter 21 // Drill 3.10

Sort vd, output it again.


Monday, 27 September 2021

Chapter 21 // Drill 2 - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 2.1

Define a map<string, int> called msi.

Chapter 21 // Drill 2.2

Insert ten (name, value) pairs into it, e.g., msi["lecture"]=21.

Chapter 21 // Drill 2.3

Output the (name, value) pairs to cout in some format of your choice.

Chapter 21 // Drill 2.4

Erase the (name, value) pairs from msi.

Chapter 21 // Drill 2.5

Write a function that reads value pairs from cin and places them in msi.

Chapter 21 // Drill 2.6

Read 10 pairs from input and enter them into msi.

Chapter 21 // Drill 2.7

Write the elements of msi to cout.

Chapter 21 // Drill 2.8

Output the sum of the (integer) values in msi.

Chapter 21 // Drill 2.9

Define a map<int, string> called mis.

Chapter 21 // Drill 2.10

Enter the values from msi into mis; that is, if msi has an element ("lecture",21), mis should have an element (21, "lecture").

Chapter 21 // Drill 2.11

Output the elements of mis to cout.


For Drill 2.4 I wasn't sure if he meant erase a few or erase all so I went with erase all.

With Drill 2.7 I think he meant create an overload of the output operator for the map as creating a print function has already been done.

Whilst doing 2.9 I decided to make my functions more generic so they could accept maps of almost any type.

Sunday, 26 September 2021

Chapter 21 // Drill 1 - 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 1.8

Repeat the exercise with a list<Item> rather than a vector<Item>.


I had to do two minimal things to get this work;
1 - Use advance instead of It + num.
2 - Use the sort() provided by the list container instead of just the standard one.

Saturday, 25 September 2021

Chapter 21 // Drill 1 - 1, 2, 3, 4, 5, 6, 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 1.1

Define struct Item { string name; int iid; double value; /* ... */};, make a vector<Item>, vi, and fill it with ten items from a file.

Chapter 21 // Drill 1.2

Sort vi by name.

Chapter 21 // Drill 1.3

Sort vi by iid.

Chapter 21 // Drill 1.4

Sort vi by value; print it in order of decreasing value (i.e., largest value first).

Chapter 21 // Drill 1.5

Insert Item{"horse shoe", 99, 12.34} and Item{"Canon S400", 9988, 499.95}.

Chapter 21 // Drill 1.6

Remove (erase) two Items identified by name from vi.

Chapter 21 // Drill 1.7

Remove (erase) two Items identified by iid from vi.

Github: 

New things all around in this exercise. I used the code from page 791 to start with and added operator>> to Item so I didn't have to modify it. This worked perfectly and vector happily constructed 10 Items using the data from the text file thanks to the operator overload. The input code is a bit hardcoded however it allows Items to have spaces in the name value as it uses getline().

For the second drill I tried my hand at creating a function object to sort the name. They are very easy to use.

For sorting by value I allowed a bool to passed to the function object which allows it to switch between increasing/decreasing order.

Whilst doing Drill 6 I also found out that std::remove_if will erase a member of a custom object when passed a predicate to find that member; very cool.

Wednesday, 17 March 2021

Chapter 20 // Drill 6, 7, 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2019 and a modified version of the std_lib_facilities header found here.

Drill 6

Write a simple copy() operation,
template<typename Iter1, typename Iter2>
    // requires Input_iterator<iter1>() && Output_iterator<Iter2>()
Iter2 copy(Iter f1, Iter e1, Iter f2);
that  copies [f1, e1) to [f2, f2+(e1-f1)) and returns f2+(e1 - f1) just like the standard library copy function. Note that if f1==e1 the sequence is empty, so that there is nothing to copy.

Drill 7

Use your copy() to copy the array into the vector and to copy the list into the array.

Drill 8

Use the standard library function find() to see if the vector contains the value 3 and print out its position if it does; use find() to see if the list contains the value 27 and print out it's position if it does. The "position" of the first element  is 0, the position of the second element is 1, etc. Note that if find() returns the end of the sequence, the value wasn't found.

Github: 

In drill 7 I realised he meant to use a std:: array as a normal C array does not have iterators built in, so I changed it.

I did discover some nice std functions in these drills though. Namely distance() which returns the index of an iterator and find. I'm sure I've used find before but now I actually understand what it's doing "under the hood".

Tuesday, 16 March 2021

Chapter 20 // Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2019 and a modified version of the std_lib_facilities header found here.

Drill 1

Define an array of ints with the ten elements { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.

Drill 2

Define a vector<int> with those ten elements.

Drill 3

Define a list<int> with those ten elements.

Drill 4

Define a second array, vector and list, each initialised as a copy of the first array, vector, and list, respectively.

Drill 5

Increase the value of each element in the array by 2; increase the value of each element in the vector by 3; increase the value of each element in the list by 5.

Github: 

I'm not sure if he meant a user defined array (as seen on pg 748), a std::array or an actual array[]. I went with the last one.