Diferencia entre revisiones de «matriz»

De ArduWiki
Saltar a: navegación, buscar
(EJemplo 1)
(Ejemplo 3)
 
(No se muestran 12 ediciones intermedias del mismo usuario)
Línea 1: Línea 1:
 
== Descripcion ==
 
== Descripcion ==
Una matriz es una variable que puede contener multiples elementos del mismo tipo.
+
Una matriz (array en ingles) es una variable que puede contener multiples elementos del mismo tipo.
  
 
== Sintaxis ==
 
== Sintaxis ==
Línea 21: Línea 21:
 
== Ejemplo 1 ==
 
== Ejemplo 1 ==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
byte matriz1[] = {21,250,2,64};
+
char matriz1[] = "Arduino";
int matriz2[] = {2019,7,14,8,0,0};
+
byte matriz2[] = {21,250,2,64};
 +
int matriz3[] = {2019,7,14,8,0,0};
 +
</syntaxhighlight>
 +
 
 +
== Ejemplo 2 ==
 +
<syntaxhighlight lang="c++">
 +
const int matriz[][6] = {
 +
  {2019,7,14,8,0,0},
 +
  {2019,7,19,8,0,0},
 +
  {2019,7,22,8,0,0}
 +
};
 +
 
 +
void setup(){
 +
  Serial.begin(9600);
 +
  for (byte i=0; i<3; i++){
 +
      for (byte j=0; j<6; j++){
 +
        Serial.print(matriz[i][j]);
 +
      }
 +
      Serial.println();
 +
  }
 +
}
 +
 
 +
void loop(){
 +
  //Nada
 +
}
 +
</syntaxhighlight>
 +
 
 +
== Ejemplo 3 ==
 +
No se puede crear una '''matriz''' de matrices. Pero si se puede usar [[asterisco|punteros]] a matrices, asi:
 +
 
 +
<syntaxhighlight lang="c++">
 +
void setup(){
 +
  Serial.begin(9600);
 +
  byte cero[] = { 0xA6, 0xD4, 0xAC, 0xF8 };
 +
  byte uno[] = { 0xE9, 0x48, 0xCC, 0xE3 };
 +
  byte dos[] = { 0x46, 0x6F, 0xA5, 0xF8 };
 +
  byte tres[] = { 0xC2, 0x9D, 0xFC, 0x33 };
 +
  byte* matriz[4] = {cero, uno, dos, tres};  //Usando puntero
 +
  for (byte i=0; i<4; i++){
 +
      for (byte j=0; j<4; j++){
 +
        Serial.print(matriz[i][j], HEX);
 +
        Serial.print(", ");
 +
      }
 +
      Serial.println();
 +
  }
 +
}
 +
 
 +
void loop() {
 +
  //Nada
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Vea también ==
 
== Vea también ==
 
* [[variable publica]]
 
* [[variable publica]]
 +
* [[variable local]]
 
* [[static]]
 
* [[static]]
 
* [[volatile]]
 
* [[volatile]]
 
* [[const]]
 
* [[const]]
 +
* [[define|#define]]
 
* [[Palabras reservadas]]
 
* [[Palabras reservadas]]
 
* [[Comentarios]] - (//)
 
* [[Comentarios]] - (//)
 +
* [[sizeof()]]
  
 
== Referencias externas ==
 
== Referencias externas ==
* [https://forum.arduino.cc/index.php?topic=208323.0] Matrices
+
* [https://aprendiendoarduino.wordpress.com/tag/arrays/ Arrays y String] - Aprendiendo Arduino
 +
* [https://www.luisllamas.es/arduino-array-dinamico/ Array dinamico] - Luis Llamas
 +
* [http://codigoelectronica.com/blog/arduino-array Array en Arduino] - Codigo electronica
 +
* [https://forum.arduino.cc/index.php?topic=208323.0 Matrices] - Foro Arduino
  
 
[[Category:Variables]]
 
[[Category:Variables]]

Revisión actual del 14:54 2 feb 2020

Descripcion

Una matriz (array en ingles) es una variable que puede contener multiples elementos del mismo tipo.

Sintaxis

tipo nombre[] = {elemento1, elemento2...};
tipo nombre[][n] = {{1,2,3},{4,5,6},{7,8,9}};

Parámetros

tipo
puede ser char, byte, int, unsigned int, long, unsigned long o float.
nombre
es el nombre que se asignara a la matriz
n
Es el numero de elementos

Comentarios

  • En un matriz se puede obviar el numero de elementos (filas), pero en una matriz bidireccional es obligatorio indicar el numero de columnas.

Advertancia

Nada.

Ejemplo 1

char matriz1[] = "Arduino";
byte matriz2[] = {21,250,2,64};
int matriz3[] = {2019,7,14,8,0,0};

Ejemplo 2

const int matriz[][6] = {
   {2019,7,14,8,0,0},
   {2019,7,19,8,0,0},
   {2019,7,22,8,0,0}
};

void setup(){
   Serial.begin(9600);
   for (byte i=0; i<3; i++){
      for (byte j=0; j<6; j++){
         Serial.print(matriz[i][j]);
      }
      Serial.println();
   }
}

void loop(){
   //Nada
}

Ejemplo 3

No se puede crear una matriz de matrices. Pero si se puede usar punteros a matrices, asi:

void setup(){
   Serial.begin(9600);
   byte cero[] = { 0xA6, 0xD4, 0xAC, 0xF8 };
   byte uno[] = { 0xE9, 0x48, 0xCC, 0xE3 };
   byte dos[] = { 0x46, 0x6F, 0xA5, 0xF8 };
   byte tres[] = { 0xC2, 0x9D, 0xFC, 0x33 };
   byte* matriz[4] = {cero, uno, dos, tres};   //Usando puntero
   for (byte i=0; i<4; i++){
      for (byte j=0; j<4; j++){
         Serial.print(matriz[i][j], HEX);
         Serial.print(", ");
      }
      Serial.println();
   }
}

void loop() {
   //Nada
}

Vea también

Referencias externas