Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino array


May 15, 2021 Arduino


Table of contents


An array is a continuous set of memory locations of the same type. /b10> To refer to a specific location or element in an array, we specify the name of the array and the location number of the specific element in the array.

The following illustration shows an array of integers called C, which contains 11 elements. /b10>You can refer to any of these elements by giving the array name, followed by the position number of a particular element: square brackets. /b11> Position numbers are more formally referred to as substations or indexes (the number specifies the number of elements starting with the array). /b12> The first element has a subse cursor of 0 (zero), sometimes referred to as a zero element.

"Thus, the elements of array C are C,0,C,1,C,2, and so on." /b10> The highest underse cursor in array C is 10, which is 1 less than the number of elements in the array. /b11> Array names follow the same conventions as other variable names.


Arduino array


The subse cursor must be an integer or an integer expression (using any integer type). /b10> If the program uses an expression as a subsection, the program evaluates the expression to determine the subsection. /b11>For example, if we assume that the variable a is equal to 5 and variable b is equal to 6, the statement adds the array element C .

The underseed array name is a left value that can be used on the left side of the assignment, just like a non-array variable name.

Let's examine array C more carefully in a given diagram. /b10> The name of the entire array is C. I ts 11 elements are called C.0. to C.10. /b11>The value of C[0 is -45, the value of C is 6, the value of C is 0, the value of C is 62, and the value of C is 78.

To print the sum of the values contained in the first three elements of array C, we will write:

Serial.print (C[ 0 ] + C[ 1 ] + C[ 2 ] );

To divide the value of C.6 by 2 and assign the result to variable x, we'll write:

x = C[ 6 ] / 2;

Declare the array

Arrays take up space in memory. To specify the type of element and the number of elements required for an array, use the following declaration:

type arrayName [ arraySize ] ;

The compiler retains the appropriate amount of memory (in recall, the declaration of memory retention is more appropriately referred to as a definition). /b11> ArraySize must be an integer constant greater than zero. For example, to tell the compiler to keep 11 elements for integer array C, use the declaration:

int C[ 12 ]; // C is an array of 12 integers

An array can be declared as a value that contains any non-referenced data type. /b10> For example, you can use an array of string types to store strings.

An example of using an array

This section provides many examples to demonstrate how to declare, initialize, and manipulate arrays.

Example 1: Declare an array and use a loop to initialize the elements of the array

The program declares an integer array of 10 elements n. L ine a-b initializes array elements to zero using the For statement. /b11> Like other automatic variables, automatic arrays are not implicitly initialized to zero. /b12>The first output statement (row c) displays the column title of the column printed in subsequent for statements (row d-e) and prints the array in table format.

Example

int n[ 10 ] ; // n is an array of 10 integers

void setup () {

}

void loop () {
   for ( int i = 0; i < 10; ++i ) // initialize elements of array n to 0 {
      n[ i ] = 0; // set element at location i to 0
      Serial.print (i) ;
      Serial.print (‘\r’) ;
   }
   for ( int j = 0; j < 10; ++j ) // output each array element's value {
      Serial.print (n[j]) ;
      Serial.print (‘\r’) ;
   } 
}

Result - It produces the following results:

Components Value

0

1

2

3

4

5

6

7

8

9

0

0

0

0

0

0

0

0

0

0


Example 2: Initialize the array in a declaration using the initializer list

Array elements can also be initialized in array declarations by following the equal sign after the array name and a list of initializers separated by braces and commas. /b10>The program uses an initializer list to initialize an array of integers with 10 values (row a) and print the array in table format (line b-c).

Example

// n is an array of 10 integers
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 } ;

void setup () {

}

void loop () {
   for ( int i = 0; i < 10; ++i ) // initialize elements of array n to 0 {
      Serial.print (i) ;
      Serial.print (‘\r’) ;
   }
   for ( int j = 0; j < 10; ++j ) // output each array element's value {
      Serial.print (n[j]) ;
      Serial.print (‘\r’) ;
   } 
}

Result - It produces the following results:

Components Value

0

1

2

3

4

5

6

7

8

9

32

27

64

18

95

14

90

70

60

37


Example 3: The elements of the array are coupled

Typically, the elements of an array represent a series of values to be used in calculations. /b10>For example, if the elements of an array represent exam scores, the professor might want to add up the elements of the array and use the sum to calculate the average score for the class exam. /b11> The program will include a value in the 10-element integer array a to be added.

Example

const int arraySize = 10; // constant variable indicating size of array
int a[ arraySize ] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
int total = 0;

void setup () {

}
void loop () {
   // sum contents of array a
   for ( int i = 0; i < arraySize; ++i )
      total += a[ i ];
   Serial.print (“Total of array elements : ") ;
   Serial.print(total) ;
}

Result - It produces the following results:

Total of array elements: 849

Arrays are important to Arduino and more attention should be paid. Here are some important concepts related to arrays that Aduino should be aware of:

Serial number Concepts and descriptions
1 Pass the array to the function

To pass array parameters to a function, specify the name of the array without any parentheses.

2 Multi-dimensional array

An array with two dimensions (that is, subse cured) usually represents a table of values consisting of information arranged in rows and columns.