JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Arduino 2D Array

Author:JIYIK Last Updated:2025/04/16 Views:

In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data.


2D Array Initialization in Arduino

Two-dimensional array initialization is very similar to one-dimensional array initialization. In a two-dimensional array, we have to define the number of rows and columns and then initialize it with some data. For example, see the code below.

void setup() {
  int nRow = 2;
  int nCol = 4;
  int myArray[nRow][nCol] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
}

In the above code, nRowis inta variable of type that defines the number of rows of W in the two-dimensional array. nColis a variable of type intthat defines the number of columns in the two-dimensional array, and myArrayis intan array of type that stores the given intvalues. You can change all these values ​​based on the given data. Note that you can also define other data type arrays in Arduino, such as float. Also, note that during array initialization, you must define the number of rows and columns, or at least the number of columns.


Storing data in a 2D array in Arduino

If you want to store data into a two-dimensional array, you must use two loops. To store data in a two-dimensional array, you must go to each position in the two-dimensional array and store the data there. A two-dimensional array contains many elements, so manually storing data at each position will be time-consuming. To save time, you can use two loops to go to each position and store the given data at a specific position. For example, see the code below.

void setup() {
  int data = 0;
  int myArray[nRow][nCol];
  for (int nr = 0; nr < nRow; nr++) {
    for (int nc = 0; nc < nCol; nc++) {
      myArray[nr][nc] = data++;
    }
  }
}

In the above code, we use two loops to go to each position in the 2d array and store the given data there. In this example, data is a variable with a value of zero, but you can change its value based on the given data. This method will arraystore or replace the data at each position in the 2d.


Replace values ​​in 2D array at specific position in Arduino

If you want to store or replace data in only one location, you only need to use the assignment operator for that specific location. For example, see the code below.

void setup() {
  int nRow = 2;
  int nCol = 4;
  int myArray[nRow][nCol] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
  myArray[nRow][nCol] = 0;
}

In the above code, we replace the values ​​at positions nRowand of the two-dimensional array with .nCol0


Retrieve value at specific position in 2D array in Arduino

If you want to get a value from a specific position in a two-dimensional array, you can use the assignment operator. For example, see the following code.

void setup() {
  int nRow = 2;
  int nCol = 4;
  int myArray[nRow][nCol] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
  int myValue = myArray[nRow][nCol];
}

In the above code, we are retrieving the values ​​at positions nRowand in the two-dimensional array, i.e.nCol8


MatrixMathArduino library for 2D arrays

You can use the MatrixMath library provided in Arduino to perform many operations such as addition, subtraction, multiplication, inverse operations, and printing 2D arrays. Read the documentation of this library for more information and check out the examples here.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stopping a loop in Arduino

Publish Date:2025/04/16 Views:150 Category:C++

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi

Arduino prints to console

Publish Date:2025/04/16 Views:68 Category:C++

This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything

Arduino Array Length

Publish Date:2025/04/16 Views:181 Category:C++

Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p

Arduino mills() function

Publish Date:2025/04/16 Views:156 Category:C++

This tutorial will discuss millis() the use of functions in different applications in Arduino. This tutorial will also discuss some examples to better understand millis() the functions. millis() Checking the elapsed time in Arduino using th

Arduino 二维数组

Publish Date:2024/03/13 Views:181 Category:C++

二维数组与 1d 数组非常相似,像矩阵一样包含行和列。

在C中将整数转换为字符

Publish Date:2024/01/03 Views:135 Category:C语言

本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。

如何在 C++ 中实现毫秒级的睡眠

Publish Date:2024/01/02 Views:238 Category:C++

本文介绍了在 C++ 中使用不同方法暂停程序执行,实现睡眠的方法。本文介绍了在 C++ 中睡眠毫秒的方法。使用 std::this_thread::sleep_for 方法在 C++ 中睡眠

如何在 C++ 中将双精度数四舍五入到整数上

Publish Date:2024/01/02 Views:156 Category:C++

本文演示了如何在 C++ 中把双精度数四舍五入到整数中。本文将为大家讲解几种在 C++ 中如何将双精度数四舍五入为整数的方法。使用 round() 函数将双精度数四舍五入到整数

如何在 C++ 中以毫秒为单位获取时间

Publish Date:2024/01/02 Views:149 Category:C++

本文介绍了如何在 C++ 中获取以毫秒为单位的时间。本文将介绍多种 C++ 方法,介绍如何以毫秒为单位获取时间。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial