|
| 1 | +# Reasons why you should learn how to program with C++ |
| 2 | + |
| 3 | +## Why? |
| 4 | + |
| 5 | +The first thing when you think of becoming a programmer is **HOW DO I START?** Well, **C++** is one of the best |
| 6 | +programming languages to give you insights into how a computer works. Through the process of learning how to code |
| 7 | +C++, you will learn not only how to use this language as a tool to solve your problems, but the farther you go, the |
| 8 | +more you will start uncovering and **exploring exciting computer concepts**. |
| 9 | + |
| 10 | +C++ gives you the **simplicity of C and adds a lot of steroids**. It delivers lots of quality-of-life stuff, increasing |
| 11 | +the developer experience. Let’s compare C with C++, shall we? |
| 12 | + |
| 13 | +1. The iconic book _"The C Programming Language" by Brian W. Kernighan and Dennis M. Ritchie_ has **only 263 pages**. |
| 14 | + Pretty simple, huh? |
| 15 | +2. The book _"C++ How to Program" by Harvey and Paul Deitel_ It holds around **1000 pages**, and the pages are way |
| 16 | + bigger than the other one. |
| 17 | + |
| 18 | +[](https://tenor.com/view/spit-out-cereal-meme-wtf-wth-what-the-hell-gif-20467304) |
| 19 | + |
| 20 | +So, don’t worry, you just need to learn the basics first, and all the rest are somehow advanced concepts. I will do |
| 21 | +my best to keep you focused on what is relevant to each moment of your learning journey. |
| 22 | + |
| 23 | +Without further ado. Get in the car! |
| 24 | + |
| 25 | +[](https://tenor.com/view/dog-jump-car-jump-cute-gif-15392159) |
| 26 | + |
| 27 | +## Speed Matters |
| 28 | + |
| 29 | +**A LOT**. Period. **C++** is one of the **closest intelligible programming languages before reaching the level of |
| 30 | +machine code**, as known as **Assembly Language**. If you code in machine code, you obviously will code precisely what |
| 31 | +you want the machine to do, but this task is too painful to be the _de-facto_ standard of coding. So we need |
| 32 | +something more straightforward and more human-readable. So C++ lies in this exact area of being close to assembly |
| 33 | +language and still able to be "easily" understandable. Note the quotes, they are there because it might not be that easy |
| 34 | +when you compare its syntax to other languages, **C++ has to obey some constraints to keep the generated binary fast** |
| 35 | +as a mad horse while trying to be easier than assembly. Remember, it can always get worse. |
| 36 | + |
| 37 | +[](https://tenor.com/view/trampling-charge-gif-19264241) |
| 38 | + |
| 39 | +## Why does speed matter? |
| 40 | + |
| 41 | +Mainly because we don’t want to waste time. Right? But it has more impactful consequences. Let’s think a bit more, you |
| 42 | +probably have a smartphone, and it lives only while it has enough energy on its battery. So, **if you are a lazy |
| 43 | +mobile developer and do not want to learn how to do code efficiently, you will make your app drain more energy |
| 44 | +from the battery** just by making the user wait for the task to be finished or by doing lots of unnecessary |
| 45 | +calculations! You will be the reason the user has not enough power to use their phones up to the end of the day. |
| 46 | +In fact, **you will be punishing your user by using your app**. You don’t want that, right? So let’s learn how to code |
| 47 | +appropriately. For the sake of the argument, worse than that, a lazy blockchain smart contract developer will make |
| 48 | +their users pay more for extra gas fee usage for the extra inefficient CPU cycles. |
| 49 | + |
| 50 | +[](https://gifs.com/gif/funcionrio-pblico-oY7PEL) |
| 51 | + |
| 52 | +## Language benchmarks |
| 53 | + |
| 54 | +I don’t want to point fingers at languages, but, hey, excuse me, python, are you listening to me, python? Python? |
| 55 | +Please answer! |
| 56 | +[reference cpp vs python](https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/gpp-python3.html). |
| 57 | +Nevermind. It is still trying to figure things out. Ah! Hey ruby, don’t be shy, I know you look gorgeous, and I |
| 58 | +admire you a lot, but can you dress up faster and be ready to run anytime soon? |
| 59 | + |
| 60 | +You don’t need makeup to run fast. That’s the idea. If the language does lots of fancy stuff, it won’t be extracting the |
| 61 | +juicy power of the CPU. |
| 62 | + |
| 63 | +So let’s first clarify some concepts for a fair comparison. Some languages do not generate binaries that run in your |
| 64 | +CPU. Some of them run on top of a virtual machine. **The Virtual Machine(VM) is a piece of software that, in runtime, |
| 65 | +translates the bytecode or even compiles source code to something the CPU can understand.** It’s like an old car; |
| 66 | +some of them will make you wait for the ignition or even get warm enough to run fast. I am looking at you Java and |
| 67 | +JavaScript. It is a funny concept, I admit, but you can see here that the ones that run on top of a translation device |
| 68 | +would never run as fast as a compiled binary ready to run on the CPU. |
| 69 | + |
| 70 | +So let’s bring some ideas from my own experience, and I invite you to test by yourself. Just search for ["_programming |
| 71 | +languages benchmark_" on your preferred search engine](https://letmegooglethat.com/?q=programming+languages+benchmark). |
| 72 | + |
| 73 | +[](https://tenor.com/view/tf2-pyro-teamfortress2-teamfortress2pyro-fire-gif-13656490) |
| 74 | + |
| 75 | +I don’t want to start a flamewar. Those numbers might be wrong, but the overall idea is correct. Assuming C++ does not |
| 76 | +add much overhead to your native binary, let’s set the speed to run as 1x. Java would be around 1.4x slower, and |
| 77 | +JavaScript is 1.6x, python 40x, and ruby 100x. The only good competitor in the house is Rust because its compiled code |
| 78 | +runs straight on the CPU efficiently with lots of quality-of-life additions. |
| 79 | +[Rust gives almost similar results](https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/gpp-rust.html) if |
| 80 | +you do not play around with memory-intensive problems. |
| 81 | + |
| 82 | +[](https://me.me/i/me-i-need-64-bytes-of-memory-rust-yeah-umm-0d048f5778154bf3aa0bcd6dc1499677) |
| 83 | + |
| 84 | +## Who should learn C++ |
| 85 | + |
| 86 | +[](https://tenor.com/view/the-office-michael-scott-steve-carell-pointing-you-gif-15115246) |
| 87 | + |
| 88 | +**YOU!** Yes, seriously, I don’t know you, but I am pretty sure **you should know how to code in any language**. C++ |
| 89 | +can be challenging, it is a fact, but if you dare to challenge yourself to learn it, your life will be somewhat better. |
| 90 | + |
| 91 | +Let’s cut to the bullets: |
| 92 | + |
| 93 | +1. The ones who seek to build efficient modules for mobile apps, such as the video/image processing unit; |
| 94 | +2. Game developers. Even the gameplay developers that usually only script things should know how to ride a horse(CPU) |
| 95 | + fast; |
| 96 | +3. Researchers looking to not waste time by coding inefficient code and wait hours, even days, to see the result of |
| 97 | + their calculations. They should reduce the costs of renting CPU clusters; |
| 98 | +4. Computer scientists are those who should know how a computer works. After all, C++ is one of the preferred |
| 99 | + programming languages that unlocks all the power of the CPU; |
| 100 | +5. Engineers, in general, should know how to simulate things efficiently; |
| 101 | + |
| 102 | +## How do machines run code? |
| 103 | + |
| 104 | +The first thing is: the CPU does not understand any programming language, only binary instructions. So you have to |
| 105 | +convert your code into something the machine can understand. This is the job of the compiler. **A compiler is a |
| 106 | +piece of software that reads a text file written following the rules of a programming language and essentially |
| 107 | +converts it into binary instructions that the CPU can execute**. There are many strategies and many ways of doing it. |
| 108 | +So, given its nature of being near assembly, **with C++, you will control precisely what instructions the CPU will |
| 109 | +run**. |
| 110 | + |
| 111 | +But, there is a catch here: for each CPU, you will need a compiler for that instruction set. Ex.: the compiler GCC can |
| 112 | +generate an executable program for ARM processors, and the generated program won’t work on x86 processors; In the same |
| 113 | +way, an x64 executable won’t work on an x86; you need to match the binary instructions generated by the compiler with |
| 114 | +the same instruction set available on the target CPU you want to run it. Some compilers can cross-compile: the compiler |
| 115 | +runs in your machine on your CPU with its instruction set, but the binary generated only runs on a target machine with |
| 116 | +its own instruction set. |
| 117 | + |
| 118 | +[](https://www.tutorialspoint.com/compiler_design/compiler_design_overview.htm) |
| 119 | + |
| 120 | +# Program Life Cycle |
| 121 | + |
| 122 | +Software development is complex and there is lots of styles, philosophies and standard, but the overall structure |
| 123 | +looks like this: |
| 124 | + |
| 125 | +1. Analysis, Specification, Problem definition |
| 126 | +2. Design of the Software (pseudocode/algorithm, flowchart), Problem analysis |
| 127 | +3. Implementation / Coding |
| 128 | +4. Testing and Debugging - In TDD(Test Driven Development) we write the tests first. |
| 129 | +5. Maintenance - Analytics and Improvements |
| 130 | +6. End of Life |
| 131 | + |
| 132 | +# Pseudocode |
| 133 | +> Pseudocode is a way of expressing algorithms using a combination of natural language and programming constructs. It |
| 134 | +is not a programming language and cannot be compiled or executed, but it provides a clear and concise way to |
| 135 | +describe the steps of an algorithm. Here is an example of pseudocode that describes the process of finding the |
| 136 | +maximum value in a list of numbers: |
| 137 | +> ``` |
| 138 | +> set maxValue to 0 |
| 139 | +> for each number in the list of numbers |
| 140 | +> if number is greater than maxValue |
| 141 | +> set maxValue to number |
| 142 | +> output maxValue |
| 143 | +> ``` |
| 144 | +> Pseudocode is often used as a planning tool for programmers and can help to clarify the logic of a program before it |
| 145 | +is written in a specific programming language. It can also be used to communicate algorithms to people who are not |
| 146 | +familiar with a particular programming language. [Reference](https://chat.openai.com/) |
| 147 | +
|
| 148 | +# Flowcharts |
| 149 | +> A flowchart is a graphical representation of a process or system that shows the steps or events in a sequential |
| 150 | +> order. It is a useful tool for demonstrating how a process works, identifying potential bottlenecks or |
| 151 | +> inefficiencies in a process, and for communicating the steps involved in a process to others. |
| 152 | +> |
| 153 | +> Flowcharts are typically composed of a series of boxes or shapes, connected by arrows, that represent the steps in |
| 154 | +> a process. Each box or shape usually contains a brief description of the step or event it represents. The arrows |
| 155 | +> show the flow or movement from one step to the next. |
| 156 | +> |
| 157 | +> Flowcharts can be used in a variety of settings, including business, engineering, and software development. They |
| 158 | +> are particularly useful for demonstrating how a process works, identifying potential issues or bottlenecks in the |
| 159 | +> process, and for communicating the steps involved in a process to others. |
| 160 | +> |
| 161 | +> There are many symbols and notations that can be used to create flowcharts, and different organizations |
| 162 | +> and industries may have their own standards or conventions for using these symbols. Some common symbols and |
| 163 | +> notations used in flowcharts include: |
| 164 | +> |
| 165 | +> 1. Start and end symbols: These are used to indicate the beginning and end of a process. |
| 166 | +> 2. Process symbols: These are used to represent the various steps or events in a process. |
| 167 | +> 3. Decision symbols: These are used to represent a decision point in a process, where the flow of the process depends on the outcome of a decision. |
| 168 | +> 4. Connector symbols: These are used to connect the various symbols in a flowchart, showing the flow or movement from one step to the next. |
| 169 | +> 5. Annotation symbols: These are used to add additional information or notes to a flowchart. |
| 170 | +> |
| 171 | +> By using a combination of these symbols and notations, you can create a clear and concise flowchart that effectively communicates the steps involved in a process or system. [Reference](https://chat.openai.com/) |
| 172 | +
|
| 173 | +I suggest using the tool [Code2Flow](https://code2flow.com/) to write pseudocode and see the flowchart drawn in real |
| 174 | +time. But you can draw them on [Diagrams](https://diagrams.net). |
| 175 | +
|
| 176 | +# Practice |
| 177 | +Try to think ahead the problem definition by questioning yourself before expressing the algorithm as pseudocode or |
| 178 | +flowchart: |
| 179 | +- What are the inputs? |
| 180 | +- What is a valid input? |
| 181 | +- How to compute the math? |
| 182 | +- What is the output? |
| 183 | +- How many decimals is needed to express the result? |
| 184 | +
|
| 185 | +Use [diagrams](https://diagrams.net) to draw a flowchart or use [Code2Flow](https://code2flow.com/) to |
| 186 | +write a working pseudocode to: |
| 187 | +1. Compute the weighted average of two numbers. The first number has weight of 1 and the |
| 188 | + second has weight of 3; |
| 189 | +2. Area of a circle; |
| 190 | +3. Compute GPA; |
| 191 | +4. Factorial number; |
| 192 | +
|
| 193 | +# Glossary |
| 194 | +- CPU |
| 195 | +- GPU |
| 196 | +- ALU |
| 197 | +- Main Memory |
| 198 | +- Secondary Memory |
| 199 | +- Programming Language |
| 200 | +- Compiler |
| 201 | +- Linker |
| 202 | +- Assembler |
| 203 | +- Pseudocode |
| 204 | +- Algorithm |
| 205 | +- Flowchart |
| 206 | +
|
| 207 | +# Activities |
| 208 | +1. Sign up on [beecrowd](https://www.beecrowd.com.br). If you are a enrolled student, look for the key in canvas |
| 209 | + to be assigned to the coding assignments. |
| 210 | +2. https://blockly.games/mazeLinks - test your ability to solve small problems via block programming |
| 211 | +3. https://codecombat.com/ - very interesting game |
| 212 | +
|
| 213 | +# Troubleshooting |
| 214 | +If you have problems here, start a |
| 215 | +[discussion](https://github.com/InfiniBrains/Introduction-to-Game-Programming-With-CPP/discussions) this is publicly |
| 216 | +visible and not FERPA compliant. Use discussions in Canvas if you are enrolled in a class with me. |
0 commit comments