Home

Multi-Dimensional Arrays

 

Introduction

So far, when creating an array, we showed a list of values. Here is an example:

550500
1215000
625000
850000

Here is an example of how we have learned to create an array of primitive values:

using namespace System;

int main()
{
    array<double> ^ SingleFamilyValues = { 550550, 1215080, 625550, 850450 };

    Console::WriteLine(L"Altair Realtors");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Properties Listing");
    Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-=");
    Console::WriteLine(L"Single Families");
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[0]);
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[1]);
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[2]);
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[3]);
    Console::WriteLine(L"============================\n");

    return 0;
}

In the same way, you can create as many arrays as you want in your program. Here are examples:

using namespace System;

int main()
{
    array<double> ^ SingleFamilyValues = { 550550, 1215080, 625550, 850450 };
    array<double> ^ TownhouseValues = { 483045, 912834, 328555, 600425 };
	
    Console::WriteLine(L"Altair Realtors");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Properties Listing");
    Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-=");
    Console::WriteLine(L"Single Families");
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[0]);
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[1]);
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[2]);
    Console::WriteLine(L"Market Value: {0:C}", SingleFamilyValues[3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Townhouses");
    Console::WriteLine(L"Market Value: {0:C}", TownhouseValues[0]);
    Console::WriteLine(L"Market Value: {0:C}", TownhouseValues[1]);
    Console::WriteLine(L"Market Value: {0:C}", TownhouseValues[2]);
    Console::WriteLine(L"Market Value: {0:C}", TownhouseValues[3]);
    Console::WriteLine(L"============================\n");

    return 0;
}

This would produce:

Altair Realtors
=---------------------------=
Properties Listing
=--=--=--=--=--=--=-=-=-=-=-=
Single Families
Market Value: $550,500.00
Market Value: $1,215,000.00
Market Value: $625,000.00
Market Value: $850,000.00
=---------------------------=
Townhouses
Market Value: $483,045.00
Market Value: $912,834.00
Market Value: $328,555.00
Market Value: $600,425.00
============================

Press any key to continue . . .

When we introduced arrays, we saw that, when creating and initializing an array, you could also use the gcnew operator before the curly brackets:

array<double> ^ SingleFamilyValues =
	gcnew array<double> { 550550, 1215080, 625550, 850450 };

When an array contains a unique list of items, such an array is referred to as single-dimensional. To indicate that the array is single dimensional, you can use the following formula:

array<DataType, 1> ^ VariableName = gcnew array<DataType>(Dimension);

You can also use the following formula:

array<DataType, 1> ^ VariableName = gcnew array<DataType, 1>(Dimension);

Here is an example

using namespace System;

int main()
{
    array<__wchar_t, 1> ^ HouseTypes = gcnew array<__wchar_t>(4)
	{ L'S', L'C', L'S', L'T' };
    array<int, 1> ^ Bedrooms = gcnew array<int>(4) { 5, 2, 3, 3 };
    array<Byte, 1> ^ Stories = gcnew array<Byte>(4) { 3, 1, 3, 2 };
    array<double, 1> ^ Bathrooms = gcnew array<double>(4)
	{ 3.5, 1, 2.5, 1.5 };
    array<bool, 1> ^ HasGarage = gcnew array<bool>(4)
	{ true, true, false, false };
    array<double, 1> ^ Values = gcnew array<double>(4)
	{ 550500, 115000, 425000, 350000 };

    Console::WriteLine(L"=======================================");
    Console::WriteLine(L"Altair Realtors");
    Console::WriteLine(L"---------------------------------------");
    Console::WriteLine(L"Real Estate Properties Inventory");
    Console::WriteLine(L"---------------------------------------");
    Console::WriteLine(L"Type Beds Baths Lvl Garage?    Value");
    Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    for(int house = 0; house < 4; house++)
    {
        Console::WriteLine(L"{0,3}{1,5}{2,6}{3,4}{4,8}{5,10}",
			   HouseTypes[house], Bedrooms[house],
			   Bathrooms[house], Stories[house],
			   HasGarage[house], Values[house]);
        Console::WriteLine(L"---------------------------------------");
    }

    Console::WriteLine(L"=======================================\n");
    return 0;
}

This would produce:
=======================================
Altair Realtors
---------------------------------------
Real Estate Properties Inventory
---------------------------------------
Type Beds Baths Lvl Garage?    Value
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  S    5   3.5   3    True    550500
---------------------------------------
  C    2     1   1    True    115000
---------------------------------------
  S    3   2.5   3   False    425000
---------------------------------------
  T    3   1.5   2   False    350000
---------------------------------------
=======================================

Press any key to continue . . .

Two-Dimensional Arrays

Instead of creating an array that contains a single list of items, you can create an array that contains two lists. It can be illustrated as follows:

550550 483045
1215080 912834
625550 328555
850450  600425

Such a list is referred to as two-dimensional. Notice that all values are of the same type. In a program, you can also create a two-dimensional array. If you want to initialize the array when creating it, the formula to use is:

array<DataType, 2> ^ VariableName = { { Members-1 }, { Members-2 } };

The array keyword is required. The value 2 is called the rank of the array. The rank of an array specifies the number of sub-lists that the array contains. In this case, 2 indicates that the array is two-dimenionsal. Make sure you give a name to the variable. As always, the array elements are created inside of curly brackets. Because the main array is in fact made of two sub-lists, each sub-list must be created inside its own curly brackets. Here is an example of creating and initializing a two-dimensional array:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = { { 550550, 1215080, 625550, 850450 },
                                        { 483045,  912834, 328555, 600425 } };

    return 0;
}

