Sunday, 24 October 2021

Chapter 22 // Exercise 5 - Principles & Practice Using C++

Chapter 22 // Exercise 5

Write a "Hello, World!" program in each of the languages mentioned in this chapter.


[Preface] Ok so....not counting various revisions of languages (like C98, C11); I counted 29 (?) languages. Now I don't know if we're just supposed to write the hello world program or actually get it to compile and execute?...I'm guessing the latter. That means I have to figure out at least 27 ways to build a language and a lot of these aren't in use anymore....hooo boy.

LETS BEGIN.

1) C++
The most obvious and easiest (for me):
This was extremely nostalgic and I had to pause for a moment as it took me back to January 2016 when I first saw "Hello, world!" in C++ and wondered "how the hell am I ever gonna remember what all this shit does?"

2) LISP
Lisp is quite honestly, disgusting, and I never want to look at it again.

3) FORTRAN
Trying to get Fortran to build on Windows honestly went over my head. Apparently there is an Intel Fortran compiler for Visual Studio but  I couldn't find it. I ended up using an online compiler again.

4) COBOL
Oh my christ; what is this?? It's like assembly and the terminator had a child.

5) ALGOL68
So this is where Python comes from? The Algol68 Hello, World is more efficient than the normal Algol but it took me a while to figure out how to add comments.

6) SIMULA87
This is a strange one. It's kind of like pseudocode but it runs....the comments also need semi-colons at the end which is weird. It's like a combination of Fortran and Pascal syntactically.

7) BCPL (Basic Combined Programming Language)
Apparently this the language that they used to program compilers with! The very first Hello, World! was written in this language as well so doing this felt like some sort of programmers pilgrimage. The language was created by Martin Richards who rivals Bjarne himself for the most "programming language creator" website in the world:
Sometimes I feel like blogger is too much and I too should resort to plain HTML.
More fun facts, it was the first brace language! And introduced // for comments (which C rejected).

Unfortunately there is no online compiler for BCPL, however it was recently revised by Martin himself as recently as 10th October 2021! So for this one I ended up getting the most recent version of the compiler and then spent all Friday evening trying to get it to work. Narrator: it did not work.

Even using the pre-compiled applications wouldn't work and it would exit immediately with an error. I think it's something to do with the date as it thinks it's January 13th 2020. Excellent. Oh well. I learnt a lot about batch files in this one so there's that!

8) PL/1
The aptly named "Programming Language 1" language also doesn't have an online compiler; or any compiler for windows. Just Linux and something called z/OS. I'm not interested in going to great lengths to get these to work. I did look at the hello world sample on Wikipedia though and I hate it.

9) PASCAL
Pascal I've come across before. Chapter 18 Exercise 11 asks you to implement a skip list and I used a paper that used Pascal. It's not completely awful. Fortunately it has an online compiler.

10) C
Ah C, my old friend. Compiling C with Visual Studio is quite simple but at least you can just create a new C++ project, add C files to it and VS should compile with the C compiler if it sees .c extensions.

11) HASKELL
This one was simple enough. It looks a bit strange; not sure how I feel about it.

12) EULER
The wiki page is short for this one and I can't find much about it. It seems it fell out of favour and no one is really using it anymore.

13) MODULA
Niklaus Wirth really loved creating programming languages. Apparently Modula 3 had an online release in 2010 but the websites look really dodgy and I don't fancy downloading weird applications off websites to try and compile it.

14) OBERON
Niklaus Writh is back in this list for the 5th time! (I think). Oberon had a stable release last year however there's no online compiler apart from one that converts Oberon to Javascript?? I did find this amazing website though:
I must check out lolcode. It seems like the language for me.

15) TURBO PASCAL
Ok so Turbo Pascal isn't actually a language; it's a compiler/IDE for Pascal??

16) DELPHI
Delphi also isn't a language; its an IDE and another Pascal compiler.

17) ADA
Not the bitch in the red dress, a programming language. Another Pascal-like language with what wikipedia says "extremely strong typing", not just strong but "extremely". 

Also, this quote from Bjarne's Website:

Do you really recommend Ada over C++ for larger projects?

No. I have no idea who started that rumor, but it must have been an over-enthusiastic or malicious Ada devotee.
 

18) SCHEME
Oh no....a LISP language KILL IT WITH FIRE.

