Arduino 2D Array

  1. 2d Array Initialization in Arduino
  2. Storing Data Into 2d Array in Arduino
  3. Replace a Value in 2d Array at a Specific Position in Arduino
  4. Retrieving a Value From a Specific Position in 2d Array in Arduino
  5. Arduino MatrixMath Library for 2d Arrays

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

2d Array Initialization in Arduino

2d array initialization is quite similar to 1d array initialization. In a 2d 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, nRow is a variable of type int which is used to defines the number of rows in the 2d array. nCol is a variable of type int which is used to define the number of columns in the 2d array, and myArray is an array of type int, which is used to store the given int values. You can change all these values according to the given data. Note that you can also define other data type arrays like float in Arduino. Also, note that you have to define the number of rows and columns or at least the number of columns during arrayinitialization.

Storing Data Into 2d Array in Arduino

If you want to store data into a 2d array, you have to use two loops for this purpose. To store data in a 2d array, you have to go to every position in a 2d array and store data there. A 2d array has many elements, so it will be time-consuming to store data at every position manually. To save time, you can use two loops to go to every position and store the given data at the 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 are using two loops to go to every position in a 2d array and store the given data there. In this example, the data is a variable whose value is zero, but you can change its value according to given data. This method will store or replace data at every position in the 2d array.

Replace a Value in 2d Array at a Specific Position in Arduino

If you want to store or replace data at only one position, you only have to use the assignment operator for the specific position. 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 are replacing the value present at the nRow and nCol position in the 2d array to 0.

Retrieving a Value From a Specific Position in 2d Array in Arduino

If you want to get a value from a specific position in 2d array, you can use the assignment operator. 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}
    };
    int myValue = myArray[nRow][nCol];
}

In the above code, we are retrieving a value present at the nRow and nCol position in 2d array, which is 8.

Arduino MatrixMath Library for 2d Arrays

You can use the MatrixMath library present in Arduino for many operations like addition, subtraction, multiplication, inverse, and printing of 2d arrays. Read the documentation of the library for more information and check an example here.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Arduino Array

  • Arduino Length of Array