Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
1 result

Target

Select target project
  • algorithmique/cours
  • aurelien.boyer/cours
  • jeremy.meissner/cours
  • radhwan.hassine/cours
  • yassin.elhakoun/cours-algo
  • gaspard.legouic/cours
  • joachim.bach/cours
  • gabriel.marinoja/algo-cours
  • loic.lavorel/cours
  • iliya.saroukha/cours
  • costanti.volta/cours
  • jacquesw.ndoumben/cours
12 results
Select Git revision
  • master
1 result
Show changes
Commits on Source (737)
Showing
with 1351 additions and 10 deletions
*.o
*.xopp*
slides/package-lock.json
slides/package.json
slides/.vscode/settings.json
.vscode/settings.json
slides/node_modules/.package-lock.json
image: omalaspinas/pandoc:latest
image: omalaspinas/c_pandoc:latest
variables:
GIT_SUBMODULE_STRATEGY: recursive
......@@ -9,8 +9,8 @@ before_script:
## Install ssh-agent if not already installed, it is required by Docker.
## (change apt-get to yum if you use an RPM-based image)
##
- 'which ssh-agent || (pacman -S --noconfirm openssh)'
- 'which rsync || (pacman -S --noconfirm rsync)'
- 'which ssh-agent || (apk add --update openssh-client)'
- 'which rsync || (apk add --update rsync)'
##
......@@ -38,8 +38,6 @@ before_script:
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
build_only:
script:
- cd slides
......@@ -47,6 +45,11 @@ build_only:
- make deploy
- rsync -avz algo_cours ur1bg_malas@ur1bg.ftp.infomaniak.com:web/malaspinas/
build_examples:
script:
- cd source_codes
- make
build_artifacts:
script:
- cd slides
......
# Remerciements et contributions
Merci aux contributeurs suivants pour leurs efforts (dans un ordre alphabétique):
* P. Albuquerque
* J. Bach
* A. Boyer
* M. Corboz
* M. Divià
* Y. El Hakouni
* A. Escribano
* P. Kunzli
* G. Legouic
* G. Marino Jarrin
* H. Radhwan
* I. Saroukhanian
* C. Volta
*.o
main
CC=gcc
CFLAGS=-g -std=gnu11 -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak -fsanitize=undefined
LDFLAGS=-fsanitize=address -fsanitize=leak -fsanitize=undefined
SOURCES=$(wildcard *.c)
OBJECTS=$(SOURCES:.c=.o)
TARGET = main
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $^ -o $@ $(LDFLAGS)
hm.c: hm.h
.PHONY = clean
clean:
rm -f $(OBJECTS) $(TARGET)
#include "hm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int hash(char *key, int capacity) {
int h = 0;
for (size_t i = 0; i < strlen(key); ++i) {
h = (h + key[i] * 43) % capacity;
}
return h;
}
static int rehash(char *key) {
return 1;
}
static int find_index(hm h, char *key) {
int i = hash(key, h.capacity);
int init = i;
while (h.table[i].state != empty &&
strncmp(h.table[i].key, key, MAX_LEN) != 0) {
i = (i + rehash(key)) % h.capacity;
if (i == init) {
return -1;
}
}
return i;
}
void hm_init(hm *h, int capacity) {
h->table = malloc(capacity * sizeof(cell_t));
h->capacity = capacity;
for (int i = 0; i < h->capacity; ++i) {
h->table[i].state = empty;
}
}
void hm_destroy(hm *h) {
free(h->table);
h->table = NULL;
h->capacity = -1;
}
bool hm_set(hm *h, char *key, char *value) {
int i = hash(key, h->capacity);
int init = i;
while (h->table[i].state == occupied &&
strncmp(h->table[i].key, key, MAX_LEN) != 0) {
i = (i + rehash(key)) % h->capacity;
if (i == init) {
return false;
}
}
if (strncpy(h->table[i].key, key, MAX_LEN) == NULL) {
return false;
};
if (strncpy(h->table[i].value, value, MAX_LEN) == NULL) {
return false;
};
h->table[i].state = occupied;
return true;
}
char *hm_get(hm h, char *key) {
int i = find_index(h, key);
return (i >= 0 && h.table[i].state == occupied) ? h.table[i].value : NULL;
}
char *hm_remove(hm *h, char *key) {
int i = find_index(*h, key);
if (i >= 0 && h->table[i].state == occupied) {
h->table[i].state = deleted;
return h->table[i].value;
} else {
return NULL;
}
}
void hm_print(hm h) {
for (int i = 0; i < h.capacity; ++i) {
if (h.table[i].state == occupied) {
printf("[%d], key: %s, value: %s\n", i, h.table[i].key,
h.table[i].value);
} else {
printf("[%d], key: none, value: none\n", i);
}
}
}
#ifndef _HM_H_
#define _HM_H_
#include <stdbool.h>
#define MAX_LEN 80
typedef enum { empty, occupied, deleted } state_t;
typedef struct _cell_t {
state_t state;
char key[MAX_LEN];
char value[MAX_LEN];
} cell_t;
typedef struct _hm {
int capacity;
cell_t *table;
} hm;
void hm_init(hm *h, int capacity);
void hm_destroy(hm *h);
bool hm_set(hm *h, char *key, char *value);
char *hm_get(hm h, char *key);
char *hm_remove(hm *h, char *key);
void hm_print(hm h);
#endif
#include "hm.h"
#include <stdio.h>
int main() {
hm h;
hm_init(&h, 5);
for (int i = 0; i < 7; ++i) {
char key[MAX_LEN], value[MAX_LEN];
printf("Enter key please:\n");
scanf("%s", key);
printf("Enter value please:\n");
scanf("%s", value);
hm_set(&h, key, value);
hm_print(h);
}
printf("DELETE VALUES!\n");
for (int i = 0; i < 2; ++i) {
char key[MAX_LEN];
printf("Enter key please:\n");
scanf("%s", key);
printf("Value: ");
char *value = hm_remove(&h, key);
if (value == NULL) {
printf("Key not found.\n");
} else {
printf("Value is %s.\n", value);
}
hm_print(h);
}
printf("SEARCH VALUES!\n");
for (int i = 0; i < 2; ++i) {
char key[MAX_LEN];
printf("Enter key please:\n");
scanf("%s", key);
printf("Value: ");
char *value = hm_get(h, key);
if (value == NULL) {
printf("Key not found.\n");
} else {
printf("Value is %s.\n", value);
}
}
hm_destroy(&h);
/* for (int i = 0; i < 6; ++i) { */
/* scanf("%s", key); */
/* scanf("%s", value); */
/* } */
}
@charset "UTF-8";
/* Import ET Book styles
adapted from https://github.com/edwardtufte/et-book/blob/gh-pages/et-book.css */
@font-face {
font-family: "et-book";
src: url("et-book/et-book-roman-line-figures/et-book-roman-line-figures.eot");
src: url("et-book/et-book-roman-line-figures/et-book-roman-line-figures.eot?#iefix") format("embedded-opentype"), url("et-book/et-book-roman-line-figures/et-book-roman-line-figures.woff") format("woff"), url("et-book/et-book-roman-line-figures/et-book-roman-line-figures.ttf") format("truetype"), url("et-book/et-book-roman-line-figures/et-book-roman-line-figures.svg#etbookromanosf") format("svg");
font-weight: normal;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "et-book";
src: url("et-book/et-book-display-italic-old-style-figures/et-book-display-italic-old-style-figures.eot");
src: url("et-book/et-book-display-italic-old-style-figures/et-book-display-italic-old-style-figures.eot?#iefix") format("embedded-opentype"), url("et-book/et-book-display-italic-old-style-figures/et-book-display-italic-old-style-figures.woff") format("woff"), url("et-book/et-book-display-italic-old-style-figures/et-book-display-italic-old-style-figures.ttf") format("truetype"), url("et-book/et-book-display-italic-old-style-figures/et-book-display-italic-old-style-figures.svg#etbookromanosf") format("svg");
font-weight: normal;
font-style: italic;
font-display: swap;
}
@font-face {
font-family: "et-book";
src: url("et-book/et-book-bold-line-figures/et-book-bold-line-figures.eot");
src: url("et-book/et-book-bold-line-figures/et-book-bold-line-figures.eot?#iefix") format("embedded-opentype"), url("et-book/et-book-bold-line-figures/et-book-bold-line-figures.woff") format("woff"), url("et-book/et-book-bold-line-figures/et-book-bold-line-figures.ttf") format("truetype"), url("et-book/et-book-bold-line-figures/et-book-bold-line-figures.svg#etbookromanosf") format("svg");
font-weight: bold;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "et-book-roman-old-style";
src: url("et-book/et-book-roman-old-style-figures/et-book-roman-old-style-figures.eot");
src: url("et-book/et-book-roman-old-style-figures/et-book-roman-old-style-figures.eot?#iefix") format("embedded-opentype"), url("et-book/et-book-roman-old-style-figures/et-book-roman-old-style-figures.woff") format("woff"), url("et-book/et-book-roman-old-style-figures/et-book-roman-old-style-figures.ttf") format("truetype"), url("et-book/et-book-roman-old-style-figures/et-book-roman-old-style-figures.svg#etbookromanosf") format("svg");
font-weight: normal;
font-style: normal;
font-display: swap;
}
/* Tufte CSS styles */
html {
font-size: 15px;
}
body {
width: 87.5%;
margin-left: auto;
margin-right: auto;
padding-left: 12.5%;
font-family: et-book, Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", Georgia, serif;
background-color: #fffff8;
color: #111;
max-width: 1400px;
counter-reset: sidenote-counter;
}
h1 {
font-weight: 400;
margin-top: 4rem;
margin-bottom: 1.5rem;
font-size: 3.2rem;
line-height: 1;
}
h2 {
font-style: italic;
font-weight: 400;
margin-top: 2.1rem;
margin-bottom: 1.4rem;
font-size: 2.2rem;
line-height: 1;
}
h3 {
font-style: italic;
font-weight: 400;
font-size: 1.7rem;
margin-top: 2rem;
margin-bottom: 1.4rem;
line-height: 1;
}
hr {
display: block;
height: 1px;
width: 55%;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
p.subtitle {
font-style: italic;
margin-top: 1rem;
margin-bottom: 1rem;
font-size: 1.8rem;
display: block;
line-height: 1;
}
.numeral {
font-family: et-book-roman-old-style;
}
.danger {
color: red;
}
article {
padding: 5rem 0rem;
}
section {
padding-top: 1rem;
padding-bottom: 1rem;
}
p,
dl,
ol,
ul {
font-size: 1.4rem;
line-height: 2rem;
}
p {
margin-top: 1.4rem;
margin-bottom: 1.4rem;
padding-right: 0;
vertical-align: baseline;
}
/* Chapter Epigraphs */
div.epigraph {
margin: 5em 0;
}
div.epigraph > blockquote {
margin-top: 3em;
margin-bottom: 3em;
}
div.epigraph > blockquote,
div.epigraph > blockquote > p {
font-style: italic;
}
div.epigraph > blockquote > footer {
font-style: normal;
}
div.epigraph > blockquote > footer > cite {
font-style: italic;
}
/* end chapter epigraphs styles */
blockquote {
font-size: 1.4rem;
}
blockquote p {
width: 55%;
margin-right: 40px;
}
blockquote footer {
width: 55%;
font-size: 1.1rem;
text-align: right;
}
section > p,
section > footer,
section > table {
width: 55%;
}
/* 50 + 5 == 55, to be the same width as paragraph */
section > dl,
section > ol,
section > ul {
width: 50%;
-webkit-padding-start: 5%;
}
dt:not(:first-child),
li:not(:first-child) {
margin-top: 0.25rem;
}
figure {
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
max-width: 55%;
-webkit-margin-start: 0;
-webkit-margin-end: 0;
margin: 0 0 3em 0;
}
figcaption {
float: right;
clear: right;
margin-top: 0;
margin-bottom: 0;
font-size: 1.1rem;
line-height: 1.6;
vertical-align: baseline;
position: relative;
max-width: 40%;
}
figure.fullwidth figcaption {
margin-right: 24%;
}
/* Links: replicate underline that clears descenders */
a:link,
a:visited {
color: inherit;
}
.no-tufte-underline:link {
background: unset;
text-shadow: unset;
}
a:link, .tufte-underline, .hover-tufte-underline:hover {
text-decoration: none;
background: -webkit-linear-gradient(#fffff8, #fffff8), -webkit-linear-gradient(#fffff8, #fffff8), -webkit-linear-gradient(currentColor, currentColor);
background: linear-gradient(#fffff8, #fffff8), linear-gradient(#fffff8, #fffff8), linear-gradient(currentColor, currentColor);
-webkit-background-size: 0.05em 1px, 0.05em 1px, 1px 1px;
-moz-background-size: 0.05em 1px, 0.05em 1px, 1px 1px;
background-size: 0.05em 1px, 0.05em 1px, 1px 1px;
background-repeat: no-repeat, no-repeat, repeat-x;
text-shadow: 0.03em 0 #fffff8, -0.03em 0 #fffff8, 0 0.03em #fffff8, 0 -0.03em #fffff8, 0.06em 0 #fffff8, -0.06em 0 #fffff8, 0.09em 0 #fffff8, -0.09em 0 #fffff8, 0.12em 0 #fffff8, -0.12em 0 #fffff8, 0.15em 0 #fffff8, -0.15em 0 #fffff8;
background-position: 0% 93%, 100% 93%, 0% 93%;
}
@media screen and (-webkit-min-device-pixel-ratio: 0) {
a:link, .tufte-underline, .hover-tufte-underline:hover {
background-position-y: 87%, 87%, 87%;
}
}
a:link::selection,
a:link::-moz-selection {
text-shadow: 0.03em 0 #b4d5fe, -0.03em 0 #b4d5fe, 0 0.03em #b4d5fe, 0 -0.03em #b4d5fe, 0.06em 0 #b4d5fe, -0.06em 0 #b4d5fe, 0.09em 0 #b4d5fe, -0.09em 0 #b4d5fe, 0.12em 0 #b4d5fe, -0.12em 0 #b4d5fe, 0.15em 0 #b4d5fe, -0.15em 0 #b4d5fe;
background: #b4d5fe;
}
/* Sidenotes, margin notes, figures, captions */
img {
max-width: 100%;
}
.sidenote,
.marginnote {
float: right;
clear: right;
margin-right: -60%;
width: 50%;
margin-top: 0.3rem;
margin-bottom: 0;
font-size: 1.1rem;
line-height: 1.3;
vertical-align: baseline;
position: relative;
}
.sidenote-number {
counter-increment: sidenote-counter;
}
.sidenote-number:after,
.sidenote:before {
font-family: et-book-roman-old-style;
position: relative;
vertical-align: baseline;
}
.sidenote-number:after {
content: counter(sidenote-counter);
font-size: 1rem;
top: -0.5rem;
left: 0.1rem;
}
.sidenote:before {
content: counter(sidenote-counter) " ";
font-size: 1rem;
top: -0.5rem;
}
blockquote .sidenote,
blockquote .marginnote {
margin-right: -82%;
min-width: 59%;
text-align: left;
}
div.fullwidth,
table.fullwidth {
width: 100%;
}
div.table-wrapper {
overflow-x: auto;
font-family: "Trebuchet MS", "Gill Sans", "Gill Sans MT", sans-serif;
}
.sans {
font-family: "Gill Sans", "Gill Sans MT", Calibri, sans-serif;
letter-spacing: .03em;
}
code, pre > code {
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 1.0rem;
line-height: 1.42;
-webkit-text-size-adjust: 100%; /* Prevent adjustments of font size after orientation changes in iOS. See https://github.com/edwardtufte/tufte-css/issues/81#issuecomment-261953409 */
}
.sans > code {
font-size: 1.2rem;
}
h1 > code,
h2 > code,
h3 > code {
font-size: 0.80em;
}
.marginnote > code,
.sidenote > code {
font-size: 1rem;
}
pre > code {
font-size: 0.9rem;
width: 52.5%;
margin-left: 2.5%;
overflow-x: auto;
display: block;
}
pre.fullwidth > code {
width: 90%;
}
.fullwidth {
max-width: 90%;
clear:both;
}
span.newthought {
font-variant: small-caps;
font-size: 1.2em;
}
input.margin-toggle {
display: none;
}
label.sidenote-number {
display: inline;
}
label.margin-toggle:not(.sidenote-number) {
display: none;
}
.iframe-wrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.iframe-wrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
@media (max-width: 760px) {
body {
width: 84%;
padding-left: 8%;
padding-right: 8%;
}
hr,
section > p,
section > footer,
section > table {
width: 100%;
}
pre > code {
width: 97%;
}
section > dl,
section > ol,
section > ul {
width: 90%;
}
figure {
max-width: 90%;
}
figcaption,
figure.fullwidth figcaption {
margin-right: 0%;
max-width: none;
}
blockquote {
margin-left: 1.5em;
margin-right: 0em;
}
blockquote p,
blockquote footer {
width: 100%;
}
label.margin-toggle:not(.sidenote-number) {
display: inline;
}
.sidenote,
.marginnote {
display: none;
}
.margin-toggle:checked + .sidenote,
.margin-toggle:checked + .marginnote {
display: block;
float: left;
left: 1rem;
clear: both;
width: 95%;
margin: 1rem 2.5%;
vertical-align: baseline;
position: relative;
}
label {
cursor: pointer;
}
div.table-wrapper,
table {
width: 85%;
}
img {
width: 100%;
}
}
main
*.o
CC=gcc
CFLAGS=-g -std=gnu11 -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak -fsanitize=undefined
LDFLAGS=-fsanitize=address -fsanitize=leak -fsanitize=undefined
SOURCES=$(wildcard *.c)
OBJECTS=$(SOURCES:.c=.o)
TARGET = main
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $^ -o $@ $(LDFLAGS)
hashmap.c: hashmap.h
.PHONY = clean
clean:
rm -f $(OBJECTS) $(TARGET)
#include "hm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static bool is_not_allocated(hm h) {
return (h.table == NULL || h.capacity < 1 || h.size < 0);
}
static size_t hash(hm h, char key[MAX_LEN]) {
int index = 0;
for (size_t i = 0; i < strlen(key); ++i) {
index = (index + key[i] * 43) % h.capacity;
}
return index;
}
static int rehash(char *key) {
return 1;
}
static bool search_condition(hm h, char key[MAX_LEN], int index) {
return strncmp(h.table[index].key, key, MAX_LEN) == 0 &&
h.table[index].state == occupied;
}
static bool set_condition(hm h, char key[MAX_LEN], int index) {
return search_condition(h, key, index) || h.table[index].state != occupied;
}
static int find_index(
hm h, char key[MAX_LEN], bool (*condition)(hm, char[], int)) {
int try = 0;
int index = hash(h, key);
while (try < h.capacity) {
if (condition(h, key, index)) {
return index;
}
index = (index + rehash(key)) % h.capacity;
try += 1;
}
return -1;
}
static bool is_empty(hm h) {
return h.size == 0;
}
void hm_init(hm *h, int capacity) {
h->capacity = capacity;
h->size = 0;
h->table = malloc(h->capacity * sizeof(*h->table));
for (int i = 0; i < h->capacity; ++i) {
h->table[i].state = empty;
}
}
void hm_destroy(hm *h) {
h->capacity = -1;
h->size = -1;
free(h->table);
h->table = NULL;
}
bool hm_set(hm *h, char *key, char *value) {
if (is_not_allocated(*h)) {
return false;
}
int index = find_index(*h, key, set_condition);
if (index < 0) {
return false;
}
/* printf("%d\n", index); */
strncpy(h->table[index].value, value, MAX_LEN);
strncpy(h->table[index].key, key, MAX_LEN);
h->table[index].state = occupied;
h->size += 1;
return true;
}
bool hm_get(hm h, char *key, char *value) {
int index = find_index(h, key, search_condition);
if (index >= 0) {
strncpy(value, h.table[index].value, MAX_LEN);
return true;
}
return false;
}
bool hm_remove(hm *h, char *key, char *value) {
int index = find_index(*h, key, search_condition);
if (index >= 0) {
h->table[index].state = deleted;
strncpy(value, h->table[index].value, MAX_LEN);
h->size -= 1;
return true;
}
return false;
}
bool hm_search(hm h, char *key) {
int index = find_index(h, key, search_condition);
return (index >= 0);
}
void hm_print(hm h) {
if (is_not_allocated(h)) {
printf("Well this hashmap is not allocated n00b.\n");
}
if (is_empty(h)) {
printf("The hashmap is empty.\n");
}
for (int i = 0; i < h.capacity; ++i) {
if (h.table[i].state == occupied) {
printf("index: %d, key: %s, value: %s\n", i, h.table[i].key,
h.table[i].value);
} else {
printf("index: %d, key: {none}, value: {none}\n", i);
}
}
}
#ifndef _HM_H_
#define _HM_H_
#include <stdbool.h>
#define MAX_LEN 80
typedef enum { empty, occupied, deleted } state_t;
typedef struct _cell_t {
char key[MAX_LEN];
char value[MAX_LEN];
state_t state;
} cell_t;
typedef struct _hm {
cell_t *table;
int capacity;
int size;
} hm;
void hm_init(hm *h, int capacity);
void hm_destroy(hm *h);
bool hm_set(hm *h, char *key, char *value);
bool hm_get(hm h, char *key, char *value);
bool hm_remove(hm *h, char *key, char *value);
bool hm_search(hm h, char *key);
void hm_print(hm h);
#endif
#include "hm.h"
#include <stdio.h>
int main() {
char key[80], value[80];
hm h;
hm_init(&h, 5);
for (int i = 0; i < 6; ++i) {
scanf("%s", key);
scanf("%s", value);
hm_set(&h, key, value);
hm_print(h);
if (hm_search(h, "orestis")) {
printf("orestis found.\n");
}
if (hm_remove(&h, "orestis", value)) {
printf("orestis removed: %s.\n", value);
}
}
hm_destroy(&h);
}
*.log
*.aux
*.pdf
*.dvi
*.eps
# Algorithmes et structures de données 2019-20
# Algorithmes et structures de données 2020-21
Contenu du cours 2 du du 25.09.2019
Contenu du cours 2 du du 23.09.2020
*****
......@@ -89,6 +89,3 @@ Contenu du cours 2 du du 25.09.2019
- Tri par sélection avec illustration sur un exemple
- Algorithme de vérification que deux mots sont des anagrammes en utilisant un tri
## Palindrome avec parcours de boucle
## Le crible d'Eratosthène
# Algorithmes et structures de données 2020-21
Contenu du cours 3 du 30.09.2020
*****
## Tableaux à deux dimensions
- Image noir et blanc avec des booléens : initialisation
```C
int n = 8, m = 8;
bool image_nb[n][m];
for (int i=0;i<n;i++) {
for (int j=0;j<m;j++) {
image_nb[i][j] = true;
}
}
```
- Image en niveaux de gris : initialisation
```C
int n = 8, m = 8;
int image_gris[n][m];
for (int i=0;i<n;i++) {
for (int j=0;j<m;j++) {
image_gris[i][j] = rand()%256;
}
}
```
>
* Négatif d'une image à 256 niveaux de gris
- Appliquer à chaque pixel l'opération `pixel = 255 - pixel`
```C
for (int i=0;i<n;i++) {
for (int j=0;j<m;j++) {
image_gris[i][j] = 255 - ­image_gris[i][j];
}
}
```
## Type énuméré
- On définit un type énuméré `semaine, coin, mois, couleur, direction` en énumérant toutes ses valeurs possibles.
Explicitement
```C
enum semaine {lu,ma,me,je,ve,sa,di};
enum coin {cinq_cts=5,dix_cts=10,vingt_cts=20,euro=100};
enum mois {
jan=1,feb,mar,apr,mai,jun,jul,aug,sep,oct,nov,dec
};
enum couleur {RED,GREEN,BLUE};
enum direction {EST=2,OUEST=3,NORD=0,SUD=1};
```
> Les valeurs d’un type énuméré peuvent être considérées comme des entiers.
Sans précision particulière, le premier élément correspond à 0 et les suivants à 1, 2, etc.
Sinon on peut explicitement spécifier des valeurs.
Rien n’empêche d'attribuer plusieurs fois le même nombre.
## Opérateur de contrôle `switch`
- Comparaison entre une valeur et un ensemble de choix pour sélectionner une action à effectuer. Toutes les possibilités doivent être prises en compte.
```C
void main() {
type data = id1;
int val;
switch(data) {
case ID1: val = data+1; break;
case ID2: val = data*2; break;
case ID3: val = data/3.0; break;
};
println!(\"val =%d\\n\",val);
}
```
## Exemple de la « couverture de la reine »
```C
enum piece {SAFE,VULN,QUEEN};
int size = 8;
piece board[size][size];
// initialisation de l'échiquier
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
board[i][j] = SAFE;
}
}
int pos_reine[] = {3,4};
// couverture sans les diagonales !!!
for (int k=0;k<size;k++) {
board[k][pos_reine[1]] = VULN;
board[pos_reine[0]][k] = VULN;
}
board[pos_reine[0]][pos_reine[1]] = QUEEN;
```
## Matrices
1. Tableau 2D de nombres
2. Opérations arithmétiques
1. Addition : `C = A+B`
2. Soustraction : `C = A-B`
3. Multiplication : `C = A*B` (produit lignes/colonnes)
4. Puissance entière : `C = A`<sup>n</sup>
# Algorithmes et structures de données 2020-21
Contenu du cours 4 du 7.10.2020
*****
## Crible d’Eratosthène
- Retour sur les tableaux à 1 dimension
## Type article (enregistrement, record) : `struct`
- Un article permet de regrouper des types de nature différente (appelés champs) comme composant d'un type composé.
- Exemples de `struct`:
```C
struct fraction {
int num;
int den;
};
enum mois {
jan=1,feb,mar,apr,mai,jun,jul,aug,sep,oct,nov,dec
};
struct date {
int day;
mois month;
int year;
}
enum sexe {FEM, MASC, AUTRE};
struct employe {
char nom[80];
sexe genre;
int age;
float salaire;
}
```
## Fonction cube d'un nombre
```C
int cube(int x) {
return x*x*x;
}
```
## Fonction de parité
```C
bool est_pair(int n) {
return n%2 == 0;
}
```
## Plus Petit Commun Multiple (PPCM)
- Fonction PPCM: `int ppcm(int n,int m);`
## Plus Grand Commun Diviseur (PGCD)
- Fonction PGCD: `nt pgcd(int n,int m);`
## Paramètres de fonction
- Dans le langage C, les paramètres des fonctions sont toujours passés par copie.
Ainsi, la valeur d'une variable passée en paramètre à une fonction n'est pas modifiée,
puisque c'est une copie de cette variable qui est passée effectivement à la fonction.
Toutes les manipulations effectuées par la fonction le sont donc sur cette copie.
Celle-ci est détruite à la fin de l'exécution de la fonction, laissant la variable originelle inchangée.
- Pour que des modifications puissent subsister après l'exécution, on peut procéder de deux manières :
1. en retournant une valeur par la fonction ;
2. en passant en paramètre un pointeur : celui-ci donne accès à une
zone mémoire hors de la portée (scope) de la fonction, zone qui
peut donc être modifiée durablement.
- Fonction d'incrément
```C
void sans_effet(int x) {
x++;
}
void increment(int* x) {
(*x)++;
}
void main() {
int a = 2;
sans_effet(a);
printf("a=%d",a); // imprime 2, valeur de a inchangée
increment(&a);
printf(\"a=%d\",a); // imprime 3
}
```
## Zones d'allocation mémoire
- Un programme utilise deux types de zone mémoire lors de son exécution pour stocker les variables : la pile et le tas.
- A l'exécution d'un programme, les allocations de variables :
1. sur la pile, sont faites par le système d'exploitation
- dans ce cas, on parle de variables statiques
2. sur le tas, sont de la responsabilité de la programmeuse via un appel à la fonction `malloc()`
- dans ce cas, on parle de variables dynamiques
- La désallocation d'une variable statique survient lors de la sortie de sa portée (scope) et est donc automatique.
- Une variable dynamique doit être explicitement désallouée par la programmeuse avec un appel à la fonction `free()`.
- Il y a parfois une petite confusion entre la zone mémoire où réside un pointeur et celle où se situe l'espace pointé par le pointeur.
- Par exemple, l’instruction
`char* ptr_ch = malloc(3*sizeof(char));`
alloue un espace sur le tas pour 3 caractères, alors que la variable `ptr_ch` est allouée sur la pile.
## Pseudo-code du tri par sélection
```
procedure tri_selection(tableau t[0..n­-1])
begin
for i = 0 to n-­2 do
ind_min := index_min(t[i..n-­1])
if t[ind_min] ≠ t[i] then
echanger(t[i],t[ind_min])
end if
end for
end tri_selection
```
# Algorithmique et structures de données 2020-21
Contenu du cours 5 du 14.10.2020
******
## Le tri par insertion
- Algorithme appliqué au tableau: **4 7 6 1 2**
- 1ère étape : 4 **7** 6 1 2
- trouver la position `pos` de 7 dans le sous-tableau trié à 1 élément : **4**
- décaler de 1 les éléments de la position `pos=1` à la fin du sous-tableau
- insérer l'élément 7 en position `pos=1`
- 2ème étape : 4 7 **6** 1 2
- trouver la position `pos` de 6 dans le sous-tableau trié à 2 éléments : **4 7**
- décaler de 1 les éléments de la position `pos=1` à la fin du sous-tableau
- insérer l'élément 6 en position `pos=1`
- 3ème étape : 4 6 7 **1** 2
- trouver la position `pos` de 1 dans le sous-tableau trié à 3 éléments : **4 6 7**
- décaler de 1 les éléments de la position `pos=0` à la fin du sous-tableau
- insérer l'élément 1 en position `pos=0`
- 4ème étape : 1 4 6 7 **2**
- trouver la position `pos` de 2 dans le sous-tableau trié à 4 éléments : **1 4 6 7**
- décaler de 1 les éléments de la position `pos=1` à la fin du sous-tableau
- insérer l'élément 2 en position `pos=1`
- On obtient le tableau trié: **1 2 4 6 7**
## Tri à bulles
- Algorithme appliqué au tableau **4 7 6 2 1**
- Principe : on parcours le tableau depuis et on permute les éléments successifs s'ils sont dans le désordre
**4 7** 6 2 1
4 **7 6** 2 1
4 6 **7 2** 1
4 6 2 **7 1**
4 6 2 1 7
A la fin de cette traversée, le plus grand élément se trouve en dernière position.
- On applique à nouveau ce principe, mais sur le tableau allant de la 1ère à l'avant-dernière case du tableau
**4 6** 2 1 7
4 **6 2** 1 7
4 2 **6 1** 7
4 2 1 6 7
- Et ainsi de suite ...
- En 4 étapes nécessitant 4, puis 3, puis 2, et finalement 1, opérations de comparaison-échange, on obtient un tableau trié.
Donc en 4+3+2+1 = 5**·**4/2 = 10 opérations, on a un tableau trié.
- Plus généralement, un tableau à N éléments se trie en N(N-1)/2 opérations avec le tri à bulles.
## Type composé : `struct`
- **Exemple des chaînes de caractères**
- On peut définir un type pour les chaînes de caractères en stockant la longueur effective de la chaîne, c'est-à-dire la première occurrence du caractère `\0`
```C
struct _chaine {
char* str;
int len;
} chaine;
```
- Quelques implémentations de fonctions
```C
chaine chaine_build(char* phrase) {
chaine ch;
ch.len = length(phrase);
ch.str = malloc((ch.len+1)*sizeof(char));
for (int i=0;i<ch.len;i++) {
ch.str[i] = phrase[i];
}
return ch;
}
chaine deep_copy(chaine ch) {
return chaine_build(ch.str);
}
// Dangereux, car plusieurs pointeurs
// sur le même tableau de caractères !!!
chaine shallow_copy(chaine ch) {
chaine cpy;
cpy.len = ch.len;
cpy.str = ch.str;
return cpy;
}
// Si plusieurs pointeurs sur même tableau de caractères,
// alors autres pointeurs dessus ne seront pas mis à NULL
void chaine_free(chaine* ch) {
if (NULL != ch­>str)
free(ch­>str);
ch­>str = NULL;
}
ch­>len = -­1;
}
```
- **Illustration**
![Deep vs. shallow copy](./figures/fig_string_deep_shallow_copy.png)
## Récursivité
- Exemple de la factorielle : n ! = n·(n-1)·(n-2)·... ·3·2·1 = n·(n-1) !
Donc fact(n) = n·fact(n-1) (récursivité)
et fact(1) = 1 (condition d'arrêt)
```C
int fact(int n) {
if (n > 1) {
return n*fact(n­1);
} else {
return 1;
}
}
void main() {
int f = fact(4);
}
```
![Illustration](./figures/fig_recursivite_factorielle.png)
# Algorithmique et structures de données 2020-21
Contenu du cours 6 du 28.10.2020
*****
## Récursivité
- Exemple de la factorielle : n ! = n·(n-1)·(n-2)·... ·3·2·1 = n·(n-1) !
Donc fact(n) = n·fact(n-1) (récursivité)
et fact(1) = 1 (condition d'arrêt)
```C
int fact(int n) {
if (n > 1) {
return n*fact(n­1);
} else {
return 1;
}
}
void main() {
int f = fact(4);
}
```
![Illustration](./figures/fig_recursivite_factorielle.png)
- Exemple du PGCD
Algorithme d'Euclide pour le PGCD de 42 et 27
> 42 = 27·1 + 15
> 27 = 15·1 + 12
> 15 = 12·1 + 3
> 12 = 3·4 + 0
PGCD(42,27)=PGCD(27,15)=PGCD(15,12)=PGCD(12,3)=3
```C
int pgcd(int n,int m) {
if (n%m > 0) {
return pgcd(m,n%m);
} else {
return m;
}
}
```
![Illustration de la récursivité pour l'algorithme d'Euclide](./figures/fig_recursivite_pgcd_euclide.png)
- Exemple de l'écriture binaire
```C
void binaire(int n) {
printf("%d",n%2);
if (n/2 != 0) {
binaire(n/2);
} else {
printf("\n");
}
// printf("%d",n%2);
}
Binaire(13); // affiche 1 0 1 1 puis un retour à la ligne`
```
> > > $\hspace*{36mm} 2^0 2^1 2^2 2^3$
![Illustration de la récursivité pour l'écriture binaire](./figures/fig_recursivite_binaire.png)
- Que se passe-t-il si on décommente le deuxième `printf` ?
## Exemples et exercices de récursivité
- Algorithme du PPCM de deux nombres `n` et `m`
- `ppcm(mult_n,mult_m) = ppcm(mult_n + n, mult_m)`
si `mult_n < mult_m` (récursivité)
- `ppcm(mult_n,mult_m) = ppcm(mult_n, mult_m + m)`
si `mult_n > mult_m` (récursivité)
- `ppcm(mult_n,mult_m) = mult_n`
si `mult_n = mult_m` (condition d’arrêt)
- Puissance indienne
\begin{align*}
a^b & = a^{b/2}\cdot a^{b/2} & \textrm{ si $b$ est pair (récursivité)} \\
a^b & = a^{b-1}\cdot a & \textrm{ si $b$ est impair (récursivité)} \\
a^0 & = 1 & \textrm{ (condition d’arrêt)}
\end{align*}
- Suite de Fibonacci
\begin{align*}
a_n & = a_{n-1} + a_{n-2} & \textrm{ (récursivité)} \\
a_1 & = 1, a_0 = 0 & \textrm{ (condition d’arrêt)}
\end{align*}
## Problème des 8-reines
- Le but du problème des 8 reines est de placer 8 reines d'un jeu d'échecs sur un échiquier de $8 \times 8$ cases sans que les reines ne puissent se menacer mutuellement, conformément aux règles du jeu d'échecs.
- Ainsi, deux reines ne devraient jamais partager la même rangée, colonne, ou diagonale.
- Le problème se généralise au placement de N reines sur un échiquier de $N \times N$ cases. Pour $N=8$, il y a 92 solutions
- Il s'agit d'un exemple classique de problème de backtracking qui se programme avec la récursivité.
![Problème des 8-reines. Source : wikipedia, https://fr.wikipedia.org/wiki/Problème_des_huit_dames](./figures/fig_recursivite_8_reines.png)
\ No newline at end of file