ARRAYS

Arrays are variables that can hold multiple values.
Here we have an array of string values representing each day in a week:

string sDays[] = {“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”}

To access an element of a string we need to specify the location (index) of that element.

print sDays[4] will output “Fri” because the index in arrays starts from 0, which in this case stores the value “Mon”.

Often, we populate arrays during the program’s execution. In those cases, we define the array’s size beforehand.
Let’s say that we want to record max. daily temperature for each day of the year. We define array like this:

int iDailyTemperature[365]

Then on January 1. we assign iDailyTemperature[0] = 20 , on January 2. iDailyTemperature[1] = 23 , and so on…

Arrays can be multidimensional.

iDailyTemperature[day][hour]
can be used to keep temperatures for each hour in a day, every day in a year, and defined as

int iDailyTemperature[365][24]

Numbers 365 and 24 tell us how many elements are in each dimension of the array.

[Day 1, Hour 00] [Day 1, Hour 01] … [Day 1, Hour 23]
[Day 2, Hour 00] [Day 2, Hour 01] … [Day 2, Hour 23]

[Day 365, Hour 00] [Day 365, Hour 01] … [Day 365, Hour 23]

We could even go further and add years as another dimension of an array.

iDailyTemperature[year][day][hour]

Notice that each element of the array can be accessed using its index [x , y, z], where x, y, z are ranging from 0 to max_number_of_elements – 1.

Leave a comment

Your email address will not be published.