• Aucun résultat trouvé

8 Les tableaux

N/A
N/A
Protected

Academic year: 2022

Partager "8 Les tableaux"

Copied!
323
0
0

Texte intégral

(1)

8 Les tableaux

Peter Schlagheck Universit ´e de Li `ege

Ces notes ont pour seule vocation d’ ˆetre utilis ´ees par les ´etudiants dans le cadre de leur cursus au sein de l’Universit ´e de Li `ege. Aucun autre usage ni diffusion n’est autoris ´e, sous peine de constituer une violation de la Loi du 30 juin 1994

relative au droit d’auteur.

(2)

8 Les tableaux

8.1 Les tableaux conventionnels 8.2 Les strings

8.3 Les pointeurs

8.4 Les tableaux dynamiques

(3)

8.1 Les tableaux conventionnels

Souvent on veut appliquer une s ´equence d’op ´erations non seulement pour une seule variable, mais pour un ensemble de plusieurs variables ´equivalentes.

Exemple:

traitement des donn ´ees de m ´esure contenues dans le fichier donnees.dat

−→ lire le fichier donnees.dat

−→ d ´eterminer la donn ´ee minimale

−→ ´ecrire les donn ´ees modifi ´ees

−→ (soustraction avec la valeur minimale)

−→ dans un autre fichier

donnees.dat:

53.98 48.58 55.28 62.85

(4)

8.1 Les tableaux conventionnels

Souvent on veut appliquer une s ´equence d’op ´erations non seulement pour une seule variable, mais pour un ensemble de plusieurs variables ´equivalentes.

#include <fstream>

using namespace std;

fo main() {

foifstream inp( ''donnees.dat'' );

fodouble a1, a2, a3, a4;

foinp >> a1 >> a2 >> a3 >> a4;

fodouble amin = a1;

foif ( a2 < amin ) fofoamin = a2;

foif ( a3 < amin ) fofoamin = a3;

foif ( a4 < amin ) fofoamin = a4;

foofstream out( ''donnees2.dat'' );

foout << a1 - amin << endl << a2 - amin << endl foout<< a3 - amin << endl << a4 - amin << endl;

}

−→ difficile de g ´en ´eraliser ce programme

−→ pour des donn ´ees plus nombreuses . . .

donnees.dat:

53.98 48.58 55.28 62.85

donnees2.dat:

5.4 0 6.7 14.27

(5)

8.1 Les tableaux conventionnels

Souvent on veut appliquer une s ´equence d’op ´erations non seulement pour une seule variable, mais pour un ensemble de plusieurs variables ´equivalentes.

#include <fstream>

using namespace std;

fo main() {

foifstream inp( ''donnees.dat'' );

fodouble a1, a2, a3, a4;

foinp >> a1 >> a2 >> a3 >> a4;

fodouble amin = a1;

foif ( a2 < amin ) fofoamin = a2;

foif ( a3 < amin ) fofoamin = a3;

foif ( a4 < amin ) fofoamin = a4;

foofstream out( ''donnees2.dat'' );

foout << a1 - amin << endl << a2 - amin << endl foout<< a3 - amin << endl << a4 - amin << endl;

}

−→ difficile de g ´en ´eraliser ce programme

−→ pour des donn ´ees plus nombreuses . . .

donnees.dat:

53.98 48.58 55.28 62.85 63.00 56.17 52.77 54.65 60.65 54.60

(6)

8.1 Les tableaux conventionnels

Souvent on veut appliquer une s ´equence d’op ´erations non seulement pour une seule variable, mais pour un ensemble de plusieurs variables ´equivalentes.

−→ utilisation des tableaux :

#include <fstream>

using namespace std;

fo main() {

foifstream inp( ''donnees.dat'' );

fodouble a[ 10 ];

fofor ( int i = 0; i < 10; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < 10; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < 10; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85 63.00 56.17 52.77 54.65 60.65 54.60

(7)

8.1 Les tableaux conventionnels

Souvent on veut appliquer une s ´equence d’op ´erations non seulement pour une seule variable, mais pour un ensemble de plusieurs variables ´equivalentes.

−→ utilisation des tableaux :

#include <fstream>

using namespace std;

fo main() {

foifstream inp( ''donnees.dat'' );

fodouble a[ 10 ];

fofor ( int i = 0; i < 10; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < 10; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < 10; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees2.dat:

5.4 0 6.7 14.27 14.42 7.59 4.19 6.07 12.07 6.02

(8)

8.1 Les tableaux conventionnels

Souvent on veut appliquer une s ´equence d’op ´erations non seulement pour une seule variable, mais pour un ensemble de plusieurs variables ´equivalentes.

−→ utilisation des tableaux :

#include <fstream>

using namespace std;

fo main() {

foconst int N = 10;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees2.dat:

5.4 0 6.7 14.27 14.42 7.59 4.19 6.07 12.07 6.02

(9)

8.1 Les tableaux conventionnels

Souvent on veut appliquer une s ´equence d’op ´erations non seulement pour une seule variable, mais pour un ensemble de plusieurs variables ´equivalentes.

−→ utilisation des tableaux :

#include <fstream>

using namespace std;

fo main() {

foconst int N = 20;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees2.dat:

5.4 0 6.7 14.27 14.42 7.59 4.19 6.07 12.07 6.02

(10)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(11)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(12)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(13)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(14)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(15)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(16)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(17)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(18)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(19)

8.1 Les tableaux conventionnels

Le flot du programme dans la m ´emoire:

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fodouble amin = a[ 0 ];

fofor ( int i = 1; i < N; i++ ) fofoif ( a[ i ] < amin ) fofofoamin = a[ i ];

foofstream out( ''donnees2.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoout << a[ i ] - amin << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

(20)

8.1 Les tableaux conventionnels

htypei hnomi

[

htaillei

];

−→ d ´efinition d’un tableau nomm ´e

hnomi

−→ contenant

htaillei

variables du type

htypei

−→

htaillei

doit ˆetre une expression enti `ere constante Acc `es aux ´el ´ements du tableau:

hnomi

[

hexpression enti `erei

]

−→ c¸a rend l’ ´el ´ement num ´ero

hexpression enti `erei

+1

−→ du tableau

hnomi

(21)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

focout << a[ 0 ] << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

Ex ´ecution:

53.98

(22)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

focout << a[ 3 ] << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

Ex ´ecution:

62.85

(23)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fofor ( int i = 0; i < N; i++ ) fofocout << a[ i + 1 ] << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

Ex ´ecution:

48.58 55.28 62.85

1.00768e-313

(24)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

fofor ( int i = 0; i < N; i++ ) fofocout << a[ i - 1 ] << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

Ex ´ecution:

-4.90985e-39 53.98

48.58

55.28

(25)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

focout << a[ 4 ] << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

Ex ´ecution:

1.00768e-313

(26)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

focout << a[ -1 ] << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

Ex ´ecution:

-4.90985e-39

(27)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 4;

foifstream inp( ''donnees.dat'' );

fodouble a[ N ];

fofor ( int i = 0; i < N; i++ ) fofoinp >> a[ i ];

focout << a[ -2 ] << endl;

}

donnees.dat:

53.98 48.58 55.28 62.85

Ex ´ecution:

-3.0138e-39

−→ valeur al ´eatoire (sans message d’erreur)

−→ si l’indice d ´epasse la taille du tableau

−→ vous risquez de manipuler d’autres variables

−→ si vous ne faites pas attention avec les indices !

(28)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

Ex ´ecution:

1 7.7

(29)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(30)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(31)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(32)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(33)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(34)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(35)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(36)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(37)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(38)

8.1 Les tableaux conventionnels

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 3;

fodouble x = 2.3;

fodouble y = 7.7;

fodouble a[ N ];

fofor ( int i = 0; i <= N; i++ ) fofoa[ i ] = 1.0;

focout << x << '' '' << y << endl;

}

(39)

Initialisation des tableaux

htypei hnomi

[

htaillei

] = {

hvaleur 0i

,

hvaleur 1i

, ... };

−→

hnomi

[0] est initialis ´e avec la valeur

hvaleur 0i

−→

hnomi

[1] est initialis ´e avec la valeur

hvaleur 1i

−→ . . .

(40)

Initialisation des tableaux

htypei hnomi

[

htaillei

] = {

hvaleur 0i

,

hvaleur 1i

, ... };

−→

hvaleur 0i

,

hvaleur 1i

. . . doivent ˆetre des valeurs du type

−→ (ou convertibles en)

htypei

−→ le nombre des valeurs sp ´ecifi ´ees dans l’initialisation

−→ ne doit pas d ´epasser la taille du tableau

−→ (mais il peut bien ˆetre inf ´erieur `a elle)

const int n = 3;

double a[ n ] = { 1.2, 3, -0.1 };

for ( int i = 0; i < n; i++ ) focout << a[ i ] << endl;

Ex ´ecution:

1.2

3

-0.1

(41)

Initialisation des tableaux

htypei hnomi

[

htaillei

] = {

hvaleur 0i

,

hvaleur 1i

, ... };

−→

hvaleur 0i

,

hvaleur 1i

. . . doivent ˆetre des valeurs du type

−→ (ou convertibles en)

htypei

−→ le nombre des valeurs sp ´ecifi ´ees dans l’initialisation

−→ ne doit pas d ´epasser la taille du tableau

−→ (mais il peut bien ˆetre inf ´erieur `a elle)

const int n = 3;

double a[ n ] = { 1.2, 3 };

for ( int i = 0; i < n; i++ ) focout << a[ i ] << endl;

Ex ´ecution:

1.2

3

0

(42)

Initialisation des tableaux

htypei hnomi

[

htaillei

] = {

hvaleur 0i

,

hvaleur 1i

, ... };

−→

hvaleur 0i

,

hvaleur 1i

. . . doivent ˆetre des valeurs du type

−→ (ou convertibles en)

htypei

−→ le nombre des valeurs sp ´ecifi ´ees dans l’initialisation

−→ ne doit pas d ´epasser la taille du tableau

−→ (mais il peut bien ˆetre inf ´erieur `a elle)

const int n = 3;

double a[ n ] = { 1.2, 3, -0.1, 4.1 };

for ( int i = 0; i < n; i++ ) focout << a[ i ] << endl;

−→ erreur:

too many initializers for 'double [3]'

(43)

Initialisation des tableaux

htypei hnomi

[

htaillei

] = {

hvaleur 0i

,

hvaleur 1i

, ... };

−→

hvaleur 0i

,

hvaleur 1i

. . . doivent ˆetre des valeurs du type

−→ (ou convertibles en)

htypei

−→ le nombre des valeurs sp ´ecifi ´ees dans l’initialisation

−→ ne doit pas d ´epasser la taille du tableau

−→ (mais il peut bien ˆetre inf ´erieur `a elle)

const int n = 3;

double a[ n ] = { 1.2, 3, -0.1 };

double b[ n ] = a;

for ( int i = 0; i < n; i++ ) focout << b[ i ] << endl;

−→ erreur:

array must be initialized with a brace-enclosed initializer

(44)

Initialisation des tableaux

htypei hnomi

[

htaillei

] = {

hvaleur 0i

,

hvaleur 1i

, ... };

−→

hvaleur 0i

,

hvaleur 1i

. . . doivent ˆetre des valeurs du type

−→ (ou convertibles en)

htypei

−→ le nombre des valeurs sp ´ecifi ´ees dans l’initialisation

−→ ne doit pas d ´epasser la taille du tableau

−→ (mais il peut bien ˆetre inf ´erieur `a elle)

const int n = 3;

double a[ n ] = { 1.2, 3, -0.1 };

double b[ n ];

for ( int i = 0; i < n; i++ ) fob[ i ] = a[ i ];

for ( int i = 0; i < n; i++ ) focout << b[ i ] << endl;

Ex ´ecution:

1.2

3

-0.1

(45)

Initialisation des tableaux

htypei hnomi

[] = {

hvaleur 1i

, ... ,

hvaleur Ni

};

−→ d ´efinition et initialisation d’un tableau avec N ´el ´ements:

−→

hnomi

[ 0 ] est initialis ´e avec la valeur

hvaleur 1i

−→ . . .

−→

hnomi

[ N - 1 ] est initialis ´e avec la valeur

hvaleur Ni

(46)

Initialisation des tableaux

htypei hnomi

[] = {

hvaleur 1i

, ... ,

hvaleur Ni

};

−→ d ´efinition et initialisation d’un tableau avec N ´el ´ements:

−→

hnomi

[ 0 ] est initialis ´e avec la valeur

hvaleur 1i

−→ . . .

−→

hnomi

[ N - 1 ] est initialis ´e avec la valeur

hvaleur Ni const int n = 3;

double a[ n ] = { 1.2, 3, -0.1, 4.1 }; for ( int i = 0; i < n; i++ )

focout << a[ i ] << endl;

−→ erreur:

too many initializers for 'double [3]'

(47)

Initialisation des tableaux

htypei hnomi

[] = {

hvaleur 1i

, ... ,

hvaleur Ni

};

−→ d ´efinition et initialisation d’un tableau avec N ´el ´ements:

−→

hnomi

[ 0 ] est initialis ´e avec la valeur

hvaleur 1i

−→ . . .

−→

hnomi

[ N - 1 ] est initialis ´e avec la valeur

hvaleur Ni const int n = 3;

double a[] = { 1.2, 3, -0.1, 4.1 }; for ( int i = 0; i < n; i++ ) focout << a[ i ] << endl;

Ex ´ecution:

1.2

3

-0.1

(48)

Initialisation des tableaux

htypei hnomi

[] = {

hvaleur 1i

, ... ,

hvaleur Ni

};

−→ d ´efinition et initialisation d’un tableau avec N ´el ´ements:

−→

hnomi

[ 0 ] est initialis ´e avec la valeur

hvaleur 1i

−→ . . .

−→

hnomi

[ N - 1 ] est initialis ´e avec la valeur

hvaleur Ni const int n = 3;

double a[] = { 1.2, 3, -0.1, 4.1 }; for ( int i = 0; i < 4; i++ ) focout << a[ i ] << endl;

Ex ´ecution:

1.2

3

-0.1

4.1

(49)

Les tableaux comme arguments des fonctions

D ´eclaration d’une fonction

htype fi

qui prend un tableau des variables du type

htype ai

comme argument:

htype fi hnom fi

( ... ,

htype ai hnom ai

[], ... );

Appel de cette fonction, apr `es la d ´efinition du tableau

htype ai hnomi

[

htaillei

]; :

hnom fi

( ... ,

hnomi

, ... )

−→ on transmet l’adresse du premier ´el ´ement du tableau

−→ `a la fonction

−→ la fonction travaille donc forc ´ement avec des variables

−→ originales du tableau

−→ . . . mais elle ne connaˆıt pas la taille du tableau

(50)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(51)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(52)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(53)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(54)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(55)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(56)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(57)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(58)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

(59)

Les tableaux comme arguments des fonctions

Une routine de copie de deux tableaux:

void copie( int N, double a[], double b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

} fo main() {

fodouble a[] = { 1.2, -0.1, 4.1 };

fodouble b[] = { 0, 0, 0 }; focopie( 3, a, b );

}

−→ il n’est pas possible de d ´efinir des fonctions

−→ qui rendent des tableaux comme “valeur de retour”

(60)

Les tableaux multidimensionnels

htypei hnomi

[

htaille 1i

][

htaille 2i

];

−→ d ´efinition d’un tableau `a deux dimensions

−→ = “un tableau des tableaux”

D ´efinition avec initialisation:

htypei hnomi

[

htaille 1i

][

htaille 2i

] =

fo{ {

helem 11i

,

helem 12i

, ...

helem 1Mi

}, fo{ {

helem 21i

,

helem 22i

, ...

helem 2Mi

}, fo{ { ...

fo{ {

helem N1i

,

helem N2i

, ...

helem NMi

} };

−→ N doit correspondre `a la valeur de

htaille 1i

−→ M doit correspondre `a la valeur de

htaille 2i

(61)

Les tableaux multidimensionnels

htypei hnomi

[

htaille 1i

][

htaille 2i

];

−→ d ´efinition d’un tableau `a deux dimensions

−→ = “un tableau des tableaux”

D ´efinition avec initialisation:

htypei hnomi

[][] =

fo{ {

helem 11i

,

helem 12i

, ...

helem 1Mi

}, fo{ {

helem 21i

,

helem 22i

, ...

helem 2Mi

}, fo{ { ...

fo{ {

helem N1i

,

helem N2i

, ...

helem NMi

} };

−→ erreur:

declaration of 'hnomi' as multidimensional array must have bounds for all dimensions except the first

(62)

Les tableaux multidimensionnels

htypei hnomi

[

htaille 1i

][

htaille 2i

];

−→ d ´efinition d’un tableau `a deux dimensions

−→ = “un tableau des tableaux”

D ´efinition avec initialisation:

htypei hnomi

[][

htaille 2i

] =

fo{ {

helem 11i

,

helem 12i

, ...

helem 1Mi

}, fo{ {

helem 21i

,

helem 22i

, ...

helem 2Mi

}, fo{ { ...

fo{ {

helem N1i

,

helem N2i

, ...

helem NMi

} };

−→ N d ´efinit la premi `ere dimension du tableau

(63)

Les tableaux multidimensionnels

htypei hnomi

[

htaille 1i

][

htaille 2i

];

−→ d ´efinition d’un tableau `a deux dimensions

−→ = “un tableau des tableaux”

D ´efinition avec initialisation:

htypei hnomi

[][

htaille 2i

] =

fo{ {

helem 11i

,

helem 12i

, ...

helem 1Mi

}, fo{ {

helem 21i

,

helem 22i

, ...

helem 2Mi

}, fo{ { ...

fo{ {

helem N1i

,

helem N2i

, ...

helem NMi

} };

Acc `es aux ´el ´ements:

hnomi

[

hint expr 1i

][

hint expr 2i

]

−→ c¸a rend l’ ´el ´ement no.

hint expr 1i

×

htaille 2i

+

hint expr 2i

−→ dans la s ´equence des valeurs en m ´emoire

(64)

Les tableaux multidimensionnels

htypei hnomi

[

htaille 1i

][

htaille 2i

][

htaille 3i

];

−→ d ´efinition d’un tableau `a trois dimensions

−→ = “un tableau des tableaux des tableaux”

D ´efinition avec initialisation:

htypei hnomi

[][

htaille 2i

][

htaille 3i

] = fo{ { { ... }, { ... }, ..., { ... } }, fofo{ { ... }, { ... }, ..., { ... } }, fofofofo...

fofo{ { ... }, { ... }, ..., { ... } } };

Acc `es aux ´el ´ements:

hnomi

[

hint expr 1i

][

hint expr 2i

][

hint expr 3i

]

(65)

Les tableaux multidimensionnels

main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; focout << a[ 1 ][ 1 ] << endl;

}

(66)

Les tableaux multidimensionnels

main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; focout << a[ 1 ][ 1 ] << endl;

}

Ex ´ecution:

4

−→ acc `es `a l’ ´el ´ement no. 3 × 1 + 1 = 4 du tableau a

(67)

Les tableaux multidimensionnels

main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; focout << a[ 0 ][ 3 ] << endl;

}

Ex ´ecution:

-1

−→ acc `es `a l’ ´el ´ement no. 3 × 0 + 3 = 3 du tableau a

(68)

Les tableaux multidimensionnels

main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; fodouble b[ 3 ][ 2 ] = { { 2, 3 }, { 1, -1 }, { 4, 0 } };

focout << a[ 1 ][ 1 ] << endl;

focout << b[ 1 ][ 1 ] << endl;

}

(69)

Les tableaux multidimensionnels

main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; fodouble b[ 3 ][ 2 ] = { { 2, 3 }, { 1, -1 }, { 4, 0 } };

focout << a[ 1 ][ 1 ] << endl;

focout << b[ 1 ][ 1 ] << endl;

}

Ex ´ecution:

4

-1

(70)

Les tableaux multidimensionnels

main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; fodouble b[ 3 ][ 2 ] = { { 2, 3 }, { 1, -1 }, { 4, 0 } };

focout << a[ 0 ][ 3 ] << endl;

focout << b[ 0 ][ 3 ] << endl;

}

Ex ´ecution:

-1

-1

(71)

Les tableaux multidimensionnels

. . . comme arguments des fonctions:

main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } };

fodouble b[ 2 ][ 3 ];

focopie( a, b );

focout << b[ 1 ][ 1 ] << endl;

}

(72)

Les tableaux multidimensionnels

void copie( double a[][], double b[][] ) {

fofor ( int i = 0; i < 2; i++ ) fofofor ( int j = 0; j < 3; j++ ) fofofob[ i ][ j ] = a[ i ][ j ];

} fo main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; fodouble b[ 2 ][ 3 ];

focopie( a, b );

focout << b[ 1 ][ 1 ] << endl;

}

−→ erreur:

declaration of 'a' as multidimensional array must have bounds for all dimensions except the first

declaration of 'b' as multidimensional array must have bounds for all dimensions except the first

(73)

Les tableaux multidimensionnels

void copie( double a[][ 3 ], double b[][ 3 ] ) {

fofor ( int i = 0; i < 2; i++ ) fofofor ( int j = 0; j < 3; j++ ) fofofob[ i ][ j ] = a[ i ][ j ];

} fo main() {

fodouble a[ 2 ][ 3 ] = { { 2, 3, 1 }, { -1, 4, 0 } }; fodouble b[ 2 ][ 3 ];

focopie( a, b );

focout << b[ 1 ][ 1 ] << endl;

}

Ex ´ecution:

4

(74)

Les tableaux multidimensionnels

const int M = 3;

fo

void copie( double a[][ M ], double b[][ M ] ) {

fofor ( int i = 0; i < 2; i++ ) fofofor ( int j = 0; j < M; j++ ) fofofob[ i ][ j ] = a[ i ][ j ];

} fo main() {

fodouble a[ 2 ][ M ] = { { 2, 3, 1 }, { -1, 4, 0 } }; fodouble b[ 2 ][ M ];

focopie( a, b );

focout << b[ 1 ][ 1 ] << endl;

}

−→ d ´efinition de la taille M hors des blocs de fonctions

(75)

Les tableaux multidimensionnels

const int M = 3;

fo

void copie( int N, double a[][ M ], double b[][ M ] ) {

fofor ( int i = 0; i < N; i++ ) fofofor ( int j = 0; j < M; j++ ) fofofob[ i ][ j ] = a[ i ][ j ];

} fo main() {

foconst int N = 2;

fodouble a[ N ][ M ] = { { 2, 3, 1 }, { -1, 4, 0 } };

fodouble b[ N ][ M ];

focopie( N, a, b );

focout << b[ 1 ][ 1 ] << endl;

}

(76)

Les tableaux multidimensionnels

const int M = 4;

fo

void copie( int N, double a[][ M ], double b[][ M ] ) {

fofor ( int i = 0; i < N; i++ ) fofofor ( int j = 0; j < M; j++ ) fofofob[ i ][ j ] = a[ i ][ j ];

} fo main() {

foconst int N = 3;

fodouble a[ N ][ M ] = { { 2, 3, 1 }, { -1, 4, 0 } };

fodouble b[ N ][ M ];

focopie( N, a, b );

focout << b[ 1 ][ 1 ] << endl;

}

Ex ´ecution:

4

(77)

Les tableaux multidimensionnels

const int M = 4;

fo

void copie( int N, double a[][ M ], double b[][ M ] ) {

fofor ( int i = 0; i < N; i++ ) fofofor ( int j = 0; j < M; j++ ) fofofob[ i ][ j ] = a[ i ][ j ];

} fo main() {

foconst int N = 3;

fodouble a[ N ][ M ] = { { 2, 3, 1 }, { -1, 4, 0 } };

fodouble b[ N ][ M ];

focopie( N, a, b );

focout << b[ 0 ][ 3 ] << endl;

}

Ex ´ecution:

0

(78)

Les tableaux multidimensionnels

const int M = 4;

fo

void copie( int N, double a[][ M ], double b[][ M ] ) {

fofor ( int i = 0; i < N; i++ ) fofofor ( int j = 0; j < M; j++ ) fofofob[ i ][ j ] = a[ i ][ j ];

} fo main() {

foconst int N = 3;

fodouble a[ N ][ M ] = { { 2, 3, 1 }, { -1, 4, 0 } };

fodouble b[ N ][ M ];

focopie( N, a, b );

focout << b[ 0 ][ 3 ] << endl;

}

(79)

Les tableaux multidimensionnels

Utilisation pour des routines de manipulation des matrices ? Le contenu de matrix.cpp :

const int M = 10;

fo

double trace( double a[][ M ] ) {

fodouble tr = 0;

fofor ( int i = 0; i < M; i++ ) fofotr += a[ i ][ i ];

foreturn tr;

} fo

double determinant( double a[][ M ] ) {

fo...

}

(80)

Les tableaux multidimensionnels

Utilisation pour des routines de manipulation des matrices ? Le contenu de matrix.cpp :

#include <matrix.h>

fo

double trace( double a[][ M ] ) {

fodouble tr = 0;

fofor ( int i = 0; i < M; i++ ) fofotr += a[ i ][ i ];

foreturn tr;

} fo

double determinant( double a[][ M ] ) {

fo...

}

(81)

Les tableaux multidimensionnels

Utilisation pour des routines de manipulation des matrices ? Le contenu de matrix.h :

const int M = 10;

fo

double trace( double a[][ M ] );

fo

double determinant( double a[][ M ] );

fo

void inversion( double a[][ M ] );

fo

void valeurs propres( double a[][ M ], double vpr[] );

fo ...

−→ on est oblig ´e de modifier matrix.h et de recompiler

−→ matrix.cpp si on veut changer la taille M de la matrice

−→ utiliser des tableaux unidimensionnels de la taille M × M

(82)

Les tableaux multidimensionnels

Utilisation pour des routines de manipulation des matrices ? Le contenu de matrix.h :

double trace( int M, double a[] );

fo

double determinant( int M, double a[] );

fo

void inversion( int M, double a[] );

fo

void valeurs propres( int M, double a[], double vpr[] );

fo ...

(83)

Les tableaux multidimensionnels

Utilisation pour des routines de manipulation des matrices ? Le contenu de matrix.cpp :

#include <matrix.h>

fo

double trace( int M, double a[] ) {

fodouble tr = 0;

fofor ( int i = 0; i < M; i++ ) fofotr += a[ i * M + i ]

foreturn tr;

} fo

double determinant( int M, double a[] ) {

fo...

}

(84)

Les tableaux multidimensionnels

Utilisation pour des routines de manipulation des matrices ? Le contenu de matrix.cpp :

#include <matrix.h>

fo

double trace( int M, double a[] ) {

fodouble tr = 0;

fofor ( int i = 0; i < M; i++ ) fofotr += a[ i * M + i ]

foreturn tr;

}

−→ l’ ´el ´ement a

ij

de la matrice a est accessible par

−→ a[ i * M + j ]

−→ alternative: utilisation des tableaux dynamiques

(85)

8.2 Les strings (chaˆınes de caract `eres)

Un string est un tableau de caract `eres . . .

char nom[] = { 'P', 'e', 't', 'e', 'r' };

for ( int i = 0; i < 5; i++ ) focout << nom[ i ];

cout << endl;

Ex ´ecution:

Peter

(86)

8.2 Les strings (chaˆınes de caract `eres)

Un string est un tableau de caract `eres . . . . . . qui termine avec le caract `ere ’nul’

char nom[] = { 'P', 'e', 't', 'e', 'r', 0 };

for ( int i = 0; i < 5; i++ ) focout << nom[ i ];

cout << endl;

(87)

8.2 Les strings (chaˆınes de caract `eres)

Un string est un tableau de caract `eres . . . . . . qui termine avec le caract `ere ’nul’

char nom[] = ''Peter'';

for ( int i = 0; i < 5; i++ ) focout << nom[ i ];

cout << endl;

(88)

8.2 Les strings (chaˆınes de caract `eres)

Un string est un tableau de caract `eres . . . . . . qui termine avec le caract `ere ’nul’

char nom[] = ''Peter'';

cout << nom << endl;

Ex ´ecution:

Peter

−→ initialisation “directe” avec des guillemets

−→ input et output “directs” avec le nom du string

(89)

8.2 Les strings (chaˆınes de caract `eres)

Un string est un tableau de caract `eres . . . . . . qui termine avec le caract `ere ’nul’

char nom[] = ''Peter'';

for ( int i = 0; i < 6; i++ ) focout << int( nom[ i ] ) << '' '';

cout << endl;

Ex ´ecution:

80 101 116 101 114 0

(90)

8.2 Les strings (chaˆınes de caract `eres)

char nom[ 10 ];

cout << ''entrez votre nom: '';

cin >> nom;

cout << ''voila votre nom: '' << nom << endl;

Ex ´ecution:

entrez votre nom: Peter

voila votre nom: Peter

(91)

8.2 Les strings (chaˆınes de caract `eres)

char nom[ 10 ];

cout << ''entrez votre nom: '';

cin >> nom;

cout << ''voila votre nom: '' << nom << endl;

for ( int i = 0; i < 10; i++ ) focout << int( nom[ i ] ) << '' '';

cout << endl;

Ex ´ecution:

entrez votre nom: Peter voila votre nom: Peter

80 101 116 101 114 0 8 -88 91 -37

−→ cin met les caract `eres lus depuis le clavier dans nom

−→ et ajoute le caract `ere ’nul’

−→ cout affiche tous les caract `eres de nom

−→ jusqu’au caract `ere ’nul’

(92)

Manipulations des strings

void copie( int N, char a[], char b[] ) {

fofor ( int i = 0; i < N; i++ ) fofob[ i ] = a[ i ];

}

(93)

Manipulations des strings

void copie( char a[], char b[] ) {

foint i;

fofor ( i = 0; a[ i ]; i++ ) fofob[ i ] = a[ i ];

fob[ i ] = 0;

}

−→ pas besoin de sp ´ecifier la taille du string

−→ de telles fonctions de manipulation des strings

−→ sont disponibles en incluant le header <cstring>

−→ (<string.h> en C)

(94)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar prenom[] = ''Peter'';

fochar nom[ 100 ];

fostrcpy( nom, prenom );

focout << nom << endl;

}

Ex ´ecution:

Peter

strcpy( a, b ) copie le contenu du string b dans le string a

(“a = b”)

(95)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar prenom[] = ''Peter'';

fochar nom[ 100 ];

fostrcpy( nom, prenom );

fochar nom famille[] = ''Schlagheck'';

fostrcat( nom, nom famille );

focout << nom << endl;

}

Ex ´ecution:

PeterSchlagheck

strcat( a, b ) ajoute le contenu du string b au string a

(“a = a + b”)

(96)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar prenom[] = ''Peter'';

fochar nom[ 100 ];

fostrcpy( nom, prenom );

fochar nom famille[] = ''Schlagheck'';

fostrcat( nom, '' '' );

fostrcat( nom, nom famille );

focout << nom << endl;

}

Ex ´ecution:

Peter Schlagheck

(97)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar prenom[] = ''Peter'';

fochar nom[ 100 ];

fostrcpy( nom, prenom );

fochar nom famille[] = ''Schlagheck'';

fostrcat( nom, '' '' );

fostrcat( nom, nom famille );

focout << nom << '' -- '' << strlen( nom ) << endl;

}

Ex ´ecution:

Peter Schlagheck -- 16

strlen( a ) rend la longueur du string a

(98)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar motpasse[] = ''asdj25gf'';

fochar mot[ 100 ];

focout << ''saisissez le mot de passe: '' << endl;

focin >> mot;

foif ( strcmp( mot, motpasse ) ) fofocout << ''pas correct'' << endl;

foelse

fofocout << ''correct'' << endl;

}

Ex ´ecution:

saisissez le mot de passe: asdj25gf

correct

(99)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar motpasse[] = ''asdj25gf'';

fochar mot[ 100 ];

focout << ''saisissez le mot de passe: '' << endl;

focin >> mot;

foif ( strcmp( mot, motpasse ) ) fofocout << ''pas correct'' << endl;

foelse

fofocout << ''correct'' << endl;

}

Ex ´ecution:

saisissez le mot de passe: asdj25g

pas correct

(100)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar motpasse[] = ''asdj25gf'';

fochar mot[ 100 ];

focout << ''saisissez le mot de passe: '' << endl;

focin >> mot;

foif ( strcmp( mot, motpasse ) ) fofocout << ''pas correct'' << endl;

foelse

fofocout << ''correct'' << endl;

}

Ex ´ecution:

saisissez le mot de passe: asdj25gfc

pas correct

(101)

Manipulations des strings

#include <iostream>

#include <cstring>

using namespace std;

fo main() {

fochar motpasse[] = ''asdj25gf'';

fochar mot[ 100 ];

focout << ''saisissez le mot de passe: '' << endl;

focin >> mot;

foif ( strcmp( mot, motpasse ) ) fofocout << ''pas correct'' << endl;

foelse

fofocout << ''correct'' << endl;

}

strcmp( a, b ) rend 0 si les strings a et b sont ´egaux,

1 si a > b (alphab ´etiquement) et −1 si a < b

(102)

Des tableaux des strings

char nom[ 12 ][ 50 ];

−→ d ´eclaration d’un tableau de 12 strings

−→ avec une taille maximale de 50 caract `eres

(103)

Des tableaux des strings

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

focout << prenom[ 4 ] focout << nom[ 4 ] << endl;

}

Le fichier noms.dat:

Nicolas Vandewalle Philippe Ghosez Alain Seret Geoffroy Lumay Thierry Bastin Laurent Dreesen Maryse Hoebeke John Martin Peter Schlagheck Matthieu Verstraete Alejandro Silhanek Herve Caps

David Strivay

Ex ´ecution:

ThierryBastin

(104)

Des tableaux des strings

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

focout << prenom[ 4 ] << '' '' focout << nom[ 4 ] << endl;

}

Le fichier noms.dat:

Nicolas Vandewalle Philippe Ghosez Alain Seret Geoffroy Lumay Thierry Bastin Laurent Dreesen Maryse Hoebeke John Martin Peter Schlagheck Matthieu Verstraete Alejandro Silhanek Herve Caps

David Strivay

Ex ´ecution:

Thierry Bastin

(105)

Des tableaux des strings

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

focout << prenom[ 4 ] << '' '' focout << nom[ 4 ] << endl;

}

Le fichier noms.dat:

Nicolas Vandewalle Philippe Ghosez Alain Seret Geoffroy Lumay Thierry Bastin Laurent Dreesen Maryse Hoebeke John Martin Peter Schlagheck Matthieu Verstraete Alejandro Silhanek Herve Caps

David Strivay

−→ le caract `ere ’espace’ (no. 32) s ´epare des diff ´erents strings

−→ dans le fichier noms.dat

(106)

Des tableaux des strings

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

focout << prenom[ 4 ] << '' '' focout << nom[ 4 ] << endl;

}

Le fichier noms.dat:

NicolasVandewalle PhilippeGhosez AlainSeret GeoffroyLumay ThierryBastin LaurentDreesen MaryseHoebeke JohnMartin PeterSchlagheck MatthieuVerstraete AlejandroSilhanek HerveCaps

DavidStrivay

(107)

Des tableaux des strings

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

focout << prenom[ 4 ] << '' '' focout << nom[ 4 ] << endl;

}

Le fichier noms.dat:

NicolasVandewalle PhilippeGhosez AlainSeret GeoffroyLumay ThierryBastin LaurentDreesen MaryseHoebeke JohnMartin PeterSchlagheck MatthieuVerstraete AlejandroSilhanek HerveCaps

DavidStrivay

Ex ´ecution:

PeterSchlagheck MatthieuVerstraete

(108)

Des tableaux des strings

#include <iostream>

#include <fstream>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

focout << prenom[ 4 ] << '' '' focout << nom[ 4 ] << endl;

}

Le fichier noms.dat:

Nicolas Vandewalle Philippe Ghosez Alain Seret Geoffroy Lumay Thierry Bastin Laurent Dreesen Maryse Hoebeke John Martin Peter Schlagheck Matthieu Verstraete Alejandro Silhanek Herve Caps

David Strivay

−→ trouver le premier nom dans l’alphab `ete !

(109)

Des tableaux des strings

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

fo

fochar nom0[ 50 ] = ''|'';

fofor ( int i = 0; i < N; i++ ) fofoif ( strcmp( nom[ i ], nom0 ) < 0 ) fofofostrcpy( nom0, nom[ i ] );

focout << nom0 << endl;

}

Le fichier noms.dat:

Nicolas Vandewalle Philippe Ghosez Alain Seret Geoffroy Lumay Thierry Bastin Laurent Dreesen Maryse Hoebeke John Martin Peter Schlagheck Matthieu Verstraete Alejandro Silhanek Herve Caps

David Strivay

Ex ´ecution:

Bastin

(110)

Des tableaux des strings

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

fo main() {

foconst int N = 13;

fochar nom[ N ][ 50 ];

fochar prenom[ N ][ 50 ];

foifstream inp( ''noms.dat'' );

fofor ( int i = 0; i < N; i++ ) fofoinp >> prenom[ i ] >> nom[ i ];

fo

fochar nom0[ 50 ] = ''|'';

fochar prenom0[ 50 ];

fofor ( int i = 0; i < N; i++ ) fofoif ( strcmp( nom[ i ], nom0 ) < 0 ) fofo{

fofofostrcpy( nom0, nom[ i ] );

fofofostrcpy( prenom0, prenom[ i ] );

fofo}

focout << prenom0 << '' '' << nom0 << endl;

}

Le fichier noms.dat:

Nicolas Vandewalle Philippe Ghosez Alain Seret Geoffroy Lumay Thierry Bastin Laurent Dreesen Maryse Hoebeke John Martin Peter Schlagheck Matthieu Verstraete Alejandro Silhanek Herve Caps

David Strivay

Ex ´ecution:

Thierry Bastin

(111)

8.3 Les Pointeurs

Un pointeur est un type qui correspond `a une adresse en m ´emoire

D ´eclaration:

int *ip;

−→ ip peut contenir une adresse en m ´emoire

−→ o `u se trouve une variable enti `ere du type int

(112)

8.3 Les Pointeurs

Un pointeur est un type qui correspond `a une adresse en m ´emoire

D ´eclaration:

int* ip;

−→ ip peut contenir une adresse en m ´emoire

−→ o `u se trouve une variable enti `ere du type int

(113)

8.3 Les Pointeurs

Un pointeur est un type qui correspond `a une adresse en m ´emoire

D ´eclaration:

int *ip;

double *a;

−→ a peut contenir une adresse en m ´emoire

−→ o `u se trouve une variable flottante du type double

−→ comment (et pourquoi) utiliser ?

(114)

8.3 Les Pointeurs

int n = 3;

int *ip;

ip = &n;

int k = *ip;

;

;

(115)

8.3 Les Pointeurs

int n = 3;

int *ip;

ip = &n;

int k = *ip;

;

;

(116)

8.3 Les Pointeurs

int n = 3;

int *ip;

ip = &n;

int k = *ip;

;

;

Références

Documents relatifs

Casio : menu PRGM s´electionner le programme SUITE puis F1 pour EXE Faire plusieurs essais N=3, N=10 et N=25. 5 V´ erification

On peut donc consid´erer indiff´eremment une mesure de Radon comme une forme lin´eaire continue T sur un espace de fonctions continues et comme une mesure ensembliste sign´ee µ,

Construire sur un seul graphique les nuages de points de l’indice de Qu´ etelet en fonction de la taille, en fonction du poids, pour chaque sexe8. Repr´ esenter, sur un seul

Puis on r´ eit` ere l’exp´ erience : on croise la derni` ere drosophile s´ electionn´ ee avec un individu h´ et´ erozygote, on choisit au hasard un individu de la descendance

Un arbre de profondeur n&gt;1 est représenté par un tableau contenant la représentation de son fils gauche, puis celle de son fils droit, puis la valeur associé à sa racine..

Passage de param` etre par void* : on fournit l’emplacement m´ emoire contenant le param` etre sans en pr´ eciser

Des individus peuvent aussi être déclarés positifs à tort. Lorsqu'il est positif, le test ELISA est 

Pour représenter une liste d'entiers sous forme chaînée, on crée le modèle de structure cellule qui a deux champs : un champ valeur de type int , et un champ suivant