Objectifs

  1. A quoi sert un tableau 2D ?
  2. Comment déclarer un tableau 2D ?
  3. Comment initialiser un tableau 2D ?
  4. Comment remplir un tableau 2D ?
  5. Les erreurs à éviter

Comment déclarer un tableau ?




const int Taille=3;

int Tab2D[Taille][Taille];

Comment initialiser un tableau ?

int Tab2D[3][3]           ={{0,1,2},{3,4,5},{6,7,8}};

int Tab2D[Taille][Taille] ={{0,1,2},{3,4,5},{6,7,8}};

int Tab2D[][]             ={{0,1,2},{3,4,5},{6,7,8}}; // Non valide

Comment remplir un tableau (ou initialiser) ?

Exemple

const int Taille=3;

int Tab2D[Taille][Taille] ={{0,1,2},{3,4,5},{6,7,8}};

int Tab2D_1[3][3]         ={{0,1,2},{3,4,5},{6,7,8}};




void setup() {




  // Init port série

  Serial.begin (9600);




  // Init tab 1 

  for (int i=0; i<Taille; i++)

    for (int j=0; j<Taille; j++)

      Tab2D[i][j]=0;




  //Init tab 2

  Tab2D[0][0]=-10;

  Tab2D[2][2]=-20;




  //Init tab 3




}




void loop() {

  for (int i=0; i<Taille; i++)

  {

    for (int j=0; j<Taille; j++)

    {

      Serial.print(Tab2D[i][j]);

      Serial.print("\t");

      delay(100);

    }

    Serial.print("\n");

  }

  Serial.print("\n \n");

  delay(1000);

}

Les erreurs à éviter

  1. Dépassement de la taille du tableau
  2. Mauvais choix de l’indice
Click to rate this post!
[Total: 1 Average: 5]
Catégories : Arduino

0 commentaire

Laisser un commentaire

Emplacement de l’avatar

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Anti-Robot *

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

FPGA | Arduino | Matlab | Cours will use the information you provide on this form to be in touch with you and to provide updates and marketing.