After initializing the array, you may be interested in accessing its values. The formula to access an element is:

VariableName[x,y]

After specifying the name of the variable, follow it with curly brackets. The first sub-list has an index of 0 and the second sub-list has an index of 1. Inside of a sub-list, the first element has an index of 0, the second has an index of 1, and so on. Therefore, to access an element, inside the square brackets, type the index of the sub-list, followed by the index of the sub-list, followed by a comma, and followed by the index of the element. Here are examples:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = { { 550550, 1215080, 625550, 850450 },
                                        { 483045,  912834, 328555, 600425 } };
	
    Console::WriteLine(L"Altair Realtors");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Properties Listing");
    Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-=");
    Console::WriteLine(L"Single Families");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Townhouses");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,3]);
    Console::WriteLine(L"============================\n");

    return 0;
}

Earlier, we saw that, when creating and initializing an array, you could also apply the gcnew operator before the curly brackets. This is also possible for a two-dimensional array. Here is an example:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = gcnew array<double, 2>
    {
        { 550550, 1215080, 625550, 850450 },
        { 483045,  912834, 328555, 600425 }
    };
	
    . . .

    return 0;
}

If you want, you can first declare the array, then initialize it. To declare a two-dimensional array, the formula to follow is:

array<DataType, 2> ^ VariableName = gcnew array<DataType, 2>(2, Dimension);

The DataType factor is a placeholder for the type of values that the array will hold. The VariableName is the name of the array. The Dimension factor specifies the number of elements that each sub-list will contain.

After declaring the variable, you can initialize the array. To do this, access each element by specifying its sub-list's index and its own index, then assign it the desired value. Here are examples:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = gcnew array<double, 2>(2, 4);
	
    MarketValues[0,0] = 550550;
    MarketValues[0,1] = 1215080;
    MarketValues[0,2] = 625550;
    MarketValues[0,3] = 850450;

    MarketValues[1,0] = 483045;
    MarketValues[1,1] = 912834;
    MarketValues[1,2] = 328555;
    MarketValues[1,3] = 600425;
	
    Console::WriteLine(L"Altair Realtors");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Properties Listing");
    Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-=");
    Console::WriteLine(L"Single Families");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Townhouses");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,3]);
    Console::WriteLine(L"============================\n");

    return 0;
}

You can also use this technique to initialize the array when creating it. Here is an example:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = gcnew array<double, 2>(2, 4)
    {
        { 550550, 1215080, 625550, 850450 },
        { 483045, 912834, 328555, 600425 }
    };
	
    . . .

    return 0;
}

Multi-Dimensional Arrays

Instead of just one or two sub-lists, you can create an array that contains as many sub-lists we you want. An array made of three sub-lists can be illustrated as follows:

550500 483045 224550
1215000 912834 85655
625000 328555 147576
850000 600425 92885

In the same way, an array can be made of 5, 8, 12 or as many sub-lists as you need. This is referred to as a multi-dimensional array. There are two main ways you can create a multi-dimensional array. For example, you can create an array that has a rank of two but that includes as many sub-lists as  you want. If you want to initialize the array when creating it, you can use the following formula to create it:

