Single and Two-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);
|
- To start a new program, launch Microsoft Visual C++ 2005
- On the main menu, click File -> New -> Project... (or File -> New Project...)
- On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
- In the Name box, replace the string with RealEstate13 and click OK
- To create a source file, on the main menu, click Project -> Add New Item...
- In the Templates list, click Source File (.cpp)
- In the New box, type Exercise and click Add
- In the empty file, type:
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; }
- To execute the application, on the main menu, click Debug -> Start
Without Debugging
======================================= 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 . . .
- Close the DOS window
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.
Introduction to Jagged Arrays
|
Introduction
|
So far, to declare a managed array, we entered its data type
inside the <> operator. In reality, as we will learn in the next lesson,
the section inside the <> operator is a placeholder for any valid data
type. Because an array is primarily treated as its own (user-defined) data type,
it can be used inside the <> operator. This means that you can use this
operator to create an array inside of an array. An array created inside of
another array is referred to as a jagged array.
Creating a Jagged Array
|
To create a jagged array, inside the <> operator, use
the array keyword, its own <> operator, a data type and the ^ operator to
create the new array. Here is an example:
using namespace System;
int main()
{
array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(4);
return 0;
}
This declaration indicates that you are creating an array
named SingleFamilies and that array contains 4 elements. Each element of that
array is itself an array of double-precision values. This declaration doesn't
indicate how many elements each jagged array will contain. This means that each
element of the SingleFamilies array can contain 1, 4, 100 or more elements. You
will provide this information when you initialize each element of the
SingleFamilies array.
Initializing a Jagged Array
|
As mentioned above, each element of the main array is itself
an array. Therefore, before using it, you must initialize it. Because each
element is declared as a handle, you must initialize it using the gcnew
operator. To do this, access an element using its 0-based index and allocate
memory for it using the gcnew operator. Also, because you are allocating
memory for an array, you must specify its dimension. If an element is a
one-dimensional array, specify its dimension as 1. This would be done as
follows:
using namespace System; int main() { array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(2); SingleFamilies[0] = gcnew array<double>(1); SingleFamilies[1] = gcnew array<double>(1); return 0; }
The above code only allocates memory for each element. It
initializes its element(s) to 0 if the array is numeric-based.
To access an element of a jagged array, you need at least
two pairs of square brackets on the right side of the name of the variable.
Inside the first square brackets, enter the index of the element from the main
array. Inside the second square brackets, enter the index of the particular
element you are trying to access. Here are examples:
using namespace System; int main() { array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(2); SingleFamilies[0] = gcnew array<double>(1); SingleFamilies[0][0] = 550550; SingleFamilies[1] = gcnew array<double>(1); SingleFamilies[1][0] =1215080; 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}", SingleFamilies[0][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[1][0]); 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 ============================ Press any key to continue . . .
Remember that each element of the main is its own but
undefined array. This means that you can initialize each any way you want. This
also means that one element of the main array can have a different number of
elements from the others. As mentioned already, it is when allocating memory for
an element of the jagged array that you decide what it would look like. Consider
the following example:
using namespace System; int main() { array<array<double> ^> ^ SingleFamilies = gcnew array<array<double> ^>(4); SingleFamilies[0] = gcnew array<double>(1); SingleFamilies[0][0] = 550550; SingleFamilies[1] = gcnew array<double>(1); SingleFamilies[1][0] = 1215080; SingleFamilies[2] = gcnew array<double>(3); SingleFamilies[2][0] = 722040; SingleFamilies[2][1] = 1450668; SingleFamilies[2][2] = 917297; SingleFamilies[3] = gcnew array<double>(2); SingleFamilies[3][0] = 325885; SingleFamilies[3][1] = 450735; Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Silver Spring"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[0][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[1][0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Potomac"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][1]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][2]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Condominiums in DC"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[3][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[3][1]); Console::WriteLine(L"============================\n"); return 0; }
In the above example, the first and the second elements of
the main array each is an array that contains only one element. The third
element of the main array contains 3 elements and the fourth contains 2
elements.
The above code would produce:
Altair Realtors =---------------------------= Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Silver Spring Market Value: $550,550.00 Market Value: $1,215,080.00 =---------------------------= Potomac Market Value: $722,040.00 Market Value: $1,450,668.00 Market Value: $917,297.00 =---------------------------= Condominiums in DC Market Value: $325,885.00 Market Value: $450,735.00 ============================ Press any key to continue . . .
Jagged Arrays and Functions
|
Returning a Jagged Array
|
As done for regularly managed arrays, you can create a
function that returns a jagged array. When creating the function, specify its
return type as a jagged array using the same formula of the array as we saw
above. The function can be declared as follows:
array<array<double> ^> ^ GetSingleFamilies();
At the time the function is declared, it only knows that it
would return a jagged array. The function doesn't know what the individual
internal arrays would look like. This means the array returned could be simple
or multi-dimensional. It is when you define the function that you will specify
the structures of the internal arrays of the jagged one.
When implementing the function, you can locally declare a
jagged array of the type that must be returned, then define or initialize the
array. Before exiting the function, make sure it returns the type of array it
was declared to return. Here is an example:
using namespace System; array<array<double> ^> ^ GetSingleFamilies(); int main() { array<array<double> ^> ^ SingleFamilies = GetSingleFamilies(); Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Silver Spring"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[0][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[1][0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Potomac"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][1]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[2][2]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Condominiums in DC"); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[3][0]); Console::WriteLine(L"Market Value: {0:C}", SingleFamilies[3][1]); Console::WriteLine(L"============================\n"); return 0; } array<array<double> ^> ^ GetSingleFamilies() { array<array<double> ^> ^ houses = gcnew array<array<double> ^>(4); houses[0] = gcnew array<double>(1); houses[0][0] = 550550; houses[1] = gcnew array<double>(1); houses[1][0] = 1215080; houses[2] = gcnew array<double>(3); houses[2][0] = 722040; houses[2][1] = 1450668; houses[2][2] = 917297; houses[3] = gcnew array<double>(2); houses[3][0] = 325885; houses[3][1] = 450735; return houses; }
The actual arrays inside the jagged array are defined only
in the function.
Passing a Jagged Array as Argument
|
You can pass a jagged array to a function as argument. To do
this, in the parentheses of the function, declare the jagged as you normally
would. If you are only declaring the function, you don't have to name the
argument. Here is an example:
void ShowSingleFamilies(array<array<double> ^> ^);
In the body of the function, use the array as you see fit.
For example, you can retrieve the values of its elements. Here is an example of
a function that takes a jagged array as argument:
using namespace System; array<array<double> ^> ^ GetSingleFamilies(); void ShowSingleFamilies(array<array<double> ^> ^); int main() { array<array<double> ^> ^ SingleFamilies = GetSingleFamilies(); ShowSingleFamilies(SingleFamilies); return 0; } array<array<double> ^> ^ GetSingleFamilies() { array<array<double> ^> ^ houses = gcnew array<array<double> ^>(4); houses[0] = gcnew array<double>(1); houses[0][0] = 550550; houses[1] = gcnew array<double>(1); houses[1][0] = 1215080; houses[2] = gcnew array<double>(3); houses[2][0] = 722040; houses[2][1] = 1450668; houses[2][2] = 917297; houses[3] = gcnew array<double>(2); houses[3][0] = 325885; houses[3][1] = 450735; return houses; } void ShowSingleFamilies(array<array<double> ^> ^ homes) { Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Silver Spring"); Console::WriteLine(L"Market Value: {0:C}", homes[0][0]); Console::WriteLine(L"Market Value: {0:C}", homes[1][0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Potomac"); Console::WriteLine(L"Market Value: {0:C}", homes[2][0]); Console::WriteLine(L"Market Value: {0:C}", homes[2][1]); Console::WriteLine(L"Market Value: {0:C}", homes[2][2]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Condominiums in DC"); Console::WriteLine(L"Market Value: {0:C}", homes[3][0]); Console::WriteLine(L"Market Value: {0:C}", homes[3][1]); Console::WriteLine(L"============================\n"); }
Once again, it is important to know that, when the function
that takes the jagged array is called, the caller doesn't specify, doesn't know
about, and doesn't impose on, the individual arrays that make up the jagged
array. Consider the following program:
using namespace System; array<array<double> ^> ^ GetSingleFamilies(); void ShowSingleFamilies(array<array<double> ^> ^); int main() { array<array<double> ^> ^ SingleFamilies = GetSingleFamilies(); ShowSingleFamilies(SingleFamilies); return 0; } array<array<double> ^> ^ GetSingleFamilies() { array<array<double> ^> ^ houses = gcnew array<array<double> ^>(4); houses[0] = gcnew array<double>(4); houses[0][0] = 550550; houses[0][1] = 937450; houses[0][2] = 1143855; houses[0][3] = 758060; houses[1] = gcnew array<double>(2); houses[1][0] = 1215080; houses[1][1] = 885560; houses[2] = gcnew array<double>(6); houses[2][0] = 722040; houses[2][1] = 1450668; houses[2][2] = 917297; houses[2][3] = 442255; houses[2][4] = 665775; houses[2][5] = 825675; houses[3] = gcnew array<double>(1); houses[3][0] = 325885; return houses; } void ShowSingleFamilies(array<array<double> ^> ^ homes) { Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Silver Spring"); Console::WriteLine(L"Market Value: {0:C}", homes[0][0]); Console::WriteLine(L"Market Value: {0:C}", homes[0][1]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Potomac"); Console::WriteLine(L"Market Value: {0:C}", homes[1][0]); Console::WriteLine(L"Market Value: {0:C}", homes[1][1]); Console::WriteLine(L"Market Value: {0:C}", homes[1][2]); Console::WriteLine(L"Market Value: {0:C}", homes[1][3]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Condominiums in DC"); Console::WriteLine(L"Market Value: {0:C}", homes[2][0]); Console::WriteLine(L"Market Value: {0:C}", homes[2][1]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Condominiums in Alexandria"); Console::WriteLine(L"Market Value: {0:C}", homes[3][0]); Console::WriteLine(L"Market Value: {0:C}", homes[3][1]); Console::WriteLine(L"Market Value: {0:C}", homes[3][2]); Console::WriteLine(L"============================\n"); }
This program compiles fine and this is how it works. When
the GetSingleFamilies() function is declared, the compiler doesn't know what
internal arrays the function will return. When this function is defined, the
programmer decides that it will return a jagged array made of 4 internal arrays,
2 internal arrays, 6 internal arrays, and 1 internal array. When the
ShowSingleFamilies() function is declared, the compiler doesn't know what the
internal arrays the argument holds. When this function is defined, the
programmer decides to access 2 elements from the first internal array
(regardless of how many elements that first array contains), 4 elements from the
second internal array (again, the programmer doesn't know how many elements the
second array contains), 2 elements from the 3rd internal array (without knowing
how many elements are in that array), and 3 elements from the 4th internal
array. The new programmer could continue accessing subsequent arrays because
there is no information that specifies the number of internal arrays in the
jagged array nor the number of elements that each array contains. And this
information is not know to the compiler either.
When the program runs, when the compiler tries to access
either an internal array that doesn't exist in the argument, or an element that
is not present in an internal array, it would throw an IndexOuOfRangeException
exception internal array. Again, the above program would compile fine but it
tries the execute all the functions, it would hang somewhere and produce the
following:
Altair Realtors =---------------------------= Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Silver Spring Market Value: $550,550.00 Market Value: $937,450.00 =---------------------------= Potomac Market Value: $1,215,080.00 Market Value: $885,560.00 Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at ShowSingleFamilies(Double[][] homes) in e:\programs\arrays1\arrays1\exercise.cpp:line 52 at main() in e:\programs\arrays1\arrays1\exercise.cpp:line 10 Press any key to continue . . .
The solution is that, when calling a function that takes a
jagged array, you must know about the internal arrays (how many there are) and
their elements (how many elements each internal array contains).
Multi-Dimensional Jagged Arrays
|
Two-Dimensional Jagged Arrays
|
A jagged array can be created as two-dimensional. to create
it, you can start with a normal single-dimensional array. Inside the <>
operator, create a two-dimensional array by specifying its data type and its
rank as 2. Here is an example:
array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3);
After allocating memory for the main array, you should do
the same (allocate memory) for each element of the jagged array. When doing
this, you must use the same formula you created the jagged array with inside the
<> operator. Then, when allocating memory for each element of the jagged
array, you can specify its ranks and how many elements it would have. This can
be done as follows:
using namespace System; int main() { array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3); // The first element of the main array is an array of // 2 lists and each list contains 3 elements MarketValues[0] = gcnew array<double, 2>(2, 3); // The second element of the main array is an array of // 4 lists and each list contains 1 element MarketValues[1] = gcnew array<double, 2>(4, 1); // The third element of the main array is an array of // 1 list and that list contains 5 elements MarketValues[2] = gcnew array<double, 2>(1, 5); return 0; }
To access an element of a jagged array, you use at least two
pairs of square brackets. In the fist pair, specify the rank of the current
element from the main array:
using namespace System; int main() { array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3); // The first element of the main array is an array of // 2 lists and each list contains 3 elements MarketValues[0] = gcnew array<double, 2>(2, 3); MarketValues[0][] // The second element of the main array is an array of // 4 lists and each list contains 1 element MarketValues[1] = gcnew array<double, 2>(4, 1); MarketValues[1][] return 0; }
In the second square brackets, you apply the formula we used
to access an element of a multi-dimensional array. Then you do what you want,
such as assigning the desired value or retrieving the value of the element. Here
are examples:
using namespace System; int main() { array<array<double, 2> ^> ^ MarketValues = gcnew array<array<double, 2> ^>(3); // The first element of the main array is an array of // 2 lists and each list contains 3 elements MarketValues[0] = gcnew array<double, 2>(2, 3); MarketValues[0][0,0] = 1150760; MarketValues[0][0,1] = 665265; MarketValues[0][0,2] = 888500; MarketValues[0][1,0] = 450550; MarketValues[0][1,1] = 565265; MarketValues[0][1,2] = 475640; // The second element of the main array is an array of // 4 lists and each list contains 1 element MarketValues[1] = gcnew array<double, 2>(4, 1); MarketValues[1][0,0] = 250550; MarketValues[1][1,0] = 465265; MarketValues[1][2,0] = 1215080; MarketValues[1][3,0] = 388500; // The third element of the main array is an array of // 1 list and that list contains 5 elements MarketValues[2] = gcnew array<double, 2>(1, 5); MarketValues[2][0,0] = 725660; MarketValues[2][0,1] = 635605; MarketValues[2][0,2] = 285555; MarketValues[2][0,3] = 447650; MarketValues[2][0,4] = 277800; Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Available Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Alexandria, VA"); 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][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"=---------------------------="); Console::WriteLine(L"Washington, DC"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Chevy Chase, MD"); 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"Market Value: {0:C}", MarketValues[2][0,4]); Console::WriteLine(L"============================\n"); return 0; }
This would produce:
Altair Realtors =---------------------------= Available Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Alexandria, VA Market Value: $1,150,760.00 Market Value: $665,265.00 Market Value: $888,500.00 Market Value: $450,550.00 Market Value: $565,265.00 Market Value: $475,640.00 =---------------------------= Washington, DC Market Value: $250,550.00 Market Value: $465,265.00 Market Value: $1,215,080.00 Market Value: $388,500.00 =---------------------------= Chevy Chase, MD Market Value: $725,660.00 Market Value: $635,605.00 Market Value: $285,555.00 Market Value: $447,650.00 Market Value: $277,800.00 ============================ Press any key to continue . . .
Multi-Dimensional Jagged Arrays
|
The above array was two-dimensional. A jagged array can also
be created as multi-dimensional. From the formula of the two-dimensional array,
you simply change the rank from 2 to the desired rank. Here is an example:
using namespace System;
int main()
{
array<array<double, 3> ^> ^ MarketValues = gcnew array<array<double, 3> ^>(3);
return 0;
}
To allocate memory for each jagged array, you must first use
the same rank you specified when declaring the variable. Then, you must specify
the rank and dimension that each particular jagged array will use. Here are
examples:
using namespace System; int main() { array<array<double, 3> ^> ^ MarketValues = gcnew array<array<double, 3> ^>(3); // The first element of the main array is an array of // 2 lists. Each list will contain 3 sub-lists // Each sub-list will contain 1 element MarketValues[0] = gcnew array<double, 3>(2, 3, 1); // The second element of the main array is an array of // 4 lists. Each list will contain 1 sub-list // The sub-list will contain 2 elements MarketValues[1] = gcnew array<double, 3>(4, 1, 2); // The third element of the main array is an array of // 1 list. The list will contain 2 sub-lists // Each sub-list will contain 5 elements MarketValues[2] = gcnew array<double, 3>(1, 2, 5); return 0; }
To access an element of the jagged array, you use two pairs
of square brackets. In the fist pair, specify the rank of an element from the
main array. In the second square brackets, enter the index of the first
dimension of the jagged array, followed by a comma, followed by the appropriate
index of the second dimension, followed by a comma, and followed by an index
corresponding to the last dimension. You can then you do what you want,
including assigning the desired value or retrieving the value of the element.
Here are examples:
using namespace System; int main() { array<array<double, 3> ^> ^ MarketValues = gcnew array<array<double, 3> ^>(3); // The first element of the main array is an array of // 2 lists. Each list contains 3 sub-lists // Each sub-list contains 1 element MarketValues[0] = gcnew array<double, 3>(2, 3, 1); MarketValues[0][0,0,0] = 1150760; MarketValues[0][0,1,0] = 665265; MarketValues[0][0,2,0] = 888500; MarketValues[0][1,0,0] = 450550; MarketValues[0][1,1,0] = 565265; MarketValues[0][1,2,0] = 475640; // The second element of the main array is an array of // 4 lists. Each list contains 1 sub-list // The sub-list contains 2 elements MarketValues[1] = gcnew array<double, 3>(4, 1, 2); MarketValues[1][0,0,0] = 250550; MarketValues[1][0,0,1] = 465265; MarketValues[1][1,0,0] = 1215080; MarketValues[1][1,0,1] = 388500; MarketValues[1][2,0,0] = 255550; MarketValues[1][2,0,1] = 464235; MarketValues[1][3,0,0] = 2365770; MarketValues[1][3,0,1] = 730458; // The third element of the main array is an array of // 1 list. The list contains 2 sub-lists // Each sub-list contains 5 elements MarketValues[2] = gcnew array<double, 3>(1, 2, 5); MarketValues[2][0,0,0] = 725660; MarketValues[2][0,1,1] = 635605; MarketValues[2][0,0,2] = 285555; MarketValues[2][0,1,3] = 447650; MarketValues[2][0,0,4] = 277800; MarketValues[2][0,1,0] = 7345665; MarketValues[2][0,0,1] = 5555565; MarketValues[2][0,1,2] = 2225055; MarketValues[2][0,0,3] = 645825; MarketValues[2][0,1,4] = 707650; Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Available Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Virginia"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,2,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,2,0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Washington, DC"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0,1]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Maryland"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,3]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,4]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,3]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,4]); Console::WriteLine(L"============================\n"); return 0; }
This would produce:
Altair Realtors =---------------------------= Available Properties Listing =--=--=--=--=--=--=-=-=-=-=-= Virginia Market Value: $1,150,760.00 Market Value: $665,265.00 Market Value: $888,500.00 Market Value: $450,550.00 Market Value: $565,265.00 Market Value: $475,640.00 =---------------------------= Washington, DC Market Value: $250,550.00 Market Value: $465,265.00 Market Value: $1,215,080.00 Market Value: $388,500.00 Market Value: $255,550.00 Market Value: $464,235.00 Market Value: $2,365,770.00 Market Value: $730,458.00 =---------------------------= Maryland Market Value: $725,660.00 Market Value: $635,605.00 Market Value: $285,555.00 Market Value: $447,650.00 Market Value: $277,800.00 Market Value: $7,345,665.00 Market Value: $5,555,565.00 Market Value: $2,225,055.00 Market Value: $645,825.00 Market Value: $707,650.00 ============================ Press any key to continue . . .
As described for single-dimensional jagged arrays,
you can create a function that returns a multi-dimensional jagged array.
You can also create a function that takes a multi-dimensional jagged
array as argument. Here are examples:
using namespace System; array<array<double, 3> ^> ^ GetMarketValues() { array<array<double, 3> ^> ^ MarketValues = gcnew array<array<double, 3> ^>(3); // The first element of the main array is an array of // 2 lists. Each list contains 3 sub-lists // Each sub-list contains 1 element MarketValues[0] = gcnew array<double, 3>(2, 3, 1); MarketValues[0][0,0,0] = 1150760; MarketValues[0][0,1,0] = 665265; MarketValues[0][0,2,0] = 888500; MarketValues[0][1,0,0] = 450550; MarketValues[0][1,1,0] = 565265; MarketValues[0][1,2,0] = 475640; // The second element of the main array is an array of // 4 lists. Each list contains 1 sub-list // The sub-list contains 2 elements MarketValues[1] = gcnew array<double, 3>(4, 1, 2); MarketValues[1][0,0,0] = 250550; MarketValues[1][0,0,1] = 465265; MarketValues[1][1,0,0] = 1215080; MarketValues[1][1,0,1] = 388500; MarketValues[1][2,0,0] = 255550; MarketValues[1][2,0,1] = 464235; MarketValues[1][3,0,0] = 2365770; MarketValues[1][3,0,1] = 730458; // The third element of the main array is an array of // 1 list. The list contains 2 sub-lists // Each sub-list contains 5 elements MarketValues[2] = gcnew array<double, 3>(1, 2, 5); MarketValues[2][0,0,0] = 725660; MarketValues[2][0,1,1] = 635605; MarketValues[2][0,0,2] = 285555; MarketValues[2][0,1,3] = 447650; MarketValues[2][0,0,4] = 277800; MarketValues[2][0,1,0] = 7345665; MarketValues[2][0,0,1] = 5555565; MarketValues[2][0,1,2] = 2225055; MarketValues[2][0,0,3] = 645825; MarketValues[2][0,1,4] = 707650; return MarketValues; } void ShowMarketValues(array<array<double, 3> ^> ^ MarketValues) { Console::WriteLine(L"Altair Realtors"); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Available Properties Listing"); Console::WriteLine(L"=--=--=--=--=--=--=-=-=-=-=-="); Console::WriteLine(L"Virginia"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][0,2,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[0][1,2,0]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Washington, DC"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][0,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][1,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][2,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[1][3,0,1]); Console::WriteLine(L"=---------------------------="); Console::WriteLine(L"Maryland"); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,3]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,4]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,0]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,1]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,2]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,0,3]); Console::WriteLine(L"Market Value: {0:C}", MarketValues[2][0,1,4]); Console::WriteLine(L"============================\n"); } int main() { array<array<double, 3> ^> ^ MarketValues = GetMarketValues(); ShowMarketValues(MarketValues); return 0; }
No comments:
Post a Comment