You are on page 1of 7

Programmation C

Tableaux et boucles For


David Pointcheval

Plan

1 2 3

- Tableaux - Boucles for - Paramtre sur la ligne de commande

E-mail : David.Pointcheval@ens.fr Web : http://www.di.ens.fr/~pointche/enseignement/ensta

David Pointcheval LIENS - CNRS

Programmation C - ENSTA - 2

Limite des types de base


Dfinir autant de variables que de cases mmoires ncessaires Accder chaque variable, une par une, et par son nom en dur dans le programme pas dindexation possible
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 3 David Pointcheval LIENS - CNRS

Les tableaux

Dfinir sous un nom unique un groupe de cases mmoires de mme type Accder chaque case par sa position dans le tableau

Programmation C - ENSTA - 4

La mmoire
int tab[4]; dfinit un tableau de 4 entiers conscutifs tab

tab[0] tab[1] tab[2] tab[3]

Toutes sortes de tableaux


float tabF[10];
tabF, tableau de 10 floats

double tabD[100];
tabD, tableau de 100 doubles

char chaine[256];

chaine, chane de 256 caractres

int tabI[N];
tabI, tableau de N entiers
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 6

David Pointcheval LIENS - CNRS

Programmation C - ENSTA - 5

Taille constante
La longueur du tableau doit tre une constante
int tabI[20]; float tabF[10]; #define N 10 int tabI[N];

Initialisation dun tableau


Lors de sa cration un tableau contient nimporte quoi dans ses cases : en effet, la dclaration rserve seulement la mmoire, mais ny touche pas ! il faut initialiser les cases, une une. Dclaration + initialisation Initialisation systmatique : boucle for Initialisation case par case
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 8

soit une constante en dur dans le programme (10, 100, ) soit (de faon quivalente !) par le pr-processeur #define N 20

cette dernire mthode est prconiser : il est alors facile de rviser la taille
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 7

Dclaration + initialisation
Comme pour les variables de types simples, il est possible de combiner la dclaration et linitialisation : int tab[4] = { 2, 3, -1, 5 }; cre le tableau tab de 4 entiers avec tab[0] = 2; tab[1] = 3; tab[2] = -1; tab[3] = 5;
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 9

Initialisation partielle
On peut ninitialiser que certaines cases : int tab[10] = { 2, , -1, 5 }; cre le tableau tab de 10 entiers avec tab[0] = 2 tab[2] = -1 tab[3] = 5 les autres cases ne sont pas initialises
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 10

Tableaux de caractres
Un tableau de caractres est un tableau particulier : chane de caractres char mot[10] = toto; cre le tableau mot de 10 caractres avec mot[0] = t mot[1] = o mot[2] = t mot[3] = o mot[4] = \0 (fin de chane) les autres cases ne sont pas initialises
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 11

Initialisation systmatique : Boucle for


Il est possible dinitialiser chaque case en fonction de sa position i tabI[i] = f(i); o f est une fonction qui dpend de i La commande for permet de boucler en incrmentant le compteur i chaque tour
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 12