array<DataType, 2> ^ VariableName = gcnew array<DataType, 2>(Rank, Dimension)
{
    { Members-1 },
    { Members-2 },
    { Members-n } };

The Rank factor specifies the number of sub-lists that the array will contain. The Dimension factor represents the number of elements that each sub-list will contain. For example, to create an array that contains 3 sub-lists and each sub-list will contain 4 elements, you would declare the array as follows:

array<double, 2> ^ MarketValues = gcnew array<double, 2>(3, 4)

Here is an example of such an array:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = gcnew array<double, 2>(3, 4)
    {
        { 550550, 1215080, 625550, 850450 },
        { 483045,  912834, 328555, 600425 },
        { 224550, 85655, 147576, 92885 }
    };

    return 0;
}

To access an element, after using the name of the array, inside the square brackets, type the index of the sub-list, followed by a comma, followed by the index of the particular element you want to access inside the sub-list. Here are examples:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = gcnew array<double, 2>(3, 4)
    {
        { 550550, 1215080, 625550, 850450 },
        { 483045,  912834, 328555, 600425 },
        { 224550, 85655, 147576, 92885 }
    };
	
    Console::WriteLine(L"Altair Realtors");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Properties Listing");
    Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-=");
    Console::WriteLine(L"Single Families");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Townhouses");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Condominiums");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,3]);
    Console::WriteLine(L"============================\n");

    return 0;
}

This would produce:

Altair Realtors
=---------------------------=
Properties Listing
=--=--=--=--=--=--=-=-=-=-=-=
Single Families
Market Value: $550,550.00
Market Value: $1,215,080.00
Market Value: $625,550.00
Market Value: $850,450.00
=---------------------------=
Townhouses
Market Value: $483,045.00
Market Value: $912,834.00
Market Value: $328,555.00
Market Value: $600,425.00
=---------------------------=
Condominiums
Market Value: $224,550.00
Market Value: $85,655.00
Market Value: $147,576.00
Market Value: $92,885.00
============================

Press any key to continue . . .

If you want, you can first declare the array, then initialize it. Here is an example:

using namespace System;

int main()
{
    array<double, 2> ^ MarketValues = gcnew array<double, 2>(3, 4);

    MarketValues[0,0] = 550550;
    MarketValues[0,1] = 1215080;
    MarketValues[0,2] = 625550;
    MarketValues[0,3] = 850450;
    MarketValues[1,0] = 483045;
    MarketValues[1,1] = 912834;
    MarketValues[1,2] = 328555;
    MarketValues[1,3] = 600425;
    MarketValues[2,0] = 224550;
    MarketValues[2,1] = 85655;
    MarketValues[2,2] = 147576;
    MarketValues[2,3] = 92885;
	
    . . .

    return 0;
}

Notice that this simple technique allows you to access the members of the multi-dimensional array using the techniques of a two-dimensional array.

Another technique, more explicit but may appear a little dark to comprehend, to create a multi-dimensional array consists of using the rank in the <> operator to specify the number of pages of the array. The formula to use can be shown as follows:

array<DataType, Lists> ^ Variable = 
	gcnew array<DataType, Lists>(Lists, Sub-Lists, Elements)

To illustrate this formula, try to think that you want to create a few lists of items. From our formula, the Lists factor specifies the number of lists that you will need or use. The Sub-Lists factor specifies the number of sub-lists that each Lists will contain. The Elements factor specifies the number of values that each sub-list will contain. Here is an example of declaring such an array:

array<double, 3> ^ MarketValues = gcnew array<double, 3>(3, 2, 4)

This declaration indicates that the array will have 3 lists. Each list will have 2 sub-lists and each sub-list will have 4 elements. After declaring the variable, you can initialize the array. To initialize the array when creating it, create the square brackets for each list. Inside the square brackets of a list, create the square brackets for the sub-list(s). Inside the square brackets of the sub-list, specify the value of each element, separating them with commas.

To access an element, after using the name of the array, type the normal square brackets of an array. Inside the square brackets, type the index of a list, followed by a comma, followed by the index of a sub-list, followed by a comma, followed by the index of the particular element you want to access inside the sub-list. Here are examples:

using namespace System;

