Showing posts with label chapter 21 exercise. Show all posts
Showing posts with label chapter 21 exercise. Show all posts

Friday, 22 October 2021

Chapter 21 // Exercise 15 - 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 // Exercise 15

Provide a GUI for the program from the previous exercise.


Oh this took an hour of just pure ctrl+c ctrl+v slight edit. I'm not surprised the creators of FLTK created Fluid, creating forms is tedious. But I powered through and I'm happy with the result.

FLTK Forms Chapter 21 // Exercise 15 - Principles & Practice Using C++

I did it! I managed not to procrastinate as much with this chapter and got it done in just under a month. With that I'm finally onto a chapter I've been looking forward to.

Thursday, 21 October 2021

Chapter 21 // Exercise 14 - 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 // Exercise 14

Write a program (using the output from the previous exercise) to answer questions such as: 
"How many occurrences of 'ship' are there in a file?"
"Which word occurs most frequently?"
"Which is the longest word in the file?"
"Which is the shortest?"
"List all words starting with 's'"
"List all four letter words."

Github: 

So I know now why he wanted us to replace all punctuation with whitespace. It makes checking for words starting with certain characters a nightmare....it also makes words longer/shorter than they appear.

I didn't make this one interactive as the next one does that.

Wednesday, 20 October 2021

Chapter 21 // Exercise 13 - 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 // Exercise 13

