Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookDeclaring and Using 2D Arrays

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 1
some-alt