CSharp-Advanced-Multidimensional-Arrays
CSharp-Advanced-Multidimensional-Arrays
SoftUni Team
Technical Trainers
Software University
https://about.softuni.bg/
Table of Contents
1. Multidimensional Arrays
Creating Matrices and Multidimensional Arrays
Accessing Their Elements
Reading and Printing
2. Jagged Arrays (Arrays of Arrays)
Creating a Jagger Array
Accessing Their Elements
Reading and Printing
Have a Question?
sli.do
#csharp-advanced
3
Multidimensional Arrays
Definition and Usage
What is a Multidimensional Array?
Array is a systematic arrangement of similar objects
Multidimensional arrays have more than one dimension
The most used multidimensional arrays are
the 2-dimensional, also called matrices
COLS
6
Initializing Multidimensional Arrays
Initializing with values:
int[,] matrix = {
{1, 2, 3, 4}, // row 0 values
{5, 6, 7, 8} // row 1 values
};
int[,] matrix =
{ { 5, 2, 3, 1 },
{ 1, 9, 2, 4 },
{ 9, 8, 6, 11 } };
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write("{0} ", matrix[row, col]);
}
Console.WriteLine();
}
9
Printing Matrix – Example (2)
Foreach iterates through all the elements in the matrix
int[,] matrix = {
{ 5, 2, 3, 1 },
{ 1, 9, 2, 4 },
{ 9, 8, 6, 9 }
};
10
Problem: Sum Matrix Elements
Read a matrix from the console
Print the number of rows
Print the number of columns
Print the sum of all numbers in the matrix
3, 6 3, 4
7, 1, 3, 3, 2, 1 3 3
1, 2, 3, 1
1, 3, 9, 8, 5, 6 6 4
1, 2, 2, 4
4, 6, 7, 9, 1, 0 76 24
2, 2, 2, 2
12
Solution: Sum Matrix Elements (2)
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(matrix.GetLength(0));
Console.WriteLine(matrix.GetLength(1));
Console.WriteLine(sum);
13
Problem: Sum Matrix Columns
Read matrix sizes
Read a matrix from the console
Print the sum of all numbers in matrix columns
12
3, 6 10 3, 3
12
7 1 3 3 2 1 19 1 2 3
15
1 3 9 8 5 6 20 4 5 6
18
4 6 7 9 1 0 8 7 8 9
7
15
Solution: Sum Matrix Columns (2)
16
Problem: Square with Maximum Sum
Find 2x2 square with max sum in given matrix
Read matrix from the console
Find biggest sum of 2x2 submatrix
Print the result as a new matrix, followed by the sum
3, 6
9 8
7, 1, 3, 3, 2, 1
7 9
1, 3, 9, 8, 5, 6
33
4, 6, 7, 9, 1, 0
3
1 2 3
4 5 6 7 Invalid coordinates 0 1 2
8 9 10 6 2 3 row 0 1 2 3
Add 0 0 5 4 5 4 7 row 1 4 5 6 7
Subtract 1 2 2 8 9 10 row 2 8 9 10
Subtract 1 4 7
END
25
Solution: Jagged-Array Modification (1)
26
Solution: Jagged-Array Modification (2)
string line;
while ((line = Console.ReadLine()) != "END") {
string[] tokens = line.Split();
string command = tokens[0];
int row = int.Parse(tokens[1]); Check the row
int col = int.Parse(tokens[2]); and col ranges
int value = int.Parse(tokens[3]);
if (row < 0 || row >= matrix.Length || … )
Console.WriteLine("Invalid coordinates");
else
{ // TODO: Execute the command }
}
// TODO: Print the matrix
27
Problem: Pascal Triangle
Write a program to prints on the console the Pascal's Triangle
1
1 1 1
1 2 1 1 1 1
6 4 2
1 3 3 1 1 2 1 1 1
1 4 6 4 1 1 3 3 1
1 5 10 10 5 1
29
Solution: Pascal Triangle (2)
if (currentRow.Length > 2)
{
for (int i = 1; i < currentRow.Length - 1; i++)
{
long[] previousRow = triangle[row - 1];
long prevoiousRowSum = previousRow[i] + previousRow[i - 1];
currentRow[i] = prevoiousRowSum;
}
}
// TODO: Print triangle
foreach (long[] row in triangle)
Console.WriteLine(string.Join(" ", row));
30
Summary
Multidimensional
… arrays
…Have more than one dimension
…Two-dimensional arrays are like tables
with rows and columns
Jagged arrays
Arrays of arrays
Each element is an array itself
31
Questions?
© SoftUni – https://about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
SoftUni Diamond Partners
Educational Partners
34
Trainings @ Software University (SoftUni)
Software University – High-Quality Education,
Profession and Job for Software Developers
softuni.bg, about.softuni.bg
Software University Foundation
softuni.foundation
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University Forums
forum.softuni.bg
35
License
36