You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 00 Algorithms/2 Thinking in algorithms.md
+62-62Lines changed: 62 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ OUTPUT:
27
27
28
28
As you can see, we have used **labels**`a` and `b` for an input data that <u>needs to be provided at the program execution</u> ( at the moment, let's do not worry how) and also we have performed some computations and **labeled** (named) their result as `area`. Finally, we can provide this named value as the algorithm output.
29
29
30
-
Let's take a look at another example. Given two numbers *x*<sub>1</sub> and *x*<sub>2</sub> computer their average. The algorithm would be as follows:
30
+
Let's take a look at another example. Given two numbers *x*₁ and *x*₂ computer their average. The algorithm would be as follows:
31
31
32
32
```
33
33
INPUT:
@@ -43,12 +43,12 @@ OUTPUT:
43
43
44
44
What you see above is a recipe. It contains two simple steps. You should try to track it and see how the labeled values change:
45
45
46
-
| STEP |*x*<sub>1</sub> |*x*<sub>2</sub>|*total*|*average*| RESULT |
The above algorithm was not performed linearly. Depending on some condition either one or another operation was performed. This is called a *conditional*.
86
86
87
-
Let's take a look at more advanced example — solution to [Quadratic equation](https://en.wikipedia.org/wiki/Quadratic_equation) of the form *a**x*<sup>2</sup> + *b**x* + *c* = 0. I hope you remember how to do it! Hence, the simplest and most naive algorithm would be:
87
+
Let's take a look at more advanced example — solution to [Quadratic equation](https://en.wikipedia.org/wiki/Quadratic_equation) of the form *a**x*² + *b**x* + *c* = 0. I hope you remember how to do it! Hence, the simplest and most naive algorithm would be:
88
88
89
89
```bash
90
90
INPUT:
@@ -122,58 +122,58 @@ OUTPUT:
122
122
x₁, x₂
123
123
```
124
124
125
-
Above, you can see that the algorithm can take three paths, depending on the value of *Δ*. What is important, is that in each path we mention both *x*<sub>1</sub> and *x*<sub>2</sub>. This is because, we have identified both of them as an output, so **we must define such names in every possible path!** Even in the cases when one or both of them does not exits, we mark them as non-existent (right now, you need not worry how: programming languages allow to do it one way or another).
125
+
Above, you can see that the algorithm can take three paths, depending on the value of *Δ*. What is important, is that in each path we mention both *x*₁ and *x*₂. This is because, we have identified both of them as an output, so **we must define such names in every possible path!** Even in the cases when one or both of them does not exits, we mark them as non-existent (right now, you need not worry how: programming languages allow to do it one way or another).
126
126
127
127
So the algorithm looks good... Let's track if for some data:
128
128
129
-
Solving *x*<sup>2</sup> + 2 *x* – 8 = 0:
130
-
131
-
| STEP |*a*|*b*|*c*|*Δ*|*x*<sub>1</sub>|*x*<sub>2</sub>| RESULT |
Can you see what happened? Out algorithm failed for a case where *a* = 0! What can be done about it? Well, we need to explicitly check this case. So the algorithm will be:
173
173
174
174
```
175
175
INPUT:
176
-
a, b, c — eqution coeffi ients.
176
+
a, b, c — eqution coefficients.
177
177
178
178
STEPS:
179
179
— Check if a ≠ 0, if so:
@@ -196,7 +196,7 @@ OUTPUT:
196
196
197
197
What you can see above is a **nested conditional**. First we check one condition (*a* ≠ 0) and in one of the cases we check another one (*Δ* > 0 or *Δ* = 0). When you write your own algorithms, clearly mark which block is nested in which!
Please trace this algorithm for the equations *x*² + 2 *x* –8 = 0, *x*² – 2*x* + 1 = 0, *x*² + 2 *x* + 3 = 0, and 2 *x* + 4 = 0 (*a* = 0) yourself.
0 commit comments