Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
algorithmique
cours
Commits
c36bce8a
Verified
Commit
c36bce8a
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added 2d tables
parent
04ff3346
No related branches found
No related tags found
No related merge requests found
Pipeline
#14577
failed
3 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
slides/Makefile
+1
-0
1 addition, 0 deletions
slides/Makefile
slides/cours_3.md
+233
-36
233 additions, 36 deletions
slides/cours_3.md
with
234 additions
and
36 deletions
slides/Makefile
+
1
−
0
View file @
c36bce8a
PDFOPTIONS
=
-t
beamer
PDFOPTIONS
+=
-F
pantable
PDFOPTIONS
+=
-F
mermaid-filter
PDFOPTIONS
+=
--highlight-style
my_highlight.theme
PDFOPTIONS
+=
--pdf-engine
xelatex
...
...
This diff is collapsed.
Click to expand it.
slides/cours_3.md
+
233
−
36
View file @
c36bce8a
---
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
\f
ootnotesize
## 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`
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment