Arrays

Hello! Today I am going to explain how arrays work and why they are so important in procedural programming.

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier). Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be accessed using the same identifier, with the proper index. These elements are numbered from 0 to 4, being 0 the first and 4 the last; In C++, the first element in an array is always numbered with a zero (not a one), no matter its length.

Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

type name [n]

where type is a valid type (such as int or float), name is a valid identifier and the n field (which is always enclosed in square brackets []), specifies the length of the array in terms of the number of elements.

Therefore, the v array, with five elements of type int, can be declared as such:

int v[5];

Initializing an array

By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.

But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:

int v[5] ={0, 85, 264, 2, 4};

The number of values between braces {} shall not be greater than the number of elements in the array. For example, in the example above, v was declared having 5 elements , and the braces {} contained exactly 5 values. If declared with less, the remaining elements are set to their default values (which for fundamental types, means they are filled with zeroes). For example:

int x[5] ={1,2,3};

will save the values 1,2 and 3 on the first three positions and the rest will be filled with zeros.

When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty []. In this case, the compiler will assume automatically a size for the array that matches the number of values included between the braces {}:

int v [] ={0, 85, 264, 2, 4};

Accessing the values of an array

The values of any of the elements in an array can be accessed just like the value of a regular variable of the same type. The syntax is:

name[index]

For example:

v[3] =150;

will change the value of the third element inside the array to 150. As you can see the expression name[index] acts as a normal variable of type int or float or whatever it is declared.

Example code:

#include <iostream>
using namespace std;

int v [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
  for ( n=0 ; n<5 ; n++ )
  {
    result =result + foo[n];
  }
  cout << result;
  return 0;
}
//this code will output the value 12206 inside the console

I hope this helped you acquire a basic understanding of arrays and if you have any further questions feel free to ask me !

Keep on coding 😉 !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s