Boucle for
La boucle for rpte une instruction plusieurs fois, avec un compteur qui sincrmente chaque tour :
for (<init >; <test>; <incr (<init>; <incrmentation>) <instruction> <init> init> : initialisation du compteur <test> : test de continuation <incr <incrmentation> :

for = while
La boucle for nest en fait quune reformulation du while :
for (<init>; <test>; <incrmentation>) <instruction> <init> while (<test>) { <instruction> <incrmentation> }
Programmation C - ENSTA - 14

incrmentation du compteur
Programmation C - ENSTA - 13

David Pointcheval LIENS - CNRS

Boucle for
Une instruction est excute, plusieurs fois avec un compteur: linstruction peut ne jamais tre excute
David Pointcheval LIENS - CNRS

wh il e

fo r

David Pointcheval LIENS - CNRS

for classique
Lusage classique de la boucle for est le suivant :
int n=15; int i; for (i=0; i<n; i++) <instruction(i)>
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 16

<initialisation>

<test>
oui

non

<instruction> <incrmentation>

Programmation C - ENSTA - 15

Initialisation dun tableau


La boucle for permet dinitialiser aisment un tableau : #define N 10 int tab[N]; int i; for (i=0; i<N; i++) tab[i] = 0; Les cases sont toutes initialises Zro
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 17

Initialisation dun tableau


La boucle for permet dinitialiser un tableau avec des valeurs qui dpendent de lindice de la case : #define N 10 int carre[N]; int i; for (i=0; i<N; i++) carre[i] = i*i;
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 18

Affichage dun tableau


La commande printf ne permet pas dafficher un tableau tel quel, il faut afficher les cases une une :
/* carre.c - Carrs */ #include <stdio.h> #include <stdlib.h> #define N 10 int main(int argc, char *argv[]) { int carre[N]; int i; for (i=0; i<N; i++) carre[i] = i*i; for (i=0; i<N; i++) printf(%d ,carre[i]); printf(\n); return 0; }
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 19 David Pointcheval LIENS - CNRS

Arguments utilisateur
Initialisations :
a = 3; b = 5;
int main() { int a,b,c; a = 3; b = 5; c = addition(a,b); printf(%d+%d=%d\n,a,b,c); return 0; }

valeurs imposes la compilation, pas modifiables au cours de lexcution excutions toutes identiques pas grand intrt !

Programmation C - ENSTA - 20

argc et argv
#include <stdio.h> int main(int argc, char *argv[]) { printf(%s %s\n,argv[0],argv[1]); return 0; }
#include <stdio.h>

Exemple
int main(int argc, char *argv[]) { printf(%s %s\n,argv[1],argv[2]); return 0; }

En effet, le tableau
char *argv[];

contient

Fonction main :

Saisir le programme Compiler le programme Excuter


David Pointcheval LIENS - CNRS

Premier argument int argc

emacs affiche.c &

Deuxime argument char *argv[]


tableau qui contient les mots passs sur la ligne de commande
David Pointcheval LIENS - CNRS

nombre de cases du tableau = nombre de mots sur la ligne de commande

affiche dans argv[0] toto dans argv[1] tata dans argv[2]

gcc -Wall affiche.c -o affiche >affiche toto tata toto tata

et lentier
int argc;

est gal 3
Programmation C - ENSTA - 22

Programmation C - ENSTA - 21

Conversion atoi
Les argv[i] sont des chanes de caractres convertir en entiers (quand elles codent des entiers)
#include <stdio.h> int main(int argc, char *argv[]) { int x; x = atoi(argv[1]); printf(%d -> %d \n,x,x+1); return 0; }
David Pointcheval LIENS - CNRS

Suites
Exemple prcdent : ui = f (i) Autres possibilits : ui = f (i, ui-k, ui-k+1, , ui-1) Ex : la factorielle
/* fact.c - Factorielle */ #include <stdio.h> #include <stdlib.h> #define N 30 int fact[N]; int main(int argc, char *argv[]) { int i; int n = atoi(argv[1]); fact[0] = 1; for (i=1; i<=n; i++) fact[i] = fact[i-1]*i; printf(Fact(%d) = %d \n,n,fact[n]); return 0; }

int x; x = atoi(argv[1]);

Programmation C - ENSTA - 23

David Pointcheval LIENS - CNRS

Programmation C - ENSTA - 24

Plusieurs dimensions
Un tableau plusieurs dimensions est en fait un tableau de tableaux de tableaux de Un tableau n dimensions est un tableau de tableaux n-1 dimensions
David Pointcheval LIENS - CNRS

La mmoire
char tab[4][3]; dfinit un tableau de 4 tableaux de 3 caractres (char) conscutifs tab[1] tab[0]

tab[2] tab[3]

tab[2][1]
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 26

Programmation C - ENSTA - 25

Boucles for imbriques


#define M 20 #define N 10 int tab[M][N]; int i,j; for (i=0; i<M; i++) for (j=0; j<N; j++) tab[i][j] = 0;
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 27 David Pointcheval LIENS - CNRS

Pointeurs
Les pointeurs permettront de dfinir des tableaux de taille non constante (dfinie au cours de lexcution du programme) cf. les cours sur les pointeurs et lallocation dynamique
Programmation C - ENSTA - 28

You might also like