Home

Introduction to Arrays

 

Definition

A collection is a series of items that have the same characteristics, that is, they can be described using the same criteria. For example, having a group of houses, each can be described using the same options as the other houses: type of house, number of bedrooms, the year the house was built, etc. A collection must have at least one item. The item or each item of the collection must be clearly defined, that is, its characteristics must be identified. At one particular time when accessing a collection, it must have a size. The size of a collection is the number of items that it contains. On this issue, there are two types of collections: those that have a fixed size and those that can grow or shrink.

An array is a collection of items that has a fixed size. This means that, when an array is created, its size must be set and it cannot change. When you create an array, you must give two pieces of information: the type of items that the array contains and the number of items in the array. Each item of an array is also called an element. Based on the type of value that each element would require, the compiler prepares to reserve an area in memory for each one. Using the second piece of information, the compiler would evaluate how much total space the whole collection would need.

Creating an Array

When programming in C++/CLI, there are two types of arrays: C/C++ regular arrays and managed arrays. We will briefly review the former and we will use the latter for the rest of this ebook.

Based on what we have mentioned so far about collections and arrays, the basic formula to create an array in C++ is:

DataType ArrayName[Size];

The DataType is any of the data types we have learned so far. It could be an integer, a decimal type, a Boolean type, or a character, etc. The name of an array follows the same rules we have applied to other variables. The Size of our formula is the number of items of the array. Here is an example of declaring an array:

int main()
{
    int Numbers[5];

    return 0;
}

After this declaration, the compiler reserves five portions of memory in the stack. Each portion must be able to accommodate an integer. The elements of the array are positioned contiguously in memory and each can be identified by its index. The first element has an index of 0. The second element has an index of 1, and so on. This results in the last element having an index of Size-1:

Element0 Element1 Element2 Element3 Element4
 

Initializing an Array

Initializing an array consists of assigning a value to each of its elements. To locate an element of an array, you type the name of the array variable followed by an opening square bracket "[" and a closing square bracket "]":

Element[0] Element[1] Element[2] Element[3] Element[4]

Once you have identified an element, you can assign it the desired value. Here are examples:

int main()
{
    int Numbers[5];

    Numbers[0] = 192;
    Numbers[1] = 36;
    Numbers[2] = 48007;
    Numbers[3] = -53;
    Numbers[4] = 6;

    return 0;
}

In the same way, to retrieve the value of an element, access it by its index:

using namespace System;

int main()
{
    int Numbers[5];

    Numbers[0] = 192;
    Numbers[1] = 36;
    Numbers[2] = 48007;
    Numbers[3] = -53;
    Numbers[4] = 6;

    Console::WriteLine(L"Number[0]: {0}", Numbers[0]);
    Console::WriteLine(L"Number[1]: {0}", Numbers[1]);
    Console::WriteLine(L"Number[2]: {0}", Numbers[2]);
    Console::WriteLine(L"Number[3]: {0}", Numbers[3]);
    Console::WriteLine(L"Number[4]: {0}\n", Numbers[4]);

    return 0;
}

This would produce:

Number[0]: 192
Number[1]: 36
Number[2]: 48007
Number[3]: -53
Number[4]: 6

Press any key to continue . . .

When initializing an array, instead of accessing each element and assigning it a value, after declaring the array but before the semi-colon, you can assign the list the values of the elements inside of curly brackets. The values must be separated by commas. Here is an example:

using namespace System;

int main()
{
    int Numbers[5] = { 192, 36, 48007, -53, 6 };

    Console::WriteLine(L"Number[0]: {0}", Numbers[0]);
    Console::WriteLine(L"Number[1]: {0}", Numbers[1]);
    Console::WriteLine(L"Number[2]: {0}", Numbers[2]);
    Console::WriteLine(L"Number[3]: {0}", Numbers[3]);
    Console::WriteLine(L"Number[4]: {0}\n", Numbers[4]);

    return 0;
}

If you declare and initialize an array like this, the compiler is equipped to calculate the number of elements. For this reason, you can omit the Size:

using namespace System;

int main()
{
    int Numbers[] = { 192, 36, 48007, -53, 6 };

    Console::WriteLine(L"Number[0]: {0}", Numbers[0]);
    Console::WriteLine(L"Number[1]: {0}", Numbers[1]);
    Console::WriteLine(L"Number[2]: {0}", Numbers[2]);
    Console::WriteLine(L"Number[3]: {0}", Numbers[3]);
    Console::WriteLine(L"Number[4]: {0}\n", Numbers[4]);

    return 0;
}

Type Defining an Array

If you happen to create various arrays of the same kind, you can use the typedef keyword to create a custom name for similar arrays. The syntax used is:

typedef DataType ArrayName[Size];

The typedef keyword is required

DataType must be a known (int, char, double, etc) or an already defined data type (such as a class)

ArrayName is a regular name for a variable

The size of the variables is enclosed between square brackets.

An example of such a programmer defined data type is:

typedef int Player[11];

After this statement, the name Player by itself represents an array of 11 integers. Therefore, the word Player can be used to declare an array of 11 items. Here are examples:

using namespace System;

int main()
{
    typedef string Players[11];
    typedef unsigned int Categories[6];
    typedef double Salaries[5];

    // Each team is an array of 11 members of type Player
    Players Arsenal, Juventus, DCUnited, Canon;
    // Each variable here is an array of 6 positive integers of type Category
    Salaries HourlySalary, WeeklySalary;
    // Each category is a list of 5 double-precision numbers
    Categories BookCategory, MusicCategory, Videos;
    
    return 0;
}
 

Home Copyright © 2007-2013, FunctionX Next