Vous êtes sur la page 1sur 11

www.almohandiss.

com

Les tableaux
Algorithmique et Programmation en C Chapitre 5 Karim Bouzoubaa
Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing www.almohandiss.com of 9.
1

Chapitre 5 : Les tableaux

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

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

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

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

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

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

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

NOTE : nom du tableau Un tableau de 5 lments (indices de 0 4)


NOTE[2] = 12

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

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

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

www.almohandiss.com of 9.
6

Tableau

www.almohandiss.com

Un algo pour calculer lcart type de n valeurs


Algorithme type "calcule de lecart
som = 0 Pour i = 0 MAX-1 { som = som + (X[i] - moy)2 } ec = racine(som / MAX) Afficher (lecart type est:, ec) Fin

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,

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

www.almohandiss.com of 9.
7

Tableau 2 dimensions

www.almohandiss.com

Algo qui lit et affiche une matrice et la somme de ses lments

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

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

www.almohandiss.com of 9.
8

Mthodes de recherche

www.almohandiss.com

Rechercher si un lment existe ou non dans le tableau


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

Trouve type booleen

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

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

www.almohandiss.com of 9.
9

Mthodes de tri

www.almohandiss.com

Tri: opration qui consiste ordonner les lments en ordre squentiel


Tri par extraction Tri par change Tri bulles


Pour i = 0 MAX-1 { indMin = 1

Algorithme tri par extraction Const: MAX = 5, GRANDE = 9999 Entres entier : X, : Y i, tableau j, de MAX type

Pour j = 1 MAX-1 { si (X[j]<X[indMin])

Auxilieres entier Traitement:


Dbut

indMin

indMin = i } Y[i] = X[indMin] } Pour i = 0 MAX-1 { afficher(Y[i]) } Fin


www.almohandiss.com of 9.
10

Pour i = 0 MAX-1 { lire(X[i]) }

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

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

Karim Bouzoubaa Ecole Mohammadia dIngnieurs Ing

www.almohandiss.com of 9.
11

Vous aimerez peut-être aussi