19) CPL
So this is the predecessor to BCPL? It seems to be maintained which is cool however it's only for Linux and even the Windows version wants you to have a Linux subsystem so no thank you. 

20) B
I feel like when I'm on Wikipedia looking at pages for the Jurassic age with all these ancient languages and "predecessors"/"ancestors" talk. B is very similar to C and someone made a working compiler for it but it's janky. Also, just use C. I learnt that B was implemented and then C immediately came out. I've read a few comments around saying how small B is and "any good programmer should be able to implement it". Sounds like a good exercise.

21) SMALLTALK
Another object-orientated language derived from Simula. Wikipedia says its an entirely reflective system which is interesting and could be worth learning to understand how Unreal Engines Reflection system works (it uses reflection for the editor/blueprints). The hello world is a one liner in this project and looks completely unlike an object-orientated language.

22) EIFFEL
An object oriented language that ended up influencing Java and Microsoft Java and is still in use today! You have to make a class and then do things with that class...a true Java Parent....disgusting. Also, if you google eiffel stuff it almost always brings up pictures of the eiffel tower.

23) VISUAL BASIC
In all seriousness though I actually didn't mind this one? I think it's because it was extremely easy to set up and run thanks to Visual Studio. I kind of like how it looks as well, plus it comes with a console window....Also, Excel uses a form of VB, maybe I'll finally become an Excel wizard?

24) PYTHON
Oh Python...how I detest you. We had a brief module in the first year of Uni where we had to create a networked version of noughts and crosses using Python. I hate Python. However back then, Visual Studio had only begun to toy with the idea of adding Python support so I had to use the Python thing; Idle (which I also hate). Now VS has full Python support and makes this Hello World a painless 1 liner.

25) PHP
I was weirdly looking forward to this one as everyone online seems to think PHP is the anti-christ or something. Yeah it looks a bit strange but have they ever used Lisp??? To be honest I don't understand the fuss with this language? It's basically just HTML with added functionality.

26) JAVA
I also detest Java with a passion however I am strongly team Android and as such use Android Studio quite often. Even though I hate it, I'd say it's my second most-used language and thanks to learning C++ first I didn't really even need to learn Java. I just google what I need to know as I go along. I used an online compiler for this as it's a nightmare installing IntelliJ IDEA and Android Studio takes 5 years to boot.

27) PERL
I don't really have any strong opinions on this one. Basically looks like Python.

28) C#
Sometimes I'm not sure which I hate more Java or C#. I have to use C# at work sometimes (as all our tools are written in it) and I hate it. Sure, it's fast to get things working and you see results quickly but the language is not for me. It does have an edge over Java though because I can use Visual Studio. 

Microsoft though has a really cool interactive website that takes you through learning the basics of C#:

29) JAVASCRIPT
And last (and certainly least) is Javascript where everything is THIS and VAR. Like PHP, this is just another way to add functionality to HTML. Due to its C++ style syntax I find it easier to understand than PHP but I don't like using it. I think it's safe to say I don't like any form of web programming.

And with that I'm done! I managed to compile and run 20/27 languages which is pretty cool and it gave me a real insight into language similarities/differences. It also made me seriously appreciate Visual Studio more than I already do. I think at this point I'm too far down the VS rabbit-hole and giving it up would feel like losing something dear to me.

I don't think using Online Compilers is cheating though; setting up language environments is tough and honestly one of the worst aspects of programming; I think that's why VS wins for me and a deciding factor over which languages I choose in the future.

Saturday, 23 October 2021

Chapter 22 // Exercise 1, 2, 3, 4 - Principles & Practice Using C++

I thought about not doing these written exercises, however the entire chapter is about learning about history so you don't make the same mistakes/understand what came before and how that influences the present.

