diff --git a/slides/Makefile b/slides/Makefile index 1403418496850cfbaf6c61fbbe3ec3c5ef0455d5..cb71e27eda6537b5443f35fae0e6d5da551a8fd2 100644 --- a/slides/Makefile +++ b/slides/Makefile @@ -1,4 +1,5 @@ PDFOPTIONS = -t beamer +PDFOPTIONS += -F pantable PDFOPTIONS += -F mermaid-filter PDFOPTIONS += --highlight-style my_highlight.theme PDFOPTIONS += --pdf-engine xelatex diff --git a/slides/cours_3.md b/slides/cours_3.md index e0be64f4fbf6cd40bbb596a6ae2010f34fa4832c..c253236036492253ff2ad468e932d7bb76827ae4 100644 --- a/slides/cours_3.md +++ b/slides/cours_3.md @@ -1,13 +1,28 @@ --- title: "Introduction aux algorithmes" date: "2021-10-06" ---- +patat: + eval: + tai: + command: fish + fragment: false + replace: true + ccc: + command: fish + fragment: false + replace: true + images: + backend: auto +... # Quelques algorithmes simples ## Quelques algorithmes supplémentaires -- Remplissage d'un tableau et recherche de la valeur minimale +* Remplissage d'un tableau et recherche de la valeur minimal +* Anagrammes +* Palindromes +* Crible d'ératosthène # Collections: tableaux statiques @@ -67,13 +82,14 @@ date: "2021-10-06" . . . - ```C - #define SIZE 10 - double tab[SIZE]; - for (int i = 0; i < SIZE; ++i) { - tab[i] = rand() / (double)RAND_MAX * 10.0 - 5.0; // double [-5;5] - } - ``` +```C +#define SIZE 10 +double tab[SIZE]; +for (int i = 0; i < SIZE; ++i) { + tab[i] = rand() / (double)RAND_MAX * 10.0 - 5.0; + // tab[i] contient un double dans [-5;5] +} +``` # Recherche du minimum dans un tableau (1/2) @@ -178,9 +194,18 @@ char str[] = "HELLO !"; Est représenté par -| `H` | `E` | `L` | `L` | `O` | | `!` | `\0`| -|------|------|------|------|------|------|------|-----| -| `72` | `69` | `76` | `76` | `79` | `32` | `33` | `0` | +| `char` | `H` | `E` | `L` | `L` | `O` | | `!` | `\0`| +|---------|------|------|------|------|------|------|------|-----| +| `ASCII` | `72` | `69` | `76` | `76` | `79` | `32` | `33` | `0` | + +. . . + +## A quoi sert le `\0`? + +. . . + +Permet de connaître la fin de la chaîne de caractères (pas le cas des autres +sortes de tableaux). # Syntaxe @@ -244,38 +269,210 @@ lettres mais dans un ordre différent. 1. Trier les deux mots. 2. Vérifier s'ils contiennent les mêmes lettres. +## Implémentation en live (offerte par HepiaVPN) + ```C -void tri(char tab[]); -int strlen(char mot[]) { - int i = 0; - while (mot[i] != '\0') { - i++; +int main() { // pseudo C + tri(mot1); + tri(mot2); + if egalite(mot1, mot2) { + // anagrammes + } else { + // pas anagrammes } - return i; } -bool is_equal(char mot1[], char mot2[]) { - if (strlen(mot1) != strlen(mot2)) { +``` + +# Les palindromes + +Mot qui se lit pareil de droite à gauche que de gauche à droite: + +. . . + +* rotor, kayak, ressasser, ... + +## Problème: proposer un algorithme pour détecter un palindrome (3min) + +. . . + +## Solution 1 + +```C +while (first_index != last_index { + if mot[first_index] != mot [last_index] { return false; } - for (int i = 0; i < strlen(mot1); ++i) { - if (mot1[i] != mot2[i]) { - return false; - } - } - return true; + first_index += 1; + last_index += 1; } +return true; +``` + +. . . + +## Solution 2 + +```C +mot_tmp = revert(mot); +return mot == mot_tmp; +``` + +# Crible d'Ératosthène + +Algorithme de génération de nombres premiers. + +## Exercice + +* À l'aide d'un tableau de booléens, +* Générer les nombres premiers plus petits qu'un nombre $N$ + +## Pseudo-code + +* Par groupe de trois, réfléchir à un algorithme. + +## Programme en C +* Implémenter l'algorithme et le poster sur le salon `Element`. + +# Crible d'Ératosthène: solution + +\footnotesize + +## Une solution possible + +```C +#include <stdio.h> +#include <stdbool.h> +#define SIZE 51 int main() { - char mot1[] = "tutut"; - char mot2[] = "tutut"; - printf("%s et %s ", mot1, mot2); - tri(mot1); - tri(mot2); - boll anagramme = is_equal(mot1, mot2); - if (anagramme) { - printf("sont des anagramme!"); - } else { - printf("ne sont pas des anagramme!"); + bool tab[SIZE]; + for (int i=0;i<SIZE;i++) { + tab[i] = true; + } + for (int i = 2; i < SIZE; i++) { + if (tab[i]) { + printf("%d ", i); + int j = i; + while (true) { + j += i; + if (j >= SIZE) { + break; + } + tab[j] = false; + } + } + } + printf("\n"); +} +``` + +# Tableau à deux dimensions (1/4) + +## Mais qu'est-ce donc? + +. . . + +* Un tableau où chaque cellule est un tableau. + +## Quels cas d'utilisation? + +. . . + +* Tableau à double entrée; +* Image; +* Écran (pixels); +* Matrice (mathématique); + +# Tableau à deux dimensions (2/4) + +## Exemple: tableau à 3 lignes et 4 colonnes d'entiers + ++-----------+-----+-----+-----+-----+ +| `indices` | `0` | `1` | `2` | `3` | ++-----------+-----+-----+-----+-----+ +| `0` | `7` | `4` | `7` | `3` | ++-----------+-----+-----+-----+-----+ +| `1` | `2` | `2` | `9` | `2` | ++-----------+-----+-----+-----+-----+ +| `2` | `4` | `8` | `8` | `9` | ++-----------+-----+-----+-----+-----+ + +## Syntaxe + +```C +int tab[3][4]; // déclaration d'un tableau 4x3 +tab[2][1]; // accès à la case 2, 1 +tab[2][1] = 14; // assignation de 14 à la position 2, 1 +``` + +# Tableau à deux dimensions (3/4) + +## Exercice: déclarer et initialiser aléatoirement un tableau `50x100` + +. . . + +```C +#define NX 50 +#define NY 100 +int tab[NX][NY]; +for (int i = 0; i < NX; ++i) { + for (int j = 0; j < NY; ++j) { + tab[i][j] = rand() % 256; // 256 niveaux de gris + } +} +``` + +## Exercice: afficher le tableau + +. . . + +```C +for (int i = 0; i < NX; ++i) { + for (int j = 0; j < NY; ++j) { + printf("%d ", tab[i][j]); } + printf("\n"); } ``` + +# Tableau à deux dimensions (4/4) + +## Attention + +* Les éléments ne sont **jamais** initialisés. +* Les bornes ne sont **jamais** vérifiées. + + ```C + int tab[3][2] = { {1, 2}, {3, 4}, {5, 6} }; + printf("%d\n", tab[2][1]); // affiche? + printf("%d\n", tab[10][9]); // affiche? + printf("%d\n", tab[3][1]); // affiche? + ``` + +# La couverture de la reine + +* Aux échecs la reine peut se déplacer horizontalement et verticalement +* Pour un échiquier `5x6`, elle *couvre* les cases comme ci-dessous + ++-----+-----+-----+-----+-----+-----+-----+ +| ` ` | `0` | `1` | `2` | `3` | `4` | `5` | ++-----+-----+-----+-----+-----+-----+-----+ +| `0` | `*` | ` ` | `*` | ` ` | `*` | ` ` | ++-----+-----+-----+-----+-----+-----+-----+ +| `1` | ` ` | `*` | `*` | `*` | ` ` | ` ` | ++-----+-----+-----+-----+-----+-----+-----+ +| `2` | `*` | `*` | `R` | `*` | `*` | `*` | ++-----+-----+-----+-----+-----+-----+-----+ +| `3` | ` ` | `*` | `*` | `*` | ` ` | ` ` | ++-----+-----+-----+-----+-----+-----+-----+ +| `4` | `*` | ` ` | `*` | ` ` | `*` | ` ` | ++-----+-----+-----+-----+-----+-----+-----+ + +## Exercice + +* En utilisant les conditions, les tableaux à deux dimensions, et des + `char` uniquement. +* Implémenter un programme qui demande à l'utilisateur d'entrer les + coordonnées de la reine et affiche un tableau comme ci-dessus. + +## Poster le résultat sur `Element`