arrays - wordpress.com...2019/03/07  · table of contents 1. declaring and creating arrays 2....

55
Arrays Arrays Processing Sequences of Elements Processing Sequences of Elements Svetlin Nakov Svetlin Nakov Telerik Corporation Telerik Corporation www.telerik.com www.telerik.com

Upload: others

Post on 02-Feb-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

  • ArraysArraysProcessing Sequences of ElementsProcessing Sequences of Elements

    Svetlin NakovSvetlin NakovTelerik CorporationTelerik Corporationwww.telerik.comwww.telerik.com

    http://www.telerik.com/

  • Table of ContentsTable of Contents

    1.1. Declaring and Creating ArraysDeclaring and Creating Arrays

    2.2. Accessing Array ElementsAccessing Array Elements

    3.3. Console Input and Output of ArraysConsole Input and Output of Arrays

    4.4. Iterating Over Arrays Using Iterating Over Arrays Using forfor and and foreachforeach

    5.5. Matrices and Multidimensional ArraysMatrices and Multidimensional Arrays

    6.6. Dynamic ArraysDynamic Arrays

    ListsLists

    Copying ArraysCopying Arrays

  • Declaring and Declaring and Creating Arrays Creating Arrays

  • What are Arrays?What are Arrays? An array is a sequence of elementsAn array is a sequence of elements

    All elements are of the same typeAll elements are of the same type

    The order of the elements is fixedThe order of the elements is fixed

    Has fixed size (Has fixed size (Array.LengthArray.Length))

    0 1 2 3 40 1 2 3 4Array of 5 Array of 5 elementselements

    ElemenElement indext index

    Element Element of an of an arrayarray

    …… …… …… …… ……

  • Declaring ArraysDeclaring Arrays Declaration defines the type of the elementsDeclaration defines the type of the elements Square brackets Square brackets [][] mean "array" mean "array" Examples:Examples:

    Declaring array of integers:Declaring array of integers:

    Declaring array of strings:Declaring array of strings:

    int[] myIntArray;int[] myIntArray;

    string[] myStringArray;string[] myStringArray;

  • Creating ArraysCreating Arrays Use the operator Use the operator newnew

    Specify array lengthSpecify array length

    Example creating (allocating) array of 5 Example creating (allocating) array of 5 integers:integers:

    myIntArray = new int[5];myIntArray = new int[5];

    myIntArraymyIntArray

    managed heapmanaged heap(dynamic memory)(dynamic memory)

    0 1 2 3 40 1 2 3 4…… …… …… …… ……

  • Creating and Initializing ArraysCreating and Initializing Arrays Creating and initializing can be done together:Creating and initializing can be done together:

    The The newnew operator is not required when using operator is not required when using curly brackets initializationcurly brackets initialization

    myIntArray = {1, 2, 3, 4, 5};myIntArray = {1, 2, 3, 4, 5};

    myIntArraymyIntArray

    managed heapmanaged heap(dynamic memory)(dynamic memory)

    0 1 2 3 40 1 2 3 4…… …… …… …… ……

  • Creating Array – ExampleCreating Array – Example

    Creating an array that contains the names of Creating an array that contains the names of the days of the weekthe days of the week

    string[] daysOfWeek =string[] daysOfWeek ={{ "Monday","Monday", "Tuesday","Tuesday", "Wednesday","Wednesday", "Thursday","Thursday", "Friday","Friday", "Saturday","Saturday", "Sunday""Sunday"};};

  • Days of WeekDays of WeekLive DemoLive Demo

  • Accessing Array ElementsAccessing Array ElementsRead and Modify Elements by IndexRead and Modify Elements by Index

  • How to Access Array Element?How to Access Array Element? Array elements are accessed using the square Array elements are accessed using the square

    brackets operator brackets operator [][] (indexer) (indexer)

    Array indexer takes element’s index as Array indexer takes element’s index as parameterparameter

    The first element has index The first element has index 00

    The last element has index The last element has index Length-1Length-1

    Array elements can be retrieved and changed Array elements can be retrieved and changed by the by the [][] operator operator

  • Reversing an Array – ExampleReversing an Array – Example Reversing the contents of an arrayReversing the contents of an array

    int[] array = new int[] {1, 2, 3, 4, 5};int[] array = new int[] {1, 2, 3, 4, 5};

    // Get array size// Get array sizeint length = array.Length;int length = array.Length;

    // Declare and create the reversed array// Declare and create the reversed arrayint[] reversed = new int[length];int[] reversed = new int[length]; // Initialize the reversed array// Initialize the reversed arrayfor (int index = 0; index < length; index++)for (int index = 0; index < length; index++){{ reversed[length-index-1] = array[index];reversed[length-index-1] = array[index];}}

  • Reversing an ArrayReversing an ArrayLive DemoLive Demo

  • Arrays: Input and OutputArrays: Input and OutputReading and Printing Arrays on the ConsoleReading and Printing Arrays on the Console

  • Reading Arrays From the Reading Arrays From the ConsoleConsole

    First, read from the console the length of the First, read from the console the length of the arrayarray

    Next, create the array of given size and read Next, create the array of given size and read its elements in a its elements in a forfor loop loop

    int n = int.Parse(Console.ReadLine());int n = int.Parse(Console.ReadLine());

    int[] arr = new int[n];int[] arr = new int[n];for (int i=0; i

  • Symmetry Check – ExampleSymmetry Check – Example Read Read intint array from the console and array from the console and

    check if it is symmetric:check if it is symmetric:

    bool isSymmetric = true;bool isSymmetric = true;for (int i=0; i

  • Symmetry CheckSymmetry CheckLive DemoLive Demo

  • Printing Arrays on the ConsolePrinting Arrays on the Console Process all elements of the arrayProcess all elements of the array

    Print each element to the consolePrint each element to the console

    Separate elements with white space or a new lineSeparate elements with white space or a new line

    string[] array = {"one", "two", "three"};string[] array = {"one", "two", "three"};

    // Process all elements of the array// Process all elements of the arrayfor (int index = 0; index < array.Length; index++)for (int index = 0; index < array.Length; index++){{ // Print each element on a separate line// Print each element on a separate line Console.WriteLine("element[{0}] = {1}",Console.WriteLine("element[{0}] = {1}", index, array[index]);index, array[index]);}}

  • Printing ArraysPrinting ArraysLive DemoLive Demo

  • Processing Array Elements Processing Array Elements Using Using forfor and and foreachforeach

  • Processing Arrays: Processing Arrays: forfor StatementStatement Use Use forfor loop to process an array when loop to process an array when

    Need to keep track of the indexNeed to keep track of the index

    Processing is not strictly sequential from the Processing is not strictly sequential from the first to the last elementfirst to the last element

    In the loop body use the element at the loop In the loop body use the element at the loop index (index (array[index]array[index]):):

    for (int index = 0; index < array.Length; index++)for (int index = 0; index < array.Length; index++){{ squares[index] = array[index] * array[index];squares[index] = array[index] * array[index];}}

  • Processing Arrays Using Processing Arrays Using forforLoop – ExamplesLoop – Examples

    Printing array of integers in reversed order:Printing array of integers in reversed order:

    Initialize all array elements with their Initialize all array elements with their corresponding index number:corresponding index number:

    Console.WriteLine("Reversed: ");Console.WriteLine("Reversed: ");for (int i = array.Length-1; i >= 0; i--)for (int i = array.Length-1; i >= 0; i--){{ Console.Write(array[i] + " ");Console.Write(array[i] + " ");}}// Result: 5 4 3 2 1// Result: 5 4 3 2 1

    for (int index = 0; index < array.Length-1; index++)for (int index = 0; index < array.Length-1; index++){{ array[index] = index;array[index] = index;}}

  • Processing Arrays: Processing Arrays: foreachforeach How How foreachforeach loop works? loop works?

    typetype – the type of the element – the type of the element

    valuevalue – local name of variable – local name of variable

    arrayarray – processing array – processing array

    Used when no indexing is neededUsed when no indexing is needed

    All elements are accessed one by oneAll elements are accessed one by one

    Elements can not be modified (read only)Elements can not be modified (read only)

    foreach (type value in array)foreach (type value in array)

  • Processing ArraysProcessing Arrays Using Using foreachforeach – Example– Example

    Print all elements of a Print all elements of a string[]string[] array: array:

    string[] capitals =string[] capitals ={{ "Sofia","Sofia", "Washington","Washington", "London","London", "Paris""Paris"};};foreach (string capital in capitals)foreach (string capital in capitals){{ Console.WriteLine(capital);Console.WriteLine(capital);}}

  • Processing ArraysProcessing ArraysLive DemoLive Demo

  • Multidimensional Arrays Multidimensional Arrays Using Array of Arrays, Matrices and CubesUsing Array of Arrays, Matrices and Cubes

  • What is Multidimensional Array?What is Multidimensional Array?

    Multidimensional arraysMultidimensional arrays have more than one have more than one dimension (2, 3, …)dimension (2, 3, …)

    The most important multidimensional arrays The most important multidimensional arrays are the 2-dimensionalare the 2-dimensional

    Known as Known as matricesmatrices or or tablestables

    Example of matrix of integers with 2 rows and Example of matrix of integers with 2 rows and 4 columns:4 columns:

    55 00 -2-2 4455 66 77 88

    0 1 2 3

    0

    1

  • Declaring and Creating Declaring and Creating Multidimensional ArraysMultidimensional Arrays

    Declaring multidimensional arrays:Declaring multidimensional arrays:

    Creating a multidimensional arrayCreating a multidimensional array

    Use Use newnew keyword keyword

    Must specify the size of each dimensionMust specify the size of each dimension

    int[,] intMatrix;int[,] intMatrix;float[,] floatMatrix;float[,] floatMatrix;string[,,] strCube;string[,,] strCube;

    int[,] intMatrix = new int[3, 4];int[,] intMatrix = new int[3, 4];float[,] floatMatrix = new float[8, 2];float[,] floatMatrix = new float[8, 2];string[,,] stringCube = new string[5, 5, 5];string[,,] stringCube = new string[5, 5, 5];

  • Initializing Multidimensional Initializing Multidimensional Arrays with ValuesArrays with Values

    Creating and initializing with values Creating and initializing with values multidimensional array:multidimensional array:

    Matrices are represented by a list of rowsMatrices are represented by a list of rows

    Rows consist of list of valuesRows consist of list of values

    The first dimension comes first, the second The first dimension comes first, the second comes next (inside the first)comes next (inside the first)

    int[,] matrix = 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}; // The matrix size is 2 x 4 (2 rows, 4 cols)}; // The matrix size is 2 x 4 (2 rows, 4 cols)

  • Accessing The Elements of Accessing The Elements of Multidimensional ArraysMultidimensional Arrays

    Accessing N-dimensional array element:Accessing N-dimensional array element:

    Getting element value example:Getting element value example:

    Setting element value example:Setting element value example:

    nDimensionalArray[index1, … , indexn]nDimensionalArray[index1, … , indexn]

    int[,] array = {{1, 2}, {3, 4}}int[,] array = {{1, 2}, {3, 4}}int element11 = array[1, 1]; //element11 = 4int element11 = array[1, 1]; //element11 = 4

    int[,] array = new int[3, 4];int[,] array = new int[3, 4];for (int row=0; row

  • Reading Matrix – ExampleReading Matrix – Example Reading a matrix from the consoleReading a matrix from the console

    int rows = int.Parse(Console.ReadLine());int rows = int.Parse(Console.ReadLine());int columns = int.Parse(Console.ReadLine());int columns = int.Parse(Console.ReadLine());int[,] matrix = new int[rows, columns];int[,] matrix = new int[rows, columns];String inputNumber;String inputNumber;for (int row=0; row

  • Printing Matrix – ExamplePrinting Matrix – Example

    Printing a matrix on the console:Printing a matrix on the console:

    for (int row=0; row

  • Reading and Reading and Printing MatricesPrinting Matrices

    Live DemoLive Demo

  • Finding a 2 x 2 platform in a matrix with a Finding a 2 x 2 platform in a matrix with a maximal sum of its elementsmaximal sum of its elementsint[,] matrix = {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;int bestSum = int.MinValue;for (int row=0; row

  • Maximal PlatformMaximal PlatformLive DemoLive Demo

  • Dynamic ArraysDynamic ArraysListList

  • ListsLists ListsLists are arrays that resize dynamicallyare arrays that resize dynamically When adding or removing elementsWhen adding or removing elements Also have indexers ( like Also have indexers ( like ArrayArray)) TT is the type that the List will hold is the type that the List will hold E.g. E.g. ListList will hold will hold integersintegers

    ListList will hold will hold objectsobjects

    Basic Methods and PropertiesBasic Methods and Properties Add(TAdd(T element)element) – adds new element to the end– adds new element to the end Remove(element)Remove(element) – removes the element – removes the element CountCount – returns the – returns the current size of the Listcurrent size of the List

  • List Example List Example List intList=new List();List intList=new List();for( int i=0; i

  • Lists vs. ArraysLists vs. Arrays Lets have an array with capacity of 5 elementsLets have an array with capacity of 5 elements

    If we want to add a sixth element ( we have If we want to add a sixth element ( we have already added 5) we have to doalready added 5) we have to do

    With List we simply doWith List we simply do

    39

    int[] intArray=new int[5];int[] intArray=new int[5];

    int[] copyArray = intArray;int[] copyArray = intArray;intArray = new int[6];intArray = new int[6];for (int i = 0; i < 5; i++)for (int i = 0; i < 5; i++){{ intArray[i] = copyArray[i];intArray[i] = copyArray[i];}}intArray[5]=newValue;intArray[5]=newValue;

    list.Add(newValue);list.Add(newValue);

  • Lists Lists Live DemoLive Demo

    40

  • Copying ArraysCopying ArraysThe Array ClassThe Array Class

  • Copying ArraysCopying Arrays Sometimes we must copy the values from one Sometimes we must copy the values from one

    array to another onearray to another one

    If we do it the intuitive way wIf we do it the intuitive way we would copy not e would copy not only the values but the reference to the arrayonly the values but the reference to the array

    Changing some of the values in one array will Changing some of the values in one array will affect the otheraffect the other

    The way to avoid this is using The way to avoid this is using Array.Copy()Array.Copy()

    This way only the values will be copied but not This way only the values will be copied but not the referencethe reference

    Array.Copy(sourceArray, copyArray);Array.Copy(sourceArray, copyArray);

    int[] copyArray=array;int[] copyArray=array;

  • SummarySummary Arrays are a fixed-length sequences of Arrays are a fixed-length sequences of

    elements of the same typeelements of the same type Array elements are accessible by indexArray elements are accessible by index

    Can be read and modifiedCan be read and modified

    Iteration over array elements can be done with Iteration over array elements can be done with forfor and and foreachforeach loops loops

    Matrices (2-dimensional arrays) are very useful Matrices (2-dimensional arrays) are very useful for presenting tabular datafor presenting tabular data

  • Questions?Questions?Questions?Questions?

    ArraysArrays

    http://academy.telerik.com

    http://academy.telerik.com/

  • ExercisesExercises

    1.1. Write a program that allocates array of 20 integers Write a program that allocates array of 20 integers and initializes each element by its index multiplied and initializes each element by its index multiplied by 5. Print the obtained array on the console.by 5. Print the obtained array on the console.

    2.2. Write a program that reads two arrays from the Write a program that reads two arrays from the console and compares them element by element.console and compares them element by element.

    3.3. Write a program that compares two Write a program that compares two charchar arrays arrays lexicographically (letter by letter).lexicographically (letter by letter).

    4.4. Write a program that finds the maximal sequence of Write a program that finds the maximal sequence of equal elements in an array.equal elements in an array.

    Example: {2, 1, 1, 2, 3, 3, Example: {2, 1, 1, 2, 3, 3, 2, 2, 22, 2, 2, 1} , 1} {2, 2, 2}. {2, 2, 2}.

  • Exercises (2)Exercises (2)1.1. Write a program that finds the maximal increasing Write a program that finds the maximal increasing

    sequence in an array. Example: sequence in an array. Example: {3, {3, 2, 3, 42, 3, 4, 2, 2, 4} , 2, 2, 4} {2, 3, 4}. {2, 3, 4}.

    2.2. Write a program that reads two integer numbers N Write a program that reads two integer numbers N and K and an array of N elements from the console. and K and an array of N elements from the console. Find in the array those K elements that have Find in the array those K elements that have maximal sum.maximal sum.

    3.3. Sorting an array means to arrange its elements in Sorting an array means to arrange its elements in increasing order. Write a program to sort an array. increasing order. Write a program to sort an array. Use the "selection sort" algorithm: Find the smallest Use the "selection sort" algorithm: Find the smallest element, move it at the first position, find the element, move it at the first position, find the smallest from the rest, move it at the second smallest from the rest, move it at the second position, etc.position, etc.

  • Exercises (3)Exercises (3)

    1.1. Write a program that finds the sequence of maximal Write a program that finds the sequence of maximal sum in given array. Example:sum in given array. Example:

    {2, 3, -6, -1, {2, 3, -6, -1, 2, -1, 6, 42, -1, 6, 4, -8, 8} , -8, 8} {2, -1, 6, 4} {2, -1, 6, 4}

    Can you do it with only one loop (with single scan Can you do it with only one loop (with single scan through the elements of the array)?through the elements of the array)?

    4.4. Write a program that finds the most frequent Write a program that finds the most frequent number in an array. Example:number in an array. Example:

    {{44, 1, 1, , 1, 1, 44, 2, 3, , 2, 3, 44, , 44, 1, 2, , 1, 2, 44, 9, 3} , 9, 3} 4 (5 times) 4 (5 times)

    a Write a program that finds in given array of integers Write a program that finds in given array of integers a sequence of given sum S (if present). Example:a sequence of given sum S (if present). Example: {4, 3, 1, {4, 3, 1, 4, 2, 54, 2, 5, 8}, S=11 , 8}, S=11 {4, 2, 5} {4, 2, 5}

  • Exercises (4)Exercises (4)1.1. Write a program that fills and prints a matrix of size Write a program that fills and prints a matrix of size

    (n, n) as shown below: (examples for n = 4)(n, n) as shown below: (examples for n = 4)

    11 55 99 131322 66 1010 141433 77 1111 151544 88 1212 1616

    77 1111 1414 161644 88 1212 151522 55 99 131311 33 66 1010

    11 88 99 161622 77 1010 151533 66 1111 141444 55 1212 1313

    11 1212 1111 101022 1313 1616 9933 1414 1515 8844 55 66 77

    a) b)

    c) d)

  • Exercises (5)Exercises (5)Ì Write a program that reads a rectangular matrix of Write a program that reads a rectangular matrix of

    size N x M and finds in it the square 3 x 3 that has size N x M and finds in it the square 3 x 3 that has maximal sum of its elements.maximal sum of its elements.

    t We are given a matrix of strings of size N x M. We are given a matrix of strings of size N x M. Sequences in the matrix Sequences in the matrix we define as sets of several we define as sets of several neighbor elements located on the same line, column neighbor elements located on the same line, column or diagonal. Write a program that finds the longest or diagonal. Write a program that finds the longest sequence of equal strings in the matrix. Examples:sequence of equal strings in the matrix. Examples:

    haha fifififi hoho hihi

    fofo haha hihi xxxx

    xxxxxx hoho haha xxxx

    ss qqqq ss

    pppp pppp ss

    pppp qqqq ssha, ha, ha s, s, s

  • Exercises (6)Exercises (6)

    1.1. Write a program that finds the index of given Write a program that finds the index of given element in a sorted array of integers by using the element in a sorted array of integers by using the binarybinary searchsearch algorithm (find it in Wikipedia). algorithm (find it in Wikipedia).

    2.2. Write a program that creates an array containing all Write a program that creates an array containing all letters from the alphabet (A-Z). Read a word from letters from the alphabet (A-Z). Read a word from the console and print the index of each of its letters the console and print the index of each of its letters in the array.in the array.

    3.3. Write a program that sorts an array of integers using Write a program that sorts an array of integers using the the mergemerge sort sort algorithm (find it in Wikipedia). algorithm (find it in Wikipedia).

    4.4. Write a program that sorts an array of strings using Write a program that sorts an array of strings using the the quickquick sortsort algorithm (find it in Wikipedia). algorithm (find it in Wikipedia).

    http://en.wikipedia.org/wiki/Binary_search_algorithmhttp://en.wikipedia.org/wiki/Binary_search_algorithmhttp://en.wikipedia.org/wiki/Binary_search_algorithmhttp://en.wikipedia.org/wiki/Merge_sorthttp://en.wikipedia.org/wiki/Merge_sorthttp://en.wikipedia.org/wiki/Quicksorthttp://en.wikipedia.org/wiki/Quicksorthttp://en.wikipedia.org/wiki/Quicksort

  • Exercises (7)Exercises (7)z Write a program that finds all prime numbers in the Write a program that finds all prime numbers in the

    range range [1...10 000 000].[1...10 000 000]. Use the Use the sieve of Eratosthenessieve of Eratosthenes algorithm (find it in Wikipedia).algorithm (find it in Wikipedia).

    i * We are given an array of integers and a number S. * We are given an array of integers and a number S. Write a program to find if there exists a subset of the Write a program to find if there exists a subset of the elements of the array that has a sum S. Example:elements of the array that has a sum S. Example:

    arrarr={2, ={2, 11, , 22, 4, 3, , 4, 3, 55, 2, , 2, 66}, S=14 }, S=14 yes (1+2+5+6) yes (1+2+5+6)

    4.4. * Write a program that reads three integer numbers * Write a program that reads three integer numbers N, K and S and an array of N elements from the N, K and S and an array of N elements from the console. Find in the array a subset of K elements console. Find in the array a subset of K elements that have sum S or indicate about its absence.that have sum S or indicate about its absence.

    http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

  • Exercises (8)Exercises (8)

    1.1. * Write a program that reads an array of integers * Write a program that reads an array of integers and removes from it a minimal number of elements and removes from it a minimal number of elements in such way that the remaining array is sorted in in such way that the remaining array is sorted in increasing order. Print the remaining sorted array. increasing order. Print the remaining sorted array. Example:Example:

    {6, {6, 11, 4, , 4, 33, 0, , 0, 33, 6, , 6, 44, , 55} } {1, 3, 3, 4, 5} {1, 3, 3, 4, 5}

    3.3. * Write a program that reads a number N and * Write a program that reads a number N and generates and prints all the permutations of the generates and prints all the permutations of the numbers [1 … N]. Example:numbers [1 … N]. Example:

    n = 3 n = 3 {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}{3, 2, 1}

  • Exercises (9)Exercises (9)

    1.1. Write a program that reads two numbers N and K Write a program that reads two numbers N and K and generates all the variations of K elements from and generates all the variations of K elements from the set [1..N]. Example:the set [1..N]. Example:

    N = 3, K = 2 N = 3, K = 2 {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}{3, 1}, {3, 2}, {3, 3}

    3.3. Write a program that reads two numbers N and K Write a program that reads two numbers N and K and generates all the combinations of K distinct and generates all the combinations of K distinct elements from the set [1..N]. Example:elements from the set [1..N]. Example:

    N = 5, K = 2 N = 5, K = 2 {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}{2, 5}, {3, 4}, {3, 5}, {4, 5}

  • Exercises (10)Exercises (10)

    1.1. Write a program that fills a matrix of size (N, N) as Write a program that fills a matrix of size (N, N) as shown in the examples (for Nshown in the examples (for N=4=4):):

    1616 1515 1313 10101414 1212 99 661111 88 55 3377 44 22 11

    77 1111 1414 161644 88 1212 151522 55 99 131311 33 66 1010

    1010 1111 1212 131399 22 33 141488 11 44 151577 66 55 1616

    11 1212 1111 101022 1313 1616 9933 1414 1515 8844 55 66 77

    a) b)

    *c) *d)

  • Exercises (11)Exercises (11)

    1.1. * Write a program that finds the largest area of * Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and equal neighbor elements in a rectangular matrix and prints its size. Example:prints its size. Example:

    Hint: you can use the algorithm "Hint: you can use the algorithm "Depth-first searchDepth-first search" " or "or "Breadth-first searchBreadth-first search" (find them in Wikipedia)." (find them in Wikipedia).

    11 33 22 22 22 4433 33 33 22 44 4444 33 11 22 33 3344 33 11 33 33 1144 33 33 33 11 11

    13

    http://en.wikipedia.org/wiki/Depth-first_searchhttp://en.wikipedia.org/wiki/Breadth-first_search

    ArraysTable of ContentsDeclaring and Creating Arrays What are Arrays?Declaring ArraysCreating ArraysCreating and Initializing ArraysCreating Array – ExampleDays of WeekPowerPoint PresentationHow to Access Array Element?Reversing an Array – ExampleReversing an ArrayArrays: Input and OutputReading Arrays From the ConsoleSymmetry Check – ExampleSymmetry CheckPrinting Arrays on the ConsolePrinting ArraysProcessing Array Elements Using for and foreachProcessing Arrays: for StatementProcessing Arrays Using for Loop – ExamplesProcessing Arrays: foreachProcessing Arrays Using foreach – ExampleProcessing ArraysMultidimensional Arrays What is Multidimensional Array?Declaring and Creating Multidimensional ArraysInitializing Multidimensional Arrays with ValuesAccessing The Elements of Multidimensional ArraysReading Matrix – ExamplePrinting Matrix – ExampleReading and Printing MatricesMaximal Platform – ExampleMaximal PlatformDynamic ArraysListsList Example Lists vs. ArraysLists Copying ArraysSlide 42SummarySlide 44ExercisesExercises (2)Exercises (3)Exercises (4)Exercises (5)Exercises (6)Exercises (7)Exercises (8)Exercises (9)Exercises (10)Exercises (11)