Programování 2 // Strings, slice, index string, arrays and their iterations https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_2.html/ Ex. 0.1 to previous lecture ... default value in array? try it (declaration, allocation and initialization) (what could happens generally and what happens in c#) Ex 0.2 what is default value for int, bool, string? Ex 0.3.1 Array reference vs value? Write (develop) example that tests if array are reference or value type Ex 0.3.4 Slicing array int[] a = { 2, 4, 6, 8, 10 }; int[] c = a[1..4]; // { 4, 6, 8 } int[] d = a[..3]; // { 2, 4, 6 } int[] e = a[3..]; // { 8, 10 } int[] a = { 3, 4, 5 }; Arrays are indexed from 0, and the ^ operator indexes from the end: int x = a[1]; // now x is 4 int y = a[^1]; // now y is 5, the last value in the array Ex 0.3.5 Sliced array reference vs value (copy)? Write (develop) example that tests if array are reference or value type Ex 0.4. In a rectangular array a, a.Length is the total number of elements in the array. This is the product of the lengths of all dimensions. a.Rank is the number of dimensions, and the GetLength() method returns the length of a given dimension (numbered from 0): int[,,] a = new int[5, 10, 15]; // a 3-dimensional array Console.WriteLine(a.Length); // writes 750 (= 5 * 10 * 15) Console.WriteLine(a.Rank); // writes 3 Console.WriteLine(a.GetLength(1)); // writes 10 Length and Rank are properties, not methods, so when we access them we don't write (). Ex 0.5 Jagged arrays A jagged array is an array of arrays. Unlike in a rectangular multidimensional array, each subarray in a jagged array can have a different length. You can allocate a jagged array like this: int[][] a = new int[3][]; a[0] = new int[2]; a[1] = new int[4]; a[2] = new int[5]; If you like, you can initialize a jagged array as you allocate it: int[][] a = { new int[] { 2, 3}, new int[] { 3, 4, 5}, new int[] { 1, 3, 5, 7, 9} }; You can access an element of a jagged array using an expression such as a[2][5]. Ex 0.7 parse() and toString() int.MaxValue() int.MinValue() https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_2.html/ TODO(Later?) List a = new(); dynamic array using System.Collections.Generic; Ex 0.9. Sorting Two Values Write a program that reads a sequence of integers on a single line. The sequence is guaranteed to contain at most two distinct values. For example, it might be 17 17 17 11 17 11 11 11 17 11 17 11 11 The program should print out the sequence in sorted order: 11 11 11 11 11 11 11 17 17 17 17 17 17 How efficient is your program? EX 1: 1. Read string from console (and print it). (one line string) 2.0 Print all subStrings with duplicities. 2.1 Make list of all (continous) substrings in string and print it without duplicities. (only by usage of arrays or tuples; naive non efficient implementation) aaBBaaBBBaB - How long should be an array for evidence_list of substrings? - How many substring could be in string length of n? - Static vs dynamic array - Compare of strings; -- What happens when (strings s1, s2) code: if s1==s2? -- What could happens when (strings s1, s2) code: if s1==s2? -- Ref on string or value (sequence of charaters)? aaBBaaBBaB 2.2 Count and Evidence/List all substrings in string (only by usage of arrays or tuples; naive non efficient implementation) Print list of the substrings and their count (tuples) (2.4. For all substrings count Hamming distance to all others different substrings) 3.0 Tuples: create, access, change and array of tuples int -> array int[] -> tuples (string, int) -> struct -> class https://www.programiz.com/csharp-programming/tuple