1 / 30

CSCI 3328 Object Oriented Programming in C# Chapter 7 : Arrays

CSCI 3328 Object Oriented Programming in C# Chapter 7 : Arrays. Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539 xiang.lian@utrgv.edu. Objectives. In this chapter, you will: Learn how arrays are used to store elements

pringlej
Download Presentation

CSCI 3328 Object Oriented Programming in C# Chapter 7 : Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539 xiang.lian@utrgv.edu

  2. Objectives • In this chapter, you will: • Learn how arrays are used to store elements • Get familiar with the declaration, initialization, and reference to elements of the array • Know how to pass arrays to methods • Learn how to use multidimensional arrays • Learn how to use foreachstatement to iterate through elements in the array

  3. Arrays • An array is a group of variables (called elements) containing values that all have the same data type • For most languages, the number of elements are static and defined at declaration • Single dim, two dim and multiple dim

  4. Example of an Array • The first element is the zeroth element • The highest position number is array’s upper bound • Array C on RHS: 11 • Access array elements • Use index (or subscript) • E.g., C[8]

  5. Arrays (cont'd) • Data structure consisting of related data items of the same type • One variable name, multiple elements • Arrays are passed as reference type, not value • To access an element we use name of the array and index in square bracket

  6. Declaring and Creating Arrays • Declaration • int[] array = new int[12]; (creates indices 0 to 11) • Or do it this way: • int[] array; • array = newint[12]; • Or this way: • int[] array; • constintsize = 12; • array = newint[size]; • Since array is an object, we can resize the array as follows: • Array.Resize(ref array, 10);

  7. Arrays of Other Data Types • string [] str = newstring [100]; • char [] ch = newchar [ 50]; • …

  8. Initializing an Array // creates 5-element array • Initializing arrays while declaring • int [] arr = new int [] {10, 20, 30, 40, 50}; • int [] array = {10,20,30,40,50};

  9. Sum in an Array • int[] array = {10,20,30,40,50}; // creates 5-element array • Finding the sum double sum=0; double average; for (int counter =0; counter < array.Length; counter++) sum = sum+array[counter]; size of the array

  10. Average in an Array • int[] array = {10,20,30,40,50}; // creates 5-element array • Finding the sum double sum=0; double average; for (int counter =0; counter < array.Length; counter++) sum = sum+array[counter]; average = sum/array.Length;

  11. Standard Deviation Explained • In your assignment, assuming array has length n, the standard deviation s is given by:

  12. foreach Statements • In addition to for loops, you can also work with: • Syntax • foreach (typeidentifierinarrayName) • REMEMBER: YOU HAVE TO START WITH ELEMENT 0!

  13. Syntax of foreach Statements • Example of computing summation of elements in an array called scores: foreach (int score in scores) { sum = sum + score; }

  14. Passing Arrays by Value • Declaration • int[] firstArray={1, 2, 3}; • int[] firstArrayCopy = firstArray; • public static void FirstDouble (int [] array) { for (int i = 0; i<array.Length; i++) array[i]*=2; array = new int [] {11, 12, 13}; } • Call • FirstDouble(firstArray);

  15. Passing Arrays by Reference • Declaration • int[] secondArray={1, 2, 3}; • int[] secondArrayCopy = firstArray; • public static void SecondDouble (refint [] array) { for (int i = 0; i<array.Length; i++) array[i]*=2; array = new int [] {11, 12, 13}; } • Call • SecondDouble(ref secondArray);

  16. Rectangular Arrays • Rectangular array is a two dimensional array • Similar to a table with rows and columns • arrayName[row, column] • int [ , ] a = new int [3, 4]; Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2

  17. Nested Array Initializer • int [ , ] arr = {{1, 2}, {3, 4}};

  18. Example of Rectangular Arrays public int [,] hands = new int[5, 14]; for (i=1; i<=52; i++) { hands[player, hand] = sdeck[i]; }

  19. Jagged Arrays • Tables that have rows of unequal columns • Somewhat like an array of arrays • Fixed students and exams not everyone taking it.. int [] [] scores = new int [students][]; scores[0] = new int [5]; // student 0 takes 5 exams scores[1] = new int [4]; // student 1 takes 4 exams

  20. Sort an Array

  21. Sort Method of Array • You need to write your own sort methods later own • For now you can use: • Array.Sort(scores); • The identifier, scores, is the array you created

  22. Search Method • Searching: to determine whether a value (called search key) is in the array • Linear search • Compare each element of the array with the search key • For either sorted or unsorted array • Efficient for small array • Binary search • For sorted array

  23. Linear Search search key

  24. Binary Search • You need to write your own binary search method later • But now you can use: • int index = Array.BinarySearch(array, value); • If index < 0, then value is not found in the array • Otherwise, index stores the index of value in the array

  25. Binary Search search key: 10 first middle last

  26. Binary Search search key: 10 first middle last

  27. Binary Search search key: 10 first middle last

  28. Binary Search search key: 10 first middle last

  29. Copying an Array • You can copy one element at a time or the entire array • Array.Copy(fromArray, fromIndex, toArray, to Index, length); • Note: the following statement does not copy, but creates another reference to the array • string[] copyDeck = oDeck;

More Related