From 3994a40bb94bb6a8622202fd3142afd152a8a94a Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@pm.me> Date: Mon, 26 Sep 2022 15:55:12 +0200 Subject: [PATCH] maj boucles et conditions --- slides/Makefile | 4 +-- slides/boucles_conditions.md | 55 ++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/slides/Makefile b/slides/Makefile index f56e0ed..cecf4c2 100644 --- a/slides/Makefile +++ b/slides/Makefile @@ -21,8 +21,8 @@ HTML=$(MD:%.md=%.html) # Pour les fichier html on transforme .md -> .html PDF=$(MD:%.md=%.pdf) # Pour les fichier pdf on transforme .md -> .pdf MARKDOWN=$(MD:%.md=%.markdown) # Pour les fichier markdown on transforme .md -> .markdown -all: $(PDF) $(HTML) # La cible par défaut (all) exécute les cibles %.html et %.pdf -# all: $(PDF) # La cible par défaut (all) exécute les cibles %.html et %.pdf +# all: $(PDF) $(HTML) # La cible par défaut (all) exécute les cibles %.html et %.pdf +all: $(PDF) # La cible par défaut (all) exécute les cibles %.html et %.pdf markdown: $(MARKDOWN) # La markdown les cibles %.markdown diff --git a/slides/boucles_conditions.md b/slides/boucles_conditions.md index 6456cba..ee71d10 100644 --- a/slides/boucles_conditions.md +++ b/slides/boucles_conditions.md @@ -55,6 +55,8 @@ int main() { # Structures de contrôle: `if`{.C} .. `else if`{.C} .. `else`{.C} (1/2) +\footnotesize + ## Syntaxe ```C @@ -81,6 +83,8 @@ if (x) { // si x s'évalue à `vrai` # Structures de contrôle: `continue`{.C}, `break`{.C} +\footnotesize + - `continue`{.C} saute à la prochaine itération d'une boucle. ```C @@ -136,7 +140,7 @@ int main() { $ ./prog $ echo $? 0 # tout s'est bien passé par exemple -$ if [ $? -eq 0 ]; then echo "OK" ; else echo "ERROR"; fi +$ if [ $? -eq 0 ]; then echo "OK"; else echo "ERROR"; fi ERROR # si tout s'est mal passé ``` @@ -160,12 +164,16 @@ ERROR # si tout s'est mal passé ```C #include <stdio.h> #include <stdlib.h> - int main() { printf("Hello world.\n"); int val = 1; - printf("Hello world %d time.\n", val); - printf("%f squared is equal to %f.\n", 2.5, 2.5*2.5); + // %d => int + printf("Hello world %d time.\n", val); + // %f => float + printf("%f squared is equal to %f.\n", + 2.5, 2.5*2.5); + // %s => string + printf("Hello world %s.\n", "Hello world"); return EXIT_SUCCESS; } ``` @@ -194,7 +202,7 @@ int main() { int main() { printf("Enter 3 numbers: \n"); int i, j, k; - scanf("%d %d %d", &i, &j, &k); + scanf("%d %d %d", &i, &j, &k); // !!! & printf("You entered: %d %d %d\n", i, j, k); return EXIT_SUCCESS; @@ -203,6 +211,8 @@ int main() { # Nombres aléatoires: `rand()`, `srand()`{.C} (2/2) +\footnotesize + ``` $ man 3 rand The rand() function returns a pseudo-random integer @@ -220,10 +230,43 @@ int main() { srand(0); // every run will be identical srand(time(NULL)); // every run will be be different for (int i = 0; i < 50; ++i) { - printf("%d\n", rand() % 50); + printf("%d\n", rand() % 50); // à quoi sert % 50? } return EXIT_SUCCESS; } ``` +# Le cas du nombre secret + +## But du programme + +* Faire chercher un nombre aléatoire, +* Le nombre est compris entre 0 et une borne max. + +## Quelles sont les étapes? + + +1. Donner une borne maximale à l'ordinateur, MAX. + +. . . + +2. Faire tirer un nombre aléatoire à l'ordinateur entre 0 et MAX-1. + +. . . + +3. Le·la joueur·se joue un nombre. + +. . . + +4. L'ordinateur vérifie la réponse: + + * Si correct le jeu est fini, + * Sinon indiquer si le nombre secret est plus grand ou plus petit et revenir à 3. + +# La fin + +1. Y a-t-il des questions? + +. . +2. Si oui, répondre et revenir à 1, sinon quitter le cours. -- GitLab