Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

Commit 33635dbe authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Minor corrections done while proof-reading the lecture.

parent ce2a5787
......@@ -89,29 +89,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_11:3:16: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mexpected ';' at end of declaration\u001b[0m\n",
" int foo = 5 // COMPILATION ERROR!\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m\u001b[0;32m ;\n",
"\u001b[0m"
]
},
{
"ename": "Interpreter Error",
"evalue": "",
"output_type": "error",
"traceback": [
"Interpreter Error: "
]
}
],
"outputs": [],
"source": [
"{\n",
" int foo = 5 // COMPILATION ERROR!\n",
......@@ -120,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
......@@ -138,7 +118,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
......@@ -170,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
......@@ -180,31 +160,9 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_17:2:6: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mredefinition of 'i'\u001b[0m\n",
" int i = 1; // If the cell above was run, compilation error as `i` is al...\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m\u001b[1minput_line_16:2:6: \u001b[0m\u001b[0;1;30mnote: \u001b[0mprevious definition is here\u001b[0m\n",
" int i = 2; // Should be fine on the first call. But if you have execute...\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m"
]
},
{
"ename": "Interpreter Error",
"evalue": "",
"output_type": "error",
"traceback": [
"Interpreter Error: "
]
}
],
"outputs": [],
"source": [
"int i = 1; // If the cell above was run, compilation error as `i` is already defined."
]
......@@ -219,7 +177,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
......@@ -230,7 +188,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
......@@ -245,33 +203,14 @@
"source": [
"### Input / output\n",
"\n",
"Inputs and outputs aren't directly a part of the language itself, but are in the standard library (often abbreviated as STL for Standard Template Library even if some purist may well and explain it's not 100 % the same thing...). You therefore need to __include__ a file named `iostream`; doing so will enable the use of the input / output facilities."
"Inputs and outputs aren't directly a part of the language itself, but are in the standard library (often abbreviated as STL for Standard Template Library even if some purist may yell and explain it's not 100 % the same thing...). You therefore need to __include__ a file named `iostream`; doing so will enable the use of the input / output facilities."
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_26:3:10: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mno member named 'cout' in namespace 'std'\u001b[0m\n",
" std::cout << \"Hello world!\" << std::endl; // Should fail (unless yo...\n",
"\u001b[0;1;32m ~~~~~^\n",
"\u001b[0m"
]
},
{
"ename": "Interpreter Error",
"evalue": "",
"output_type": "error",
"traceback": [
"Interpreter Error: "
]
}
],
"outputs": [],
"source": [
"{\n",
" std::cout << \"Hello world!\" << std::endl; // Should fail (unless you run a cell that includes iostream before)\n",
......@@ -280,17 +219,9 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world!\n"
]
}
],
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
......@@ -303,7 +234,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"- `std::cout` is the symbol to designate the standard output\n",
"- `std::cout` is the symbol to designate the standard output (i.e. your screen...)\n",
"- `std::endl` is the symbol to clean-up the stream and go to next line.\n",
"\n",
"The operator `<<` is used to indicate what you direct toward the stream; here std::cout << \"Hello world!\" tells to redirect the string toward the standard output.\n",
......
......@@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -60,7 +60,7 @@
" int number; // integer variable\n",
" double real; // floating-point variable\n",
"\n",
"std::cout << number << std::endl;\n",
" std::cout << number << std::endl; \n",
" std::cout << real << std::endl;\n",
"}\n",
"\n"
......@@ -77,15 +77,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Although not mandatory, it is strongly recommended to give a\n",
"Although not mandatory, it is **strongly** recommended to give a\n",
"initial value to your variables, as an expression between brackets. \n",
"\n",
"Not providing an initial value may lead to unexpected behaviour. For instance you can't make hypothesis upon the values of `number` and `real` in the cell above: you might end-up with any value... and someone else might get other values on his computer!\n",
"\n",
"\n",
"If you give braces without values, a predefined and associated value\n",
"to the type is used (usually a form of 0).\n",
"If you give nothing , the behaviour is undefined: you might end up with a 0-like value or something"
"to the type is used (usually a form of 0)."
]
},
{
......@@ -97,7 +96,7 @@
"{\n",
" int nb1 { 1 }; // integer variable set with the value 1\n",
" int nb2 {}; // same as int nb2{0};\n",
" double pi { 3.14 } ; // real variable\n",
" double pi { 3.14 }; // real variable\n",
"}"
]
},
......@@ -107,13 +106,13 @@
"source": [
"C++ actually supports many other historical forms\n",
"of initialization, which you will encounter everywhere, including in this tutorial,\n",
"with brackets and/or an equal sign. There are some subtile differences\n",
"with brackets and/or an equal sign. There are some subtle differences\n",
"between each other... that you can ignore most of the time."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
......@@ -175,6 +174,36 @@
"} "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Affectations may be chained:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = 5, b = 5 and c = 5\n"
]
}
],
"source": [
"{\n",
" int a {}, b {}, c {};\n",
" \n",
" a = b = c = 5; \n",
" \n",
" std::cout << \"a = \" << a << \", b = \" << b << \" and c = \" << c << std::endl;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -184,7 +213,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 5,
"metadata": {},
"outputs": [
{
......@@ -229,7 +258,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 8,
"metadata": {},
"outputs": [
{
......@@ -252,8 +281,7 @@
" a++; // increment a by 1.\n",
" ++a; // same, both are actually equivalents here.\n",
" \n",
" int c = a + b;\n",
" \n",
" int c = a + b; \n",
" std::cout << \"a = \" << a << \", b = \" << b << \" and c = \" << c << std::endl;\n",
" \n",
" c = a-- + b; // first a + b is evaluated, and only then a is decremented. \n",
......@@ -273,7 +301,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 9,
"metadata": {},
"outputs": [
{
......@@ -281,7 +309,7 @@
"output_type": "stream",
"text": [
"a = 7, b = 3 and c = 10\n",
"a = 6, b = 3 and c = 9\n",
"a = 6, b = 3 and c = 10\n",
"a = 6, b = 4 and c = 10\n"
]
}
......@@ -295,17 +323,14 @@
"\n",
" int c = a + b;\n",
" std::cout << \"a = \" << a << \", b = \" << b << \" and c = \" << c << std::endl;\n",
" \n",
" --a; // equivalent to a-- but for reasons related to the standard library I advise you to rather use the\n",
" // pre-increment form.\n",
" \n",
" c = a + b; // first a + b is evaluated, and only then a is decremented. \n",
" \n",
" c = a + b;\n",
" --a; // equivalent to a-- but for reasons related to the standard library I advise you \n",
" // to rather use the pre-increment form when both are equivalent.\n",
" std::cout << \"a = \" << a << \", b = \" << b << \" and c = \" << c << std::endl;\n",
" \n",
" \n",
" ++b; // same: equivalent to b++;\n",
" \n",
" c = a + b; // first a is incremented, and only then a + b is evaluated.\n",
" \n",
" ++b; // same: equivalent to b++; \n",
" c = a + b;\n",
" std::cout << \"a = \" << a << \", b = \" << b << \" and c = \" << c << std::endl;\n",
"}"
]
......@@ -404,7 +429,7 @@
"in this case we use the pointer name directly, or we want to modify the variable\n",
"pointed, in which case the name is prefixed with `*`.\n",
"\n",
"We can therefore see the pointer as a kind of redefinable reference."
"We can therefore see the pointer as a kind of redefinable reference; pointers are a feature from C language."
]
},
{
......@@ -477,7 +502,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"metadata": {},
"outputs": [
{
......@@ -491,7 +516,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_16:4:62: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mnull passed to a callee that requires a non-null argument [-Wnonnull]\u001b[0m\n",
"\u001b[1minput_line_24:4:62: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mnull passed to a callee that requires a non-null argument [-Wnonnull]\u001b[0m\n",
" std::cout << \"\\t p: address = \" << p << \", value = \" << *p << std::endl; // Dereferencing p is misguided!\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m"
......@@ -562,7 +587,7 @@
"{\n",
" const double pi = 3.1415927;\n",
" double const pi_2 = 3.1415927; // equally valid; it is just a matter of taste. Mine is to put it before,\n",
" // so that is what you will see in the remaining of the tutorial.\n",
" // so that is what you will see in the remaining of the lecture.\n",
" pi = 5.; // COMPILATION ERROR!\n",
" pi_2 = 7.; // COMPILATION ERROR!\n",
"}"
......@@ -577,7 +602,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
......@@ -592,17 +617,17 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_74:6:7: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mcannot assign to variable 'p' with const-qualified type 'int *const'\u001b[0m\n",
"\u001b[1minput_line_27:6:7: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mcannot assign to variable 'p' with const-qualified type 'int *const'\u001b[0m\n",
" p = &b; // COMPILATION ERROR - if you comment it it will \n",
"\u001b[0;1;32m ~ ^\n",
"\u001b[0m\u001b[1minput_line_74:4:16: \u001b[0m\u001b[0;1;30mnote: \u001b[0mvariable 'p' declared const here\u001b[0m\n",
"\u001b[0m\u001b[1minput_line_27:4:16: \u001b[0m\u001b[0;1;30mnote: \u001b[0mvariable 'p' declared const here\u001b[0m\n",
" int* const p { &a }; // Value is modifiable, but not the address pointed to.\n",
"\u001b[0;1;32m ~~~~~~~~~~~^~~~~~~~\n",
"\u001b[0m"
......@@ -629,14 +654,14 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_75:7:8: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mread-only variable is not assignable\u001b[0m\n",
"\u001b[1minput_line_28:7:8: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mread-only variable is not assignable\u001b[0m\n",
" *p = 5; // COMPILATION ERROR \n",
"\u001b[0;1;32m ~~ ^\n",
"\u001b[0m"
......@@ -795,17 +820,17 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_18:7:32: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1marray index 10 is past the end of the array (which contains 10 elements) [-Warray-bounds]\u001b[0m\n",
"\u001b[1minput_line_30:7:32: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1marray index 10 is past the end of the array (which contains 10 elements) [-Warray-bounds]\u001b[0m\n",
" std::cout << \"i[10] = \" << i[10] << \" (undefined behaviour: out of range. Warning identifies the issue)\" << std::endl ;\n",
"\u001b[0;1;32m ^ ~~\n",
"\u001b[0m\u001b[1minput_line_18:3:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0marray 'i' declared here\u001b[0m\n",
"\u001b[0m\u001b[1minput_line_30:3:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0marray 'i' declared here\u001b[0m\n",
" int i[10] ; // Array of 10 integers - not initialised properly!\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m"
......@@ -815,8 +840,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"i[2] = 149375929 (may be nonsense: undefined behaviour due to lack of initialization!)\n",
"i[10] = -87424866 (undefined behaviour: out of range. Warning identifies the issue)\n",
"i[2] = 50428857 (may be gibberish: undefined behaviour due to lack of initialization!)\n",
"i[10] = -2038759403 (undefined behaviour: out of range. Warning identifies the issue)\n",
"x[1] = 2 (expected: 2.)\n"
]
}
......@@ -843,7 +868,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 16,
"metadata": {},
"outputs": [
{
......@@ -891,7 +916,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 17,
"metadata": {},
"outputs": [
{
......
......@@ -64,24 +64,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Of course, the precedent is embarrassing but is due to the poor indenting used: you have to remember that in C++ indenting is just for the programmer: it is much easier to read if a program is properly indented, but it doesn't matter from the compiler standpoint!\n",
"Of course, the precedent code is embarrassing but is due to the poor indenting used: you have to remember that in C++ indenting is just for the programmer: it is much easier to read if a program is properly indented, but it doesn't matter from the compiler standpoint!\n",
"\n",
"Our case above would be much clearer however with a more logical indenting and spacing:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"It's much clearer now that this line is not encompassed by the condition!\n"
]
}
],
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
......@@ -107,17 +99,9 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a was not modified and is still 2\n"
]
}
],
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
......@@ -141,32 +125,14 @@
"source": [
"### No semicolon at the end of the `if` condition\n",
"\n",
"__BEWARE__: do not put a `;` at the end of an `if` statement! If you do so, the statement executed if the condition is true is the empty statement `;` which doesn nothing... The risk is rather mitigated: any compiler worth its salt will warn you if you do this mistake."
"__BEWARE__: do not put a `;` at the end of an `if` statement! If you do so, the statement executed if the condition is true is the empty statement `;` which does nothing... The risk is rather mitigated: any compiler worth its salt will warn you if you do this mistake."
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_11:4:16: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mif statement has empty body [-Wempty-body]\u001b[0m\n",
" if (a == 0);\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m\u001b[1minput_line_11:4:16: \u001b[0m\u001b[0;1;30mnote: \u001b[0mput the semicolon on a separate line to silence this warning\u001b[0m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Will be printed: the statement after the condition is ';', which does nothing...\n"
]
}
],
"outputs": [],
"source": [
"{\n",
" int a = 2;\n",
......@@ -177,7 +143,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
......@@ -186,7 +152,7 @@
" if (a == 2)\n",
" ; // putting the semicolon in a different line silences the warning; there are legitimate cases in\n",
" // which it's useful to do so and the risk is\n",
" // slim this was written by mistake. \n",
" // slim that this was written by mistake. \n",
"}"
]
},
......@@ -235,7 +201,7 @@
"source": [
"Please notice that:\n",
"* `else if` and `else` syntax is the same as the one for `if` and you may choose a single statement or a block in each branch of the condition.\n",
"* As soon as one condition is fulfilled, the execution of the condition blocks ends. I didn't have to spell out in the (a < 100) case that a had to be greater than 10."
"* As soon as one condition is fulfilled, the execution of the condition blocks ends. I therefore didn't have to spell out in the (`a < 100`) case that `a` had to be greater than 10."
]
},
{
......@@ -254,17 +220,9 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Is 5 even? -> false\n"
]
}
],
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
......@@ -283,17 +241,9 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Is 5 even? -> False\n"
]
}
],
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
......@@ -316,17 +266,9 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"outputs": [],
"source": [
"{\n",
" int i = 5;\n",
......@@ -345,36 +287,9 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [