TP : tableaux 2D#

#include <iostream>
#include <vector>
using namespace std;

Exercice 1 : construction pas à pas d'un tableau 2D: les quatre étapes#

  • Déclarer un tableau d'entiers t à deux dimensions; donc de type vector<vector<int>>:

/// BEGIN SOLUTION
vector<vector<int>> t;
/// END SOLUTION
  • Remarquer qu'il est vide:

t
{}
type: std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >
t.size()
0
type: unsigned long
  • On alloue t pour qu'il ait trois lignes:

t = vector<vector<int>>(3);
  • Vérifions que t a trois lignes vides:

t
{ {}, {}, {} }
type: std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >
  • On alloue la première ligne pour qu'elle ait deux éléments

t[0] = vector<int>(2);
  • Allouer de même les deux autres lignes:

/// BEGIN SOLUTION
t[1] = vector<int>(2);
t[2] = vector<int>(2);
/// END SOLUTION
{ 0, 0 }
type: std::vector<int, std::allocator<int> >
  • La valeur de quelle ligne et quelle colonne est elle changée par l'affectation suivante?

t[0][1] = 1
1
type: int
  • Vérifier:

t
{ { 0, 1 }, { 0, 0 }, { 0, 0 } }
type: std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >
  • Mettre la valeur 42 en troisième ligne et première colonne

/// BEGIN SOLUTION
t[2][0] = 42;
/// END SOLUTION
42
type: int
  • Vérifier:

t
{ { 0, 1 }, { 0, 0 }, { 42, 0 } }
type: std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >
CHECK( t[2][0] == 42 );

Digression : alignement vertical dans les affichages#

Pour afficher joliment un tableau, on doit gérer l'alignement vertical des colonnes. Cela peut se faire en C++ avec setw qui réserve un certain nombre de caractères pour l'affichage d'un nombre. Pour utiliser setw, il faut au préalable charger la bibliothèque iomanip:

#include <iomanip>
  • Exécuter plusieurs fois le code suivant en variant le paramètre de setw:

cout << "|" << setw(3) << 12 << "|" << endl;
| 12|
  • Exécuter plusieurs fois le code suivant en variant le paramètre de setw:

for ( int i = 0; i < 10; i++ ) {
    for ( int j = 0; j < 10; j++ )
        cout << setw(3) << i + j << " ";
    cout << endl;
}
  0   1   2   3   4   5   6   7   8   9 
  1   2   3   4   5   6   7   8   9  10 
  2   3   4   5   6   7   8   9  10  11 
  3   4   5   6   7   8   9  10  11  12 
  4   5   6   7   8   9  10  11  12  13 
  5   6   7   8   9  10  11  12  13  14 
  6   7   8   9  10  11  12  13  14  15 
  7   8   9  10  11  12  13  14  15  16 
  8   9  10  11  12  13  14  15  16  17 
  9  10  11  12  13  14  15  16  17  18 

Exemple de construction et d'affichage d'un plus gros tableau 2D#

int L = 10;
int C = 10;
  • Les cellules suivantes construisent un tableau à deux dimensions et l'affichent. Annoter chaque cellule avec un commentaire précisant quelle étape de la construction elle effectue (initialisation, allocation des lignes, déclaration, allocation; dans quel ordre?)

BEGIN SOLUTION

Déclaration:

END SOLUTION

vector<vector<int>> tableMultiplication;

BEGIN SOLUTION

Allocation:

END SOLUTION

tableMultiplication = vector<vector<int>>(L);

BEGIN SOLUTION

Allocation des lignes:

END SOLUTION

for ( int i = 0; i < L; i++ ) {
    tableMultiplication[i] = vector<int>(C);
}

BEGIN SOLUTION

Initialisation:

END SOLUTION

for ( int i = 0; i < L; i++ ) {
    for ( int j = 0; j < C; j++ ) {
        tableMultiplication[i][j] = i * j;
    }
}
tableMultiplication[6][9]
54
type: int

Affichage direct par Jupyter :

tableMultiplication
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 }, { 0, 3, 6, 9, 12, 15, 18, 21, 24, 27 }, { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36 }, { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45 }, { 0, 6, 12, 18, 24, 30, 36, 42, 48, 54 }, { 0, 7, 14, 21, 28, 35, 42, 49, 56, 63 }, { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72 }, { 0, 9, 18, 27, 36, 45, 54, 63, 72, 81 } }
type: std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >

Joli affichage :

for ( int i = 0; i < L; i++ ) {
    for ( int j = 0; j < C; j++ ) {
        cout << setw(2) << tableMultiplication[i][j] << " ";
    }
    cout << endl;
}
 0  0  0  0  0  0  0  0  0  0 
 0  1  2  3  4  5  6  7  8  9 
 0  2  4  6  8 10 12 14 16 18 
 0  3  6  9 12 15 18 21 24 27 
 0  4  8 12 16 20 24 28 32 36 
 0  5 10 15 20 25 30 35 40 45 
 0  6 12 18 24 30 36 42 48 54 
 0  7 14 21 28 35 42 49 56 63 
 0  8 16 24 32 40 48 56 64 72 
 0  9 18 27 36 45 54 63 72 81