Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Declaring and Using 2D Arrays | Working with 2D Arrays
Quizzes & Challenges
Quizzes
Challenges
/
C# Arrays

bookDeclaring and Using 2D Arrays

Two-dimensional arrays, or 2D arrays, allow you to store data in a grid-like structure with rows and columns. You can think of a 2D array as a table, where each cell holds a value. This structure is useful for representing matrices, game boards, seating charts, or any data that naturally fits into rows and columns.

Note
Definition

A 2D array is an array of arrays, often visualized as a table or matrix. Each element is identified by two indices: one for the row and one for the column.

Program.cs

Program.cs

copy
12345678910111213141516171819202122
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Declare and initialize a 2D integer array (3 rows, 4 columns) int[,] matrix = new int[3, 4] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Print the element at the first row and first column Console.WriteLine("Element at (0, 0): " + matrix[0, 0]); } } }

In a 2D array, elements are accessed using two indices: one for the row and one for the column. In the code above, matrix[0, 0] accesses the element in the first row and first column. The first index always represents the row, and the second represents the column. Indexing starts at zero, so the first row is index 0, the second row is index 1, and so on. The same applies to columns.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[,] matrix = new int[3, 4] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Print all elements using nested loops for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { Console.Write(matrix[row, col] + " "); } Console.WriteLine(); } } } }

This code above creates a two-dimensional array (a 3×4 matrix) and prints all its elements using nested loops. The outer loop iterates through each row, while the inner loop goes through each column of that row. Each value is printed in order, producing a structured matrix layout in the console.

1. How do you access the element in the second row and third column of a 2D array?

2. What is the difference between a 1D and a 2D array?

3. Why are nested loops used with 2D arrays?

question mark

How do you access the element in the second row and third column of a 2D array?

Select the correct answer

question mark

What is the difference between a 1D and a 2D array?

Select the correct answer

question mark

Why are nested loops used with 2D arrays?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain how to create a 2D array in a specific programming language?

How do I access or modify a specific element in a 2D array?

What are some common use cases for 2D arrays?

bookDeclaring and Using 2D Arrays

Scorri per mostrare il menu

Two-dimensional arrays, or 2D arrays, allow you to store data in a grid-like structure with rows and columns. You can think of a 2D array as a table, where each cell holds a value. This structure is useful for representing matrices, game boards, seating charts, or any data that naturally fits into rows and columns.

Note
Definition

A 2D array is an array of arrays, often visualized as a table or matrix. Each element is identified by two indices: one for the row and one for the column.

Program.cs

Program.cs

copy
12345678910111213141516171819202122
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Declare and initialize a 2D integer array (3 rows, 4 columns) int[,] matrix = new int[3, 4] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Print the element at the first row and first column Console.WriteLine("Element at (0, 0): " + matrix[0, 0]); } } }

In a 2D array, elements are accessed using two indices: one for the row and one for the column. In the code above, matrix[0, 0] accesses the element in the first row and first column. The first index always represents the row, and the second represents the column. Indexing starts at zero, so the first row is index 0, the second row is index 1, and so on. The same applies to columns.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[,] matrix = new int[3, 4] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Print all elements using nested loops for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { Console.Write(matrix[row, col] + " "); } Console.WriteLine(); } } } }

This code above creates a two-dimensional array (a 3×4 matrix) and prints all its elements using nested loops. The outer loop iterates through each row, while the inner loop goes through each column of that row. Each value is printed in order, producing a structured matrix layout in the console.

1. How do you access the element in the second row and third column of a 2D array?

2. What is the difference between a 1D and a 2D array?

3. Why are nested loops used with 2D arrays?

question mark

How do you access the element in the second row and third column of a 2D array?

Select the correct answer

question mark

What is the difference between a 1D and a 2D array?

Select the correct answer

question mark

Why are nested loops used with 2D arrays?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 1
some-alt