Write a program to "clean up" a text file for use in a word query program; that is, replace punctuation with whitespace, put words into lower case, replace "don't" with "do not" (etc.), and remove plurals (e.g., ships becomes ship. Don't be too ambitious . For example, it is hard to determine plurals in general, so just remove an 's' if you find both ship and ships. use that program on a real-world text file with at least 5000 words (e.g., a research paper).


Another long one. I read question 15 though and was like "please...WHY".

I used this research paper:
Kacmarcik, G. and Kacmarcik, S. G. (2009) ‘Introducing computer programming via gameboy advance homebrew’, in Proceedings of the 40th ACM technical symposium on Computer science education - SIGCSE ’09. New York, New York, USA: ACM Press, p. 281. doi: 10.1145/1508865.1508969. 

It was one of the main papers that helped formed my research question for my Bachelors dissertation. It's only 4320 words....but I think Bjarne will let me off.

This exercise ended up disgustingly hacky and it prints out each word on a new line as he didn't specify any output specifications...just to "clean" it up.

Tuesday, 19 October 2021

Chapter 21 // Exercise 12 - 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 // Exercise 12

Provide a GUI interface for querying a file of Orders; e.g., "Find all orders from Joe," "Find the total value of orders in file Hardware," and "List all orders in file Clothing." Hint: First design a non-GUI interface; then, build the GUI on top of that.

Github: 

So the non-gui version can be found here:

I started to implement searching name, address, date and purchases, however it started taking up too much time and he only says name so I cut it back.

Chapter 21 // Exercise 12 - Principles & Practice Using C++

For the GUI version, I designed a new window called QueryOrderWindow and delved into a new FLTK widget; Fl_Text_Display. This class allows you display multiple lines of text with scrolling capabilities and other things but I only wanted to scroll.

I went over to GUI.h and added a new child of Widget called MultilineScroll_Outbox which creates an FL_Text_Display and Fl_Text_Buffer. You can then simply add text to the buffer. After that it was simply a matter of adding some buttons and pushing text to various places.

FLTK text Display scroll Chapter 21 // Exercise 12 - Principles & Practice Using C++

Sunday, 10 October 2021

Chapter 21 // Exercise 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 // Exercise 11

Provide a GUI interface for entering Orders into files.


I hate that he made this exercise. I also hate the next exercise. I've not used FLTK in so long. I've changed pc's since I last used it and I'm glad I made myself a guide. Having been about 9 months since I last used the fltk solution I had of course forgotten how everything worked. I appreciate though that he makes you do exercises using fltk later on.

I got a bit pedantic with this one but eventually settled on a system that allows you to add purchases and then checkout when you're done. When you checkout it clears everything and allows a new customer to be added. When you quit the program; it prints out all the orders to a text file. Thanks to stringstreams, I didn't need to change any input/output in the actual order file apart from changing ifstream to istream in one place.

FLTK Input Form - Chapter 21 // Exercise 11 - Principles & Practice Using C++

Chapter 21 // Exercise 11 - Principles & Practice Using C++




Saturday, 9 October 2021

Chapter 21 // Exercise 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 // Exercise 10

Compute the total value of the orders in the two files from the previous exercise. The value of an individual Purchase is (of course) its unit_price*count.

Github:

.

Friday, 8 October 2021

Chapter 21 // Exercise 9 - 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 // Exercise 9

Define an Order class with (customer) name, address, data, and vector<Purchase> members. Purchase is a class with a (product) name, unit_price, and count members. Define a mechanism for reading and writing Orders to and from a file. Define a mechanism for printing Orders. Create a file of at least ten Orders, read it into a vector<Order>, sort it by name (customer), and write it back out to a file. Create another file of at least ten Orders of which about a third are the same as in the first file, read it into a list<Order>, sort it by address (of customer), and write it back out to a file. Merge the two files into a third using std::merge().

Github: 

Dear god, what a tricky exercise....or rather I made it tricky for myself. Instead of just using the date class we made way back in Chpater 9 or something I decided to use DateTime. Unfortunately, the new lovely std date class is only available in preview for C++20 right now so I had to use CTime. Figuring out how to parse the dates and convert them was a bit trickier than I thought but I got there in the end and it gives a nice official looking timestamp on the purchase using the pc's time.

Then std::merge() didn't work how I thought it would so that was something new.

Thursday, 7 October 2021

Chapter 21 // Exercise 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 // Exercise 8

Take the word-frequency example from section 21.6.1 and modify it to output its lines in order of frequency (rather than in lexicographical order). An example would be 3: C++ rather than C++: 3.


This one was tricky as you can't really sort a map and you can't insert based on value because if a value becomes the highest frequency then we need to move it to the top. Instead you need to create a second temporary container and add your values to that, sort it how you want and then use the data. I haven't figured out how to put the data back into the map in that order as it will sort by the key when inserting again. Maps are confusing.

Wednesday, 6 October 2021

Chapter 21 // Exercise 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 // Exercise 7

Write a binary search function for a vector<int> (without using the standard one). You can choose any interface you like. Test it. How confident are you that your binary search function is correct? Now write a binary search function for a list<string>. Test it. How much do the two binary search functions resemble each other? How much do you think they would have resembled each other if you had not known about the STL?


This one scared me at first. I think any phrase containing "binary search" scares me. Fortunately, Bjarne explains what a binary search is on pg795 and follows up with an example. I found it interesting that even a container with 10 elements can be searched faster using a binary search than just starting from the beginning.

A binary search basically reminded me of an exercise waaay back in chapter 4 (ex 4) where you had to make a "number guessing game" between 1 and 100 where the program could always guess your number in 7 answers or less due to starting in the middle then adding/subtracting half again until it got to the number.

So, he mentions binary searches expect your container to be sorted and he makes you use a vector and a list. Finding the middle is trivial for both containers however getting an iterator to the middle of a list is trickier than a vector because a list doesn't have random access iterators so you can't do list[middle]. You need to advance to the middle making the "generic" search inefficient for vectors.

I created 3 functions; ones catered specifically for vector and list and then a generic one. They're all pretty similar as I decided to use std::find() to search the containers. If we had to do this exercise much earlier in the book I think they would be extremely hardcoded without much thought to making them generic.

After this exercise I had a look at std::binary_search and was most annoyed with myself because it's 2 lines and uses std::lower_bounds(). My solutions are quite inefficient however they got the job with sorted containers (unsorted can cause problems). I made the generic one recursive as well to help with very large containers.

Tuesday, 5 October 2021

Chapter 21 // Exercise 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 21 // Exercise 6

In the Fruit example in section 21.6.5, we copy Fruits into the set. What if we didn't want to copy the Fruits? We could have a set<Fruit*> instead. However, to do that, we'd have to define a comparison operation for that set. Implement the Fruit example using a set<Fruit*, Fruit_comparison>. Discuss the differences between the two implementations.


This one was pretty simple, just had to change a few &'s to * in places. Set's don't clean up pointers either so you have to make sure that you're cleaning up  the set after use which is easily forgotten...unless you use smart pointers.

Monday, 4 October 2021

Chapter 21 // Exercise 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 21 // Exercise 5

What would we have to do if we couldn't return end() to indicate "not found"? Redesign and re-implement find() and count() to take iterators to the first and last elements. Compare the results to the standard versions.


Erm I think I did this back to front? I didn't return an iterator for count as the standard version doesn't it returns a std::iterator_traits<It>::difference_type, so that's what I did. Also, find() already takes in iterators to the first and last elements. After some thinking I realised he meant the last element not the end (which is 1 past the last element).

With count it's fairly simple as your count integer will just be at 0 to indicate nothing was found. For find, I made the return iterator an output parameter and the function now returns a bool so you can stick it in an if statement. Highly inefficient compared to it's std counterparts.

Sunday, 3 October 2021

Saturday, 2 October 2021

Chapter 21 // Exercise 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 21 // Exercise 3

Implement count() yourself. Test it.


I originally returned a number however I then had a look at the actual implementation of std::count and used difference_type instead:
https://en.cppreference.com/w/cpp/algorithm/count

Friday, 1 October 2021

Chapter 21 // Exercise 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 21 // Exercise 2

Find a reliable source of STL documentation and list every standard library algorithm.

Github: .

So there's quite a lot. Instead here is a link to all the C++ Standard Library Headers:

And here's a link to the algorithm header which inludes all the, well, algorithms:

I love cpp reference as it's well documented and they provide examples of everything in use.

Thursday, 30 September 2021

Chapter 21 // Exercise 1 - 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 // Exercise 1

Go through the chapter and do all Try This exercises that you haven't already done.

Try This Pg 761

Github: 

To see if they were truly logically different, I went into debug mode and then compared the disassembly created.

You can see that Visual Studio produced identical code, however that is in debug mode. In release mode, it was slightly different:
The first implementation is faster and could be more optimised due to the NOP instruction which I believe aligns the loop in "jump chunks" (very brief explanation because I don't fully understand yet) which can help with instruction fetches. Both loops are doing basically the same thing though:
  • setting the conditional jump
  • comparing the contents of the iterator with 4
  • jumping back to the top while the condition is not equal and increasing the iterator
Try This Pg 765

Github: N/A

1. The value could be changed without you knowing, potentially breaking a part of the code.
2. Someone could change the predicate to use a different name value.
3. Someone could try an make the value global to access it in other places; or start using it where they're not supposed to.
I'd hate to see it in any application.

Try This Pg 774

Github: 

.

Try This Pg 785

Github: 

This one confused me a bit as on pg 783 he puts {"AA"] = "Alcoa Inc."} in the constructor of the map dow_name. This wouldn't compile in VS2019 and gave errors due to the '] ='. 

Also, the code didn't run initially when using inner_product() given on pg784. It asserted with the error "cannot dereference end map/set iterator". Turns out that Bjarne is a sneaky fox. The lines:
double alcoaprice = dowPrice["AAA"];
double bowingPrice = dowPrice["BA"];
ADD to the map if those keys are not found so my dowPrice map became a size of 5 whereas dowWeight was still 3. This caused the dereferencing error on the map iterator. I will not forget ever again that map will default add an element if it doesn't exist.

Try This Pg 787

Github: 

I'm pretty sure I'm running with at least C++17 so this worked. The order was different.

Try This Pg 792

Github: 

I set max_size to INT_MAX and that still caused an error. For this the overflow caused an exception so it was quite bad and I would not be tempted to ship it.