Wednesday, 3 November 2021

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

For each language mentioned in this chapter, look at a popular textbook and see what is used as the first complete program. Write that program in all of the other languages. Warning: This could easily be a 100-program project.

Github: 

Due to the warning I decided to do something different as I don't have that much time (and I get bored easily). So instead, I went back to Chapter 3 (had a nostalgia trip) and decided to implement Exercise 2:
"Write a program in C++ that converts from miles to kilometres"
This needs input, output and some mild arithmetic; perfect.

As for the languages; I spent a couple days doing the previous exercise and that was just Hello World programs so I chose 9 of the 20 languages I got running. 5 of them are languages I'm most likely to use (and most have VS support). The other 4 are "legacy" languages and completely outside my comfort zone.

1) C++
To start off here is the nice C++ console version:

2) C
This was trickier than I expected. Reading in from the console is not as nice as cin >>. Also I forgot that bool isn't a native type in C but was added in C99 via stdbool.h. I also spent a bit too much time faffing about with VS Code so I could build and run C code in that. Honestly doesn't feel worth it; will continue to use VS.

3) Visual Basic
This was a task. First I had to google how to make functions in VB, then how to make variables, then how to write if else and compare.....This language is strange. It uses = for equality AND assignment disgusting. I also hate how everything is english yet cobol....
Dim number As Integer = 0
is way more tedious than
int number = 0;

4) Python
So I learnt that Python doesn't support const values and I found this answer from Stackoverflow amusing:
"No there is not. You cannot declare a variable or value as constant in Python. Just don't change it"
Just don't change it. If only it were that simple. It's also weird not having a main and everything is out in the open with no brackets. I hate loosely typed languages and languages that insist on not instead of !. I think I'm just set in my ways at this point.

5) Java
The only good thing about Java is that it uses semi-colons; praise be to semi-colons and brackets. The importing of everything and creating NEW of everything annoys me constantly though. When I'm making apps it just feels like every other line is 
SomeType = new SomeType(Stupid.Long.Type.Name);

6) C#
This one took me about 5 minutes; I was surprised. It was like a Java version of Visual Basic....I guess that sums up C# in a nutshell.

7) Pascal
This was truly the strangest so far...the function definition is so unnecessarily complicated. Also when multiplying a float (or real in Pascal) with another float, the point was always in the wrong place. I couldn't figure this out and frankly I don't care enough.

8) Fortran
This one took me a good 40 minutes and I initially almost gave up as I could not for the life of me figure out how to get functions to work. I now see where Visual Basic comes from. It wasn't completely awful once I'd figured out the function definition and logical operators/loops. I can understand why no one wants to learn it anymore though...

9) Cobol
I'd honestly rather program in straight up assembly than this monstrosity. It's ironic that the syntax was chosen for "readability" but ultimately it makes it more confusing. Also, calling a variable a 'picture' because you give the computer a 'picture of the value' is stupid.

This one I had to skip because it was just too different. I think Cobol has user defined functions but I couldn't figure out how to make one. Variables need to be declared up front in a "data bank" of some sorts which is almost identical to ASM.

I feel sorry for anyone who was forced to use this language professionally.

10) Simula87
And I also gave up on this one because I just couldn't anymore. The further I stray from C++, the more I want to jump off a cliff.

// Conclusion
This exercise was amazing and I highly recommend that you choose an exercise from Chapter 3 and implement it in other languages. It made me realise that Visual Studio is God, semi-colons must always end an expression and loosely typed languages should have a word with themselves (pun intended).

Attempting the exercise in Pascal, Fortran, Cobol and Simula was the most "enlightening" though. Bjarne said he made C++ for a "more enjoyable" programming experience; he wasn't lying. Programming in C++ is pure joy, not just in comparison to "legacy" languages but new ones as well. I can safely say that there is no other language I would rather program in and it's not because C++ was my first language but I genuinely believe it's the easiest and most straight forward language of them all.

Learn C++ first.

A huge shout out to TutorialsPoint though for their excellent overview of all the ancient languages.

No comments:

Post a Comment