com
Les tableaux
Algorithmique et Programmation en C Chapitre 5 Karim Bouzoubaa
Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing www.almohandiss.com of 9.
1
www.almohandiss.com
Objectifs
Comprendre la notion de tableaux et la situer par rapport aux autres types de donnes tre capable de dclarer, initialiser et utiliser un tableau Connatre la diffrence entre un tableau monodimensionnel et multidimensionnel Connatre les algorithmes de base de recherche et de tri dun tableau
Sommaire
Introduction La dclaration dun tableau Les tableaux simples Les tableaux multi-dimensions Les mthodes de recherche Les mthodes de tri
www.almohandiss.com of 9.
2
Introduction
www.almohandiss.com
Supposons une classe de 5 tudiants. Chaque tudiant a une note finale. On voudrait crire un algo qui affiche toutes les notes suprieures la moyenne de la classe.
Algorithme "notes sup la moyenne Entres : note1, note2, note3, note4, note5 type entier Auxilieres : i, moy type entier Traitement:
Dbut Lire note1, note2, note3, note4, note5 moy = (note1, note2, note3, note4, note5) / 5 si (note1 moy) Afficher(note1) si (note2 moy) Afficher(note2) si (note3 moy) Afficher(note3) si (note4 moy) Afficher(note4) si (note5 moy) Afficher(note5) Fin
www.almohandiss.com of 9.
3
Introduction
www.almohandiss.com
Lalgo nest valide que pour une classe de 5 tudiants Si on utilise une classe de 100 tudiants, dclarer 100 variables, 100 slection, etc. !!!
On associe plutot un seul nom lensemble des variables des notes dtudiants (NOTE) et lemploi dun indice (i) qui permet de reprer un lment quelconque de lensemble
NOTE[0] NOTE[1] NOTE[2] NOTE[3] NOTE[4]
16
15
12
15
18
www.almohandiss.com of 9.
4
Tableau
www.almohandiss.com
Un tableau est un ensemble ordonn qui contient un ensemble fixe dlments de mme type
NOTE[0] NOTE[1] NOTE[2] NOTE[3] NOTE[4]
16
15
12
15
18
www.almohandiss.com of 9.
5
Tableau
www.almohandiss.com
Algorithme "notes sup la moyenne Const: MAX = 5 Entres : NOTE tableau de MAX entier Auxilieres : i, som type entier, Traitement:
Dbut som = 0 Pour i = 0 MAX-1 { lire(NOTE[i]) som = som + NOTE[i] } moy = som / MAX Pour i = 0 MAX-1 { si (NOTE[i] moy) Afficher(NOTE[i]) } Fin
moy reel
www.almohandiss.com of 9.
6
Tableau
www.almohandiss.com
Const: MAX = 5 Entres : X tableau de MAX entier Auxilieres : moy, ec reel Traitement:
Dbut som = 0 Pour i = 0 MAX-1 { lire(X[i]) som = som + X[i] } moy = som / MAX
i,
som
type
entier,
www.almohandiss.com of 9.
7
Tableau 2 dimensions
www.almohandiss.com
Algorithme matrice Const: MAX1 = 5, MAX2 = 8 Entres : MAT tableau de MAX1*MAX2 entier Auxilieres : i,j, som type entier Traitement:
Dbut som = 0 Pour i = 0 MAX1-1 { Pour j = 0 MAX2-1 { lire(MAT[i][j]) som = som + MAT[i][j]} } }
Pour i = 0 MAX1-1 { Pour j = 0 MAX2-1 { Afficher (MAT[i][j]) } } Afficher (la somme est:, som) Fin
www.almohandiss.com of 9.
8
Mthodes de recherche
www.almohandiss.com
Recherche linaire: facile, O(n) Recherche dichotomique: O(logn), prix: trier le tabeau
i = 1 trouve = faux TQ (trouve = faux ET i < MAX) { si (X[i]==val) trouve = vrai
Algorithme recherche lineaire Const: MAX = 5 Entres : X tableau de MAX entier Auxilieres : i, val type entier
sinon i = i + 1
Traitement:
Dbut Pour i = 0 MAX-1 { lire(X[i]) } lire val
} si (trouve = faux) Afficher(elt trouve a pos:, i) sinon Afficher (elt non trouve) Fin
www.almohandiss.com of 9.
9
Mthodes de tri
www.almohandiss.com
Algorithme tri par extraction Const: MAX = 5, GRANDE = 9999 Entres entier : X, : Y i, tableau j, de MAX type
indMin
Exercices
www.almohandiss.com
1. Algo qui calcule la somme des lments dun tableau 2. Algo qui dtermine si une matrice carre est la matrice unit 3. Algo de recherche dichotomique 4. Algo de tri par change
www.almohandiss.com of 9.
11