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
joachim.bach
cours
Commits
e50b8963
Verified
Commit
e50b8963
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
ajout tableaux
parent
44c88e0d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/tri_selection/tri.c
+26
-0
26 additions, 0 deletions
examples/tri_selection/tri.c
slides/cours_2.md
+117
-8
117 additions, 8 deletions
slides/cours_2.md
with
143 additions
and
8 deletions
examples/tri_selection/tri.c
0 → 100644
+
26
−
0
View file @
e50b8963
#include
<stdio.h>
#include
<stdlib.h>
#define SIZE 10
void
find_min
(
double
tab
[],
int
i0
,
int
*
ind
)
{
double
min
=
tab
[
i0
];
*
ind
=
i0
;
for
(
int
i
=
i0
+
1
;
i
<
SIZE
;
++
i
)
{
if
(
min
>
tab
[
i
])
{
*
ind
=
i
;
min
=
tab
[
i
];
}
}
}
int
main
()
{
double
tab
[
SIZE
];
for
(
int
i
=
0
;
i
<
SIZE
;
++
i
)
{
tab
[
i
]
=
rand
()
/
(
double
)
RAND_MAX
;
}
for
(
int
i
=
0
;
i
<
SIZE
-
1
;
++
i
)
{
double
}
}
This diff is collapsed.
Click to expand it.
slides/cours_2.md
+
117
−
8
View file @
e50b8963
...
@@ -374,23 +374,132 @@ void main() {
...
@@ -374,23 +374,132 @@ void main() {
# Collections: tableaux statiques
# Collections: tableaux statiques
*
Collection d'objets de même type dont le nombre est connu à la
*
Objets de même type: leur nombre est
**connu à la compilation**
;
compilation;
*
Stockés contigüement en mémoire (très efficace);
*
Stockés contigüement en mémoire, sur la pile;
```C
```C
#define SIZE 10
int entiers[] = {2, 1, 4, 5, 7}; // taille 5, initialisé
int entiers[] = {2, 1, 4, 5, 7}; // taille 5, initialisé
int tab[3]; // taille 3, non initialisé
int tab[3]; // taille 3, non initialisé
float many_floats[SIZE]; // taille 10, non initialisé
```
```
*
Les indices sont numérotés de
`0`
à
`taille-1`
.
*
Les indices sont numérotés de
`0`
à
`taille-1`
;
```C
```C
int premier = entier[0];
int premier = entier[0];
// premier = 2
int dernier = entier[4];
int dernier = entier[4];
// dernier = 7
```
```
*
Les tableaux sont
**non-initialisés**
par défaut.
*
Les tableaux sont
**non-initialisés**
par défaut;
*
Les bornes ne sont
**jamais**
vérifiées.
```C
```C
int indetermine = tab[1];
int indetermine_1 = tab[1]; // undefined behavior
int indetermine_2 = entiers[5]; // UB
```
```
# Remarques
*
Depuis
`C99`
possibilité d'avoir des tableaux dont la taille est
*
inconnue à
la compilation
*
;
```C
int size;
scanf("%d", &size);
char string[size];
```
. . .
*
Considéré comme une mauvaise pratique: que se passe-t-il si
`size == 1e9`
?
*
On préfère utiliser l'allocation
**dynamique**
de mémoire pour ce genre de
cas-là (spoiler du futur du cours).
# Initialisation
*
Les variables ne sont
**jamais**
initialisées en
`C`
par défaut.
*
Question: Que contient le tableau suivant?
```C
double tab[4];
```
. . .
*
Réponse: On en sait absolument rien!
*
Comment initialiser un tableau?
. . .
```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]
}
```
# Recherche du minimum dans un tableau (1/2)
## Problématique
Trouver le minimum d'un tableau et l'indice de l'élément le plus petit.
## Écrire un pseudo-code résolvant ce problème (groupe de 3), 2min
. . .
```
C
index = 0
min = tab[0]
for i in [1; SIZE] {
if min > tab[i] {
min = tab[i]
index = i
}
}
```
# Recherche du minimum dans un tableau (2/2)
## Implémenter ce bout de code en C (groupe de 3), 4min
. . .
```
C
int index = 0;
float min = tab[0];
for (int i = 1; i < SIZE; ++i) {
if min > tab[i] {
min = tab[i];
index = i;
}
}
```
# Tri par sélection (1/2)
## Problématique
Trier un tableau par ordre croissant.
## Idée d'algorithme
```
C
ind = 0
boucle (ind < SIZE) {
Trouver le minimum du tableau, tab_min[ind:SIZE].
Échanger tab_min avec tab[ind]
ind += 1
}
```
# Tri par sélection (2/2)
## Implémentation par groupe de 3
# La fin
## Des questions?
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