int main()
{
    array<double, 3> ^ MarketValues = gcnew array<double, 3>(3, 2, 4)
    {
        // First List
        {
            // First Sub List
            {
                2250550, // 1st List - 1st Sub List - 1st Element
                1215080, // 1st List - 1st Sub List - 2nd Element
                1625550, // 1st List - 1st Sub List - 3rd Element
                850450   // 1st List - 1st Sub List - 4th Element
            },
            // Second Sub-List
            {
                558750, // 1st List - 2nd Sub List - 1st Element
                503150, // 1st List - 2nd Sub List - 2nd Element
		362650, // 1st List - 2nd Sub List - 3rd Element
		435755  // 1st List - 2nd Sub List - 4th Element
            }
        },
        // Second List
        {
            // First Sub-List
            {
                483045, // 2nd List - 1st Sub List - 1st Element
	        622440, // 2nd List - 1st Sub List - 2nd Element
                808445, // 2nd List - 1st Sub List - 3rd Element
		600425  // 2nd List - 1st Sub List - 4th Element
	    },
	    // Second Sub-List
	    {
		283045, // 2nd List - 2nd Sub List - 1st Element
                412834, // 2nd List - 2nd Sub List - 2nd Element
		328555, // 2nd List - 2nd Sub List - 3rd Element
		300425  // 2nd List - 2nd Sub List - 4th Element
	    }
	},
	// Third List
	{
	    // First Sub-List
	    {
		 624550,  // 3rd List - 1st Sub List - 1st Element
		 85655,   // 3rd List - 1st Sub List - 2nd Element
		 1250755, // 3rd List - 1st Sub List - 3rd Element
		 904685   // 3rd List - 1st Sub List - 4th Element
	    },
	    // Second Sub-List
	    {
		 324550, // 3rd List - 2nd Sub List - 1st Element
		 15655,  // 3rd List - 2nd Sub List - 2nd Element
		 147576, // 3rd List - 2nd Sub List - 3rd Element
		 92885   // 3rd List - 2nd Sub List - 4th Element
	    }
	}
    };
	
    Console::WriteLine(L"Altair Realtors");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Properties Listing");
    Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-=");
    Console::WriteLine(L"Single Families");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Luxurious Estates");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,0,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,0,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,0,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,0,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Classy Properties");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,1,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,1,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,1,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[0,1,3]);
    Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    Console::WriteLine(L"Townhouses");
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"High Standing");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,0,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,0,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,0,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,0,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Common Envy");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,1,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,1,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,1,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[1,1,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Condominiums");
    Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    Console::WriteLine(L"Deluxe Condo");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,0,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,0,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,0,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,0,3]);
    Console::WriteLine(L"=---------------------------=");
    Console::WriteLine(L"Affordable Living");
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,1,0]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,1,1]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,1,2]);
    Console::WriteLine(L"Market Value: {0:C}", MarketValues[2,1,3]);
    Console::WriteLine(L"============================\n");

    return 0;
}

This would produce:

Altair Realtors
=---------------------------=
Properties Listing
=--=--=--=--=--=--=-=-=-=-=-=
Single Families
=---------------------------=
Luxurious Estates
Market Value: $2,250,550.00
Market Value: $1,215,080.00
Market Value: $1,625,550.00
Market Value: $850,450.00
=---------------------------=
Classy Properties
Market Value: $558,750.00
Market Value: $503,150.00
Market Value: $362,650.00
Market Value: $435,755.00
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Townhouses
=---------------------------=
High Standing
Market Value: $483,045.00
Market Value: $622,440.00
Market Value: $808,445.00
Market Value: $600,425.00
=---------------------------=
Common Envy
Market Value: $283,045.00
Market Value: $412,834.00
Market Value: $328,555.00
Market Value: $300,425.00
=---------------------------=
Condominiums
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Deluxe Condo
Market Value: $624,550.00
Market Value: $85,655.00
Market Value: $1,250,755.00
Market Value: $904,685.00
=---------------------------=
Affordable Living
Market Value: $324,550.00
Market Value: $15,655.00
Market Value: $147,576.00
Market Value: $92,885.00
============================

Press any key to continue . . .

In the same way, you can create an array or many lists, with each list containing sub-lists and each sub-list containing other sub-lists, etc. You may be limited only by your computer's memory.

 

Home Copyright © 2007-2013, FunctionX Home