Have you tried to use arrays in C? Want to learn more about them? See the rest of the article to find out.
Introduction
Integer arrays and two-dimensional arrays are very useful in C. If you have a set of values you want to put in variables and then access them later through indexes, arrays are the way to go. It’s relatively easy to create an array and then access them later. The next part will illustrate this.Â
How to Declare an Array
The declaration comes with the type of variables you want to use. For example, you want to declare an array of integers with six elements of the variable A. The number of elements will be accessed through a bracketed index number. Before main, you can declare it as a global variable array as:
int A[6];
Alternatively, you can initialize and populate the array’s values beforehand. The example below demonstrates this:
int A[6] = {0, 1, 2, 3, 4, 5};
Note that the first array element starts with 0 as A[0] = 0, and then A[1] = 1, A[2] = 2, A[3] = 3, A[4] = 4, A[5] = 5.
Accessing the Array Variables
After the declaration, you can access the elements in the array inside your main or custom functions. You have to choose the array index you want. For example, suppose you wan to print out element 3 in the array of integers. You can do this simply by:
printf("A[3] is %i\n", A[3]);
And the output would be:
A[3] is 3
An automated and ordered way of accessing the elements in the array is by using iteration. For example, you can use a for-loop to access each element, not needing to type each indexed variable manually.
for(i=0;i<6;i++)
{
printf("A[%i] is %i\n", i, A[i]);
}
and the output would be:
A[0] is 0
A[1] is 1
A[2] is 2
A[3] is 3
A[4] is 4
A[5] is 5
On another note, you are not allowed to pass the values of an array to another array by simply equating them. Suppose you have another array declared as:
int H[6];
The process below is not allowed.
H = A; // This is not allowed.
Instead, you can use iteration to assign each value of the array to the other array as:
for(i=0;i<6;i++)
{
H[i] = A[i];
}
Note* There is a faster way to do these assignment operations in memory, however, this is beyond the scope of this topic.
Two-Dimensional Integer Arrays
If you have a set of integer values that you want to group into columns and rows, you can assign them to a two-dimensional array. In this way, you can access your values in an orderly manner. Consider the two-dimensional array for an LED mood light below.
#define MAX_LED_COLORS 8
uint8_t colors[MAX_LED_COLORS][3] =
{
/**RGB duty Cycle values
Dutycycle0 = BLUE
Dutycycle1 = GREEN
Dutycycle2 - RED
* 0 means 100 % duty cycle
* 31 means 1% duty cycle
*
*/
{1,1,5}, // White - 0
{31,18,0}, // Orange - 1
{0,0,31}, // Cyan - 2
{10,31,0}, // Violet - 3
{0,31,31}, // Blue - 4
{31,10,0}, // Yellow - 5
{31,31,0}, // Red - 6
{31,0,31}, // Green - 7
{25,25,0}, // Pink - 8
};
Here, an integer array is grouped into eight colors (rows) and 3 Duty Cycle values (columns). The eight colors are White, Orange, Cyan, Violet, Blue, Yellow, Red, Green, and Pink. The Duty cycle values can vary, ranging from 0 to 31.
You can easily change each DutyCycle of your RGB LED to change the color of your mood light as:
uint8_t Dutycycle0;
uint8_t Dutycycle1;
uint8_t Dutycycle2;
uint8_t color_ctr;
for(color_ctr;color_ctr<8;color_ctr++)
{
colors[color_ctr][0] = Dutycycle0;
colors[color_ctr][1] = Dutycycle1;
colors[color_ctr][2] = Dutycycle2;
}
Conclusion
Integer arrays are an excellent way to keep track of your variable data in an orderly manner. You must learn how to declare, access, and assign them values correctly. Two-dimensional arrays can give you more options for organizing your data.