Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Practicing Multi-Dimensional Arrays | Arrays
course content

Зміст курсу

C# Basics

Practicing Multi-Dimensional ArraysPracticing Multi-Dimensional Arrays

It is sometimes useful to declare constants for indexing multi-dimensional arrays. Following is the array from the last chapter's quiz:

cs

main.cs

We have created two constants FRUITS and VEGETABLES with values 0 and 1, representing the rows 0 for fruits, and 1 for vegetables in the foods array.

So if we need to access the "Cabbage" element from vegetables, we can simply write:

cs

main.cs

This trick might not be too helpful in smaller arrays like this one itself, however when dealing with more complex arrays, constants like these can help in easily accessing items from the array so that we don't need to memorize which row contains which items - in case we have categorized items in rows.

Now the challenge is to create a 2D array of type string and name productOptions, having a 4x3 shape (4 rows, 3 columns). Initialize it with the following data:

Row 1: "Red", "Green", "White" Row 2: "Square", "Rectangle", "Sphere" Row 3: "Thick", "Medium", "Thin" Row 4: "Printed", "Crafted", "None"

Access the "Crafted" element via indexing and output it. Use the relevant constant variable for that.

cs

main.cs

1. For declaring a multidimensional array we add commas inside the square brackets ([]) depending upon the dimensions of the array.
For-example, a 3D integer array would be declared like int[,,] arrayName = { ... };;
2. It is valid to use variable or constant values in indexing.
For-example if we have an array int[] exampleArray = { 1, 2, 3, 4, 5 }; and a constant const int someIndex = 2, it is valid to write exampleArray[someIndex] for accessing the element 3 from the Array.

using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            const int COLOR = 0;
            const int SHAPE = 1;
            const int WIDTH = 2;
            const int DESIGN = 3;
            // Create the array below
            string[,] productOptions = {
                { "Red", "Green", "White" },
                { "Square", "Rectangle", "Sphere" },
                { "Thick", "Medium", "Thin" },
                { "Printed", "Crafted", "None" }
            };
             // Reference the relevant element in the WriteLine statement
            Console.WriteLine(productOptions[DESIGN, 1]);
            Console.WriteLine(productOptions[COLOR, 2]);
            Console.WriteLine(productOptions[SHAPE, 0]);
            Console.WriteLine(productOptions[WIDTH, 1]);
        }
    }
} 
      

Все було зрозуміло?

Секція 5. Розділ 6
course content

Зміст курсу

C# Basics

Practicing Multi-Dimensional ArraysPracticing Multi-Dimensional Arrays

It is sometimes useful to declare constants for indexing multi-dimensional arrays. Following is the array from the last chapter's quiz:

cs

main.cs

We have created two constants FRUITS and VEGETABLES with values 0 and 1, representing the rows 0 for fruits, and 1 for vegetables in the foods array.

So if we need to access the "Cabbage" element from vegetables, we can simply write:

cs

main.cs

This trick might not be too helpful in smaller arrays like this one itself, however when dealing with more complex arrays, constants like these can help in easily accessing items from the array so that we don't need to memorize which row contains which items - in case we have categorized items in rows.

Now the challenge is to create a 2D array of type string and name productOptions, having a 4x3 shape (4 rows, 3 columns). Initialize it with the following data:

Row 1: "Red", "Green", "White" Row 2: "Square", "Rectangle", "Sphere" Row 3: "Thick", "Medium", "Thin" Row 4: "Printed", "Crafted", "None"

Access the "Crafted" element via indexing and output it. Use the relevant constant variable for that.

cs

main.cs

1. For declaring a multidimensional array we add commas inside the square brackets ([]) depending upon the dimensions of the array.
For-example, a 3D integer array would be declared like int[,,] arrayName = { ... };;
2. It is valid to use variable or constant values in indexing.
For-example if we have an array int[] exampleArray = { 1, 2, 3, 4, 5 }; and a constant const int someIndex = 2, it is valid to write exampleArray[someIndex] for accessing the element 3 from the Array.

using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            const int COLOR = 0;
            const int SHAPE = 1;
            const int WIDTH = 2;
            const int DESIGN = 3;
            // Create the array below
            string[,] productOptions = {
                { "Red", "Green", "White" },
                { "Square", "Rectangle", "Sphere" },
                { "Thick", "Medium", "Thin" },
                { "Printed", "Crafted", "None" }
            };
             // Reference the relevant element in the WriteLine statement
            Console.WriteLine(productOptions[DESIGN, 1]);
            Console.WriteLine(productOptions[COLOR, 2]);
            Console.WriteLine(productOptions[SHAPE, 0]);
            Console.WriteLine(productOptions[WIDTH, 1]);
        }
    }
} 
      

Все було зрозуміло?

Секція 5. Розділ 6
some-alt