Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.

Slides:



Advertisements
Similar presentations
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Advertisements

C# Advanced Topics Methods, Classes and Objects SoftUni Team Technical Trainers Software University
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Methods Writing and using methods, overloads, ref, out SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
Teamwork and Personal Skills Course Introduction Software University SoftUni Team Technical Trainers.
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
Conditional Statements Implementing Control-Flow Logic in C# SoftUni Team Technical Trainers Software University
Redis Key-Value Database: Practical Introduction
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
Start Your Own Blog Angel Georgiev Part-time Trainer angeru.softuni-friends.org Software University The Culture of Knowledge Sharing.
Processing Sequences of Elements Svetlin Nakov Telerik Corporation
Entity Framework Performance SoftUni Team Technical Trainers Software University
Methods, Arrays, Lists, Dictionaries, Strings, Classes and Objects
Svetlin Nakov Technical Trainer Software University
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Java Collections Basics Arrays, Lists, Strings, Sets, Maps Svetlin Nakov Technical Trainer Software University
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
Arrays, Lists, Stacks, Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Very Basic Mathematical Concepts for Programmers
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Web Development Tools Tools for Front-End Developers Writing HTML and CSS Code SoftUni Team Technical Trainers Software University
Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Tables, Rows, Columns, Cells, Header, Footer, Colspan, Rowspan
Java Collections Basics Arrays, Lists, Strings, Sets, Maps Bogomil Dimitrov Technical Trainer Software University
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
Advanced C# Course Introduction SoftUni Team Technical Trainers Software University
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
C# Advanced Topics Methods, Arrays, Lists, Dictionaries, Strings, Classes and Objects SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Programming for Beginners Course Introduction SoftUni Team Technical Trainers Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
Dictionaries, Hash Tables and Sets Dictionaries, Hash Tables, Hashing, Collisions, Sets SoftUni Team Technical Trainers Software University
Lists and Matrices Lists: Variable-Size Arrays Matrices: Arrays of Arrays (Tables) SoftUni Team Technical Trainers Software University
SoftUni Team Technical Trainers Software University Trees and Tree-Like Structures Trees, Tree-Like Structures, Binary Search Trees,
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Advanced Tree Structures Binary Trees, AVL Tree, Red-Black Tree, B-Trees, Heaps SoftUni Team Technical Trainers Software University
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Programming Fundamentals Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Sets, Hash table, Dictionaries
C# Basic Syntax, Visual Studio, Console Input / Output
C# Basic Syntax, Visual Studio, Console Input / Output
Multi-Dictionaries, Nested Dictionaries, Sets
Repeating Code Multiple Times
Array and List Algorithms
Processing Variable-Length Sequences of Elements
Multidimensional Arrays
Multidimensional Arrays, Sets, Dictionaries
Multidimensional Arrays
Presentation transcript:

Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University

Table of Contents 1.Matrices and Multidimensional Arrays 2.Jagged Arrays (arrays of arrays) 3.Sorting Arrays 4.Dictionaries – Dictionary 5.Sets – HashSet, SortedSet 2

Multidimensional Arrays Using Array of Arrays, Matrices and Cubes

4  Multidimensional arrays have more than one dimension  The most used multidimensional arrays are the 2-dimensional  Known as matrices or tables What is Multidimensional Array? One main array whose elements are arrays

Declaring and Creating Multidimensional Arrays  Declaring multidimensional arrays:  Creating a multidimensional array  Use new keyword  Must specify the size of each dimension int[,] intMatrix; float[,] floatMatrix; string[,,] strCube; int[,] intMatrix = new int[3, 4]; float[,] floatMatrix = new float[8, 2]; string[,,] stringCube = new string[5, 5, 5]; 5

