Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

25.4.4Practical Learning: Passing an Array as Argument

  1. To pass an array as argument to a function, change the program as follows:  

    using namespace System;

    array<int ^> ^ GetBedrooms();

    array<Byte> ^ GetStories();

    void GetBathrooms(array<float ^> ^);

    int main()

    {

    array<__wchar_t> ^ HouseTypes = gcnew array<__wchar_t>(4)

    { L'S', L'C', L'S', L'T' };

    array<int ^> ^ Bedrooms = GetBedrooms();

    array<Byte> ^ Stories = GetStories();

    array<float ^> ^ Bathrooms = gcnew array<float ^>(4);

    GetBathrooms(Bathrooms);

    array<bool> ^ HasGarage = gcnew array<bool>(4)

    { true, true, false, false };

    array<double ^> ^ Values = gcnew array<double ^>(4)

    {

    gcnew double(550500.00),

    gcnew double(115000.00),

    gcnew double(425000.00),

    gcnew double(350000.00)

    };

    Console::WriteLine(L"House Catalog");

    Console::WriteLine(L"===================================================");

    Console::WriteLine(L"Type Bedrooms Stories Bathrooms Garage Value");

    Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

    for(int house = 0; house < 4; house++)

    {

    Console::WriteLine(L" {0}\t{1}\t{2}\t{3}\t{4}\t{5:F}",

    HouseTypes[house], Bedrooms[house],

    Stories[house], Bathrooms[house],

    HasGarage[house], Values[house]);

    Console::WriteLine(L"---------------------------------------------------");

    }

    Console::WriteLine();

    return 0;

    }

    array<int ^> ^ GetBedrooms()

    {

    array<int ^> ^ beds = gcnew array<int ^>(4);

    beds[0] = 5;

    beds[1] = 2;

    beds[2] = 3;

    beds[3] = 1;

    return beds;

    }

    array<Byte> ^ GetStories()

    {

    array<Byte > ^ Stories = gcnew array<Byte>(4);

    Stories[0] = 4;

    Stories[1] = 2;

    Stories[2] = 3;

    Stories[3] = 1;

    return Stories;

    }

    void GetBathrooms(array<float ^> ^ baths)

    {

    baths[0] = 2.5F;

    baths[1] = 1.0F;

    baths[2] = 1.5F;

    baths[3] = 3.0F;

    }

  2. Execute the application to test it

  3. Close the DOS window and return to your programming environment

26. Multi-Dimensional Arrays

26.1 Single and Two-Dimensional Arrays

26.1.1 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);