Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Matrix Operations: Sum and Transpose | Working with 2D Arrays
Quizzes & Challenges
Quizzes
Challenges
/
C# Arrays

bookMatrix Operations: Sum and Transpose

Matrix operations are essential in many programming tasks, especially when working with data that fits naturally into a grid-like structure, such as images, spreadsheets, or game boards. Two fundamental operations you will often perform on matrices (2D arrays) are calculating the sum of all their elements and transposing them. Summing all elements is useful for data analysis, while transposing a matrix—swapping its rows and columns—is crucial in mathematical computations, graphics, and algorithms that require a different orientation of the data.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[,] matrix = { {1, 2, 3}, {4, 5, 6} }; int sum = 0; for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { sum += matrix[row, col]; } } Console.WriteLine("Sum of all elements: " + sum); } } }

To sum all elements in a 2D array, you use nested loops: the outer loop iterates over each row, and the inner loop iterates over each column within that row. For every element, you add its value to a running total. In the example above, the program calculates the sum of all elements in a 2x3 matrix by accessing each value with matrix[row, col] and accumulating them in the sum variable.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[,] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int size = matrix.GetLength(0); int[,] transposed = new int[size, size]; for (int row = 0; row < size; row++) { for (int col = 0; col < size; col++) { transposed[col, row] = matrix[row, col]; } } Console.WriteLine("Transposed matrix:"); for (int row = 0; row < size; row++) { for (int col = 0; col < size; col++) { Console.Write(transposed[row, col] + " "); } Console.WriteLine(); } } } }

This code performs a matrix transpose. It starts with a 3×3 matrix and creates a new matrix where rows and columns are swapped. Using nested loops, each element at position (row, col) is reassigned to (col, row) in the transposed matrix. The program then prints the resulting matrix, showing how the original rows become columns.

Note
Note

Transposing a matrix means turning its rows into columns and vice versa.

1. What is the result of transposing a 2x3 matrix?

2. How do you sum all elements in a 2D array?

3. Why is transposing useful in matrix operations?

question mark

What is the result of transposing a 2x3 matrix?

Select the correct answer

question mark

How do you sum all elements in a 2D array?

Select the correct answer

question mark

Why is transposing useful in matrix operations?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain how to implement these matrix operations in a specific programming language?

What are some real-world applications of matrix transposition?

Can you provide an example of summing elements in a larger matrix?

bookMatrix Operations: Sum and Transpose

Deslize para mostrar o menu

Matrix operations are essential in many programming tasks, especially when working with data that fits naturally into a grid-like structure, such as images, spreadsheets, or game boards. Two fundamental operations you will often perform on matrices (2D arrays) are calculating the sum of all their elements and transposing them. Summing all elements is useful for data analysis, while transposing a matrix—swapping its rows and columns—is crucial in mathematical computations, graphics, and algorithms that require a different orientation of the data.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[,] matrix = { {1, 2, 3}, {4, 5, 6} }; int sum = 0; for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { sum += matrix[row, col]; } } Console.WriteLine("Sum of all elements: " + sum); } } }

To sum all elements in a 2D array, you use nested loops: the outer loop iterates over each row, and the inner loop iterates over each column within that row. For every element, you add its value to a running total. In the example above, the program calculates the sum of all elements in a 2x3 matrix by accessing each value with matrix[row, col] and accumulating them in the sum variable.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[,] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int size = matrix.GetLength(0); int[,] transposed = new int[size, size]; for (int row = 0; row < size; row++) { for (int col = 0; col < size; col++) { transposed[col, row] = matrix[row, col]; } } Console.WriteLine("Transposed matrix:"); for (int row = 0; row < size; row++) { for (int col = 0; col < size; col++) { Console.Write(transposed[row, col] + " "); } Console.WriteLine(); } } } }

This code performs a matrix transpose. It starts with a 3×3 matrix and creates a new matrix where rows and columns are swapped. Using nested loops, each element at position (row, col) is reassigned to (col, row) in the transposed matrix. The program then prints the resulting matrix, showing how the original rows become columns.

Note
Note

Transposing a matrix means turning its rows into columns and vice versa.

1. What is the result of transposing a 2x3 matrix?

2. How do you sum all elements in a 2D array?

3. Why is transposing useful in matrix operations?

question mark

What is the result of transposing a 2x3 matrix?

Select the correct answer

question mark

How do you sum all elements in a 2D array?

Select the correct answer

question mark

Why is transposing useful in matrix operations?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
some-alt