Initializing Multidimensional Arrays  Initializing with values multidimensional array:  Matrices are represented by a list of rows  Rows consist of list of values  The first dimension comes first, the second comes next (inside the first) int[,] matrix = { {1, 2, 3, 4}, // row 0 values {1, 2, 3, 4}, // row 0 values {5, 6, 7, 8} // row 1 values {5, 6, 7, 8} // row 1 values}; 6

Accessing Elements  Accessing N-dimensional array element:  Getting element value example:  Setting element value example: nDimensionalArray[index 1, …, index n ] int[,] array = {{1, 2}, {3, 4}} int element11 = array[1, 1]; // element11 = 4 int[,] array = new int[3, 4]; for (int row = 0; row < array.GetLength(0); row++) for (int col = 0; col < array.GetLength(1); col++) for (int col = 0; col < array.GetLength(1); col++) array[row, col] = row + col; array[row, col] = row + col; 7

Reading a Matrix – Example int rows = int.Parse(Console.ReadLine()); int columns = int.Parse(Console.ReadLine()); int[,] matrix = new int[rows, columns]; for (int row = 0; row < rows; row++) { for (int column = 0; column < cols; column++) for (int column = 0; column < cols; column++) { Console.Write("matrix[{0},{1}] = ", row, column); Console.Write("matrix[{0},{1}] = ", row, column); string inputNumber = Console.ReadLine(); string inputNumber = Console.ReadLine(); matrix[row, column] = int.Parse(inputNumber); matrix[row, column] = int.Parse(inputNumber); }} 8

Printing Matrix – Example int[,] matrix = { { 5, 2, 3, 1 }, { 5, 2, 3, 1 }, { 1, 9, 2, 4 }, { 1, 9, 2, 4 }, { 9, 8, 6, 11 } { 9, 8, 6, 11 }}; for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) for (int col = 0; col < matrix.GetLength(1); col++) { Console.Write("{0} ", matrix[row, col]); Console.Write("{0} ", matrix[row, col]); } Console.WriteLine(); Console.WriteLine();} 9 Gets length of 0 th dimension (rows) Gets length of 1 st dimension (columns)

Reading and Printing Matrices Live Demo

Maximal Platform – Example  Finding maximal sum of 2x2 platform int[,] matrix = { {7, 1, 3, 3, 2, 1}, {7, 1, 3, 3, 2, 1}, {1, 3, 9, 8, 5, 6}, {1, 3, 9, 8, 5, 6}, {4, 6, 7, 9, 1, 0} }; {4, 6, 7, 9, 1, 0} }; int bestSum = int.MinValue; for (int row = 0; row < matrix.GetLength(0) - 1; row++) for (int col = 0; col < matrix.GetLength(1) - 1; col++) for (int col = 0; col < matrix.GetLength(1) - 1; col++) { int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row + 1, col] + matrix[row + 1, col + 1]; int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row + 1, col] + matrix[row + 1, col + 1]; if (sum > bestSum) if (sum > bestSum) bestSum = sum; bestSum = sum; } 11

Maximal Platform Live Demo

Matrix Multiplication Live Demo

Exercises in Class

 Write a program that generates a snake-like NxM matrix: Snake Matrix x 3 =>

Jagged Arrays What are Jagged Arrays and How to Use Them

Jagged Arrays  Jagged arrays are multidimensional arrays  But each dimension has different size  A jagged array is an array of arrays  Each of the arrays has different length int[][] jagged = new int[3][]; jagged[0] = new int[3]; jagged[1] = new int[2]; jagged[2] = new int[5];

18 int[][] jagged = new int[5][]; for (int i = 0; i < jagged.GetLength(0); i++) { string[] inputNumbers = Console.ReadLine().Split(' '); string[] inputNumbers = Console.ReadLine().Split(' '); jagged[i] = new int[inputNumbers.Length]; jagged[i] = new int[inputNumbers.Length]; for (int j = 0; j < jagged.GetLength(1); j++) for (int j = 0; j < jagged.GetLength(1); j++) { jagged[i][j] = int.Parse(inputNumbers[j]); jagged[i][j] = int.Parse(inputNumbers[j]); }} Filling a Jagged Array

Example of Jagged Arrays  Read a set of numbers and group them by their remainder when dividing to 3 ( 0, 1 and 2 ) 19 1, 4, 113, 55, 3, 1, 2, 66, 557, 124, 2

int[] numbers = { 1, 4, 113, 55, 3, 1, 2, 66, 557, 124, 2 }; int[] sizes = new int[3]; int[] offsets = new int[3]; foreach (var number in numbers) { int remainder = number % 3; int remainder = number % 3; sizes[remainder]++; sizes[remainder]++;} int[][] numbersByRemainder = { new int[sizes[0]], new int[sizes[1]], new int[sizes[2]] }; { new int[sizes[0]], new int[sizes[1]], new int[sizes[2]] }; foreach (var number in numbers) { int remainder = number % 3; int remainder = number % 3; int index = offsets[remainder]; int index = offsets[remainder]; numbersByRemainder[remainder][index] = number; numbersByRemainder[remainder][index] = number; offsets[remainder]++; offsets[remainder]++;} Example of Jagged Arrays 20