I actually enjoyed looking up all these random people. Well not all of them are random (I'd heard of most of the computer scientists) but the non-scientists were more interesting.

Chapter 22 // Exercise 1

Define programming.

Github: N/A

I would say programming is the art of making a computer do what you want.

Chapter 22 // Exercise 2

Define programming language.

Github: N/A

Using page 807, a programming language is many but most notably a tool for making the computer do what you want. There are many different languages that do different things but having standards for those languages allows effective communication between programmers. I used to be a flight attendant for Emirates. The global aviation language is English; when flying, relaying instructions etc., it is imperative you speak English so everyone is on the same page.

Chapter 22 // Exercise 3

Go through the book and look at chapter vignettes. Which were from computer scientists? Write one paragraph summarising what each of those scientists contributed.

Github: N/A

Well I'm not writing a paragraph...later on he wants 10-20 page reports on C++....ain't nobody got time for that.

Chapter 2 - Brian Kernighan; Canadian computer scientist that contributed to the development of Unix and C (and many other things).

Chapter 4 - Gerald Weinberg; American computer scientist who specialised in the psychology and anthropology of computer software development.

Chapter 5 - Maurice Wilkes; British computer scientist who designed and helped build the Electronic Delay Storage Automatic Calculator (one of the earliest stored program computers) and invented microprogramming.

Chapter 6 - Kristen Nygaard; Norwegian computer scientist who is the co-inventor of object-oriented programming and Simula.

Chapter 13 - Alan Perlis? This chapter says traditional however google says Alan Perlis but it isn't sure. Anyway he was an American computer Scientist and was the first recipient of the Turing Award.

Chapter 17 - Alex Stepanov is a Russian-American computer programmer best known as the primary designer and implementer of the C++ Standard template library.

Chapter 20 - Douglas McIlroy is an American (?) mathematician, engineer and programmer. He participated in the design of many influential languages (including C++) and is still a professor at the age of 89.

Chapter 21 - Trygve Reenskaug is a Norwegian computer scientist who formulated the model-view-controller patter for graphical user interface software design.

Chapter 22 - Alan Perlis. See Chapter 13 description.

Chapter 26 - Donald Knuth is an American computer scientist and called "the father of analysis of algorithms).

Chapter 27 - Dennis Ritchie was an American computer scientist and co-creator of the C programming language (amongst many other things).

Chapter 22 // Exercise 4

Go through the book and look at chapter vignettes. Which ones were not from computer scientists? Identify the country of origin and field of work in each.

Github: N/A

Chapter 0 (Switzerland?) - Swiss army proverb . I google this and it came up with many other books also using this quote in their chapter vignettes but couldn't actually find a source for it.

Chapter 1 (USA) - R A Heinlein is a Science Fiction author and this quote is from the "Competent man". Apparently a competent human should be able to do such things as "build a wall", "pitch manure", "solve equations, "butcher a hog", "die gallantly" amongst other things....

Chapter 3 (France) - Louis Pasteur was a French chemist and microbiologist renowned for discoveries of the principles of vaccination, fermentation and pasteurisation...I actually found this very interesting. I guess Pasteur is where the word pasteurisation comes from.

Chapter 7 (USA?) - First recorded use was Dallas Morning News in 1976 apparently.

Chapter 8 (USA?) - It says "traditional" in the book but google attributes this quote to Marion J. Levy Jr who was an American sociologist noted for his work on modernisation theory.

Chapter 9 (Denmark) - Peit Hein was a Danish polymath. He also invented the Soma cube and the board game Hex.

Chapter 10 (USA) - Richard Feynman was an American theoretical physicist known for many things made out of big words. He looks more like a Hollywood actor than a scientist.

Chapter 11 (Germany) - Albert Einstein was a German theoretical physicist who practically invented all physics (I'm exaggerating but he made a lot of contributions). I had no idea that he died in 1955, I thought it was much later.

Chapter 12 (USA) - Calvin and Hobbes was a daily American comic strip that I had never heard of until researching this. I literally put "Calvin's dad" into google and it knew exactly what I was talking about. Google says it's cited as "the last great comic newspaper". Clearly they had never heard of Garfield.

Chapter 14 (Roman Republic) - A Roman author, architect and civil/military engineer during the 1st century BC.

Chapter 18 (?) - Caveat Emptor is apparently a Latin phrase that means "let the buyer beware".

Chapter 15 (France) - His actual name is Francois-Marie Arouet (quite possible one the Frenchest names I've encountered) and was born 1694. He was an French "enlightenment" writer. I'm guessing that's not to do with new age stuff but more philosophy. 

Chapter 16 (USA) - Nicholas Negroponte is a Greek-American architect and stupidly rich.

Chapter 19 (UK) - Winston Churchill was the Prime Minister of the UK from 1940 till 1945.

Chapter 23 (USA) - Errol Morris is an American film director known for documentaries. 
 
Chapter 24 (USA) - Henry Louis Mencken was an American Journalist.

Chapter 25 - Pretty sure Bjarne made this one up.

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.