Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Desafío: Matrices Multidimensionales | Arreglos
Quizzes & Challenges
Quizzes
Challenges
/
Fundamentos de C#

bookDesafío: Matrices Multidimensionales

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

main.cs

main.cs

copy
1234567
const int FRUITS = 0; const int VEGETABLES = 1; string[,] foods = { { "Apple", "Apricot", "Banana", "Grapes", "Lime" }, // fruits { "Tomato", "Cabbage", "Carrot", "Pumpkin", "Broccoli" }, // vegetables };

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:

main.cs

main.cs

copy
1
foods[VEGETABLES][1];

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.

main.cs

main.cs

copy
123456789101112131415161718192021222324
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 _______ = { _______ }; // 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]); } } }
  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.
main.cs

main.cs

copy
123456789101112131415161718192021222324252627
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]); } } }

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 6

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you show me how to declare the constants for each row in the array?

What would the code look like to access the "Crafted" element using a constant?

Can you explain why using constants for indexing is helpful in larger arrays?

bookDesafío: Matrices Multidimensionales

Desliza para mostrar el menú

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

main.cs

main.cs

copy
1234567
const int FRUITS = 0; const int VEGETABLES = 1; string[,] foods = { { "Apple", "Apricot", "Banana", "Grapes", "Lime" }, // fruits { "Tomato", "Cabbage", "Carrot", "Pumpkin", "Broccoli" }, // vegetables };

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:

main.cs

main.cs

copy
1
foods[VEGETABLES][1];

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.

main.cs

main.cs

copy
123456789101112131415161718192021222324
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 _______ = { _______ }; // 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]); } } }
  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.
main.cs

main.cs

copy
123456789101112131415161718192021222324252627
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]); } } }

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 6
some-alt