Remainders of 3 Live Demo

Pascal's Triangle Live Demo

Sets HashSet and SortedSet

24  A set keep unique elements  Provides methods for adding/removing/searching elements  Offers very fast performance  HashSet  Keeps a set of elements in a hash-tables  The elements are randomly ordered (by their hash code)  SortedSet  Keeps a set of elements in a red-black ordered search tree  The elements are ordered incrementally Sets in C#

25 HashSet – Example HashSet set = new HashSet (); set.Add("Pesho");set.Add("Pesho");set.Add("Gosho");set.Add("Alice"); Console.WriteLine(string.Join(" ", set)); // Pesho Gosho Alice Console.WriteLine(set.Contains("Georgi")); // false Console.WriteLine(set.Contains("Pesho")); // true set.Remove("Pesho"); Console.WriteLine(set.Count); // 2

26 SortedSet – Example SortedSet set = new SortedSet (); set.Add("Pesho");set.Add("Pesho");set.Add("Pesho");set.Add("Gosho");set.Add("Maria");set.Add("Alice"); Console.WriteLine(string.Join(" ", set)); // Alice Gosho Maria Pesho Keeps the elements sorted

HashSet and SortedSet Live Demo

Associative Arrays Dictionary

 Associative arrays are arrays indexed by keys  Not by the numbers 0, 1, 2, …  Hold a set of pairs Associative Arrays (Maps, Dictionaries)  Traditional array  Associative array John Smith Lisa Smith Sam Doe key value key value 29

Phonebook – Example Dictionary phonebook = new Dictionary (); new Dictionary (); phonebook["John Smith"] = " "; phonebook["Lisa Smith"] = " "; phonebook["Sam Doe"] = " "; phonebook["Nakov"] = " "; phonebook["Nakov"] = " "; phonebook.Remove("John Smith"); foreach (var pair in phonebook) { Console.WriteLine("{0} --> {1}", entry.Key, entry.Value); Console.WriteLine("{0} --> {1}", entry.Key, entry.Value);} 30

31 Events – Example SortedDictionary events = new SortedDictionary (); new SortedDictionary (); events[new DateTime(1998, 9, 4)] = "Google's birth date"; events[new DateTime(2013, 11, 5)] = "SoftUni's birth date"; events[new DateTime(1975, 4, 4)] = "Microsoft's birth date"; events[new DateTime(2004, 2, 4)] = "Facebook's birth date"; events[new DateTime(2013, 11, 5)] = "Nakov left Telerik Academy to establish SoftUni"; "Nakov left Telerik Academy to establish SoftUni"; foreach (var entry in events) { Console.WriteLine("{0:dd-MMM-yyyy}: {1}", Console.WriteLine("{0:dd-MMM-yyyy}: {1}", entry.Key, entry.Value); entry.Key, entry.Value);}

Dictionary and SortedDictionary Dictionary and SortedDictionary  Implemented as a hash table  Have property Count – the number of key-value pairs  Keys – a collection of all keys  Values – a collection of all values  Basic operations – Add(), Remove(), Clear()  Boolean methods:  ContainsKey() – checks if a key is present in the dictionary  ContainsValue() – checks if a value is present in the dictionary 32

Associative Arrays Live Demo

Summary  Multidimensional arrays have more than one dimension  Two-dimensional arrays are like tables with rows and columns  Jagged arrays are arrays of arrays – each element is an array itself  The HashSet and SortedSet hold unique elements and are very fast  Dictionary is an associative array where a value is accessed by its key 34

? ? ? ? ? ? ? ? ? Multidimensional Arrays, Sets, Dictionaries

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International  Attribution: this work may contain portions from  "C# Fundamentals – Part 1" course by Telerik Academy under CC-BY-NC-SA licenseCC-BY-NC-SA  "C# Fundamentals – Part 2" course by Telerik Academy under CC-BY-NC-SA licenseCC-BY-NC-SA 36

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg