1 / 33

CHAPTER 6: ARRAYS

CHAPTER 6: ARRAYS. Introduction to Computer Science Using Ruby. Arrays. A data structure is any organized means of storage An array is a simple data structure, belonging to (instantiated from) the Array Class. Figure 6.1: An ordered list of variables. One Dimensional Arrays.

quynh
Download Presentation

CHAPTER 6: 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. (c) 2012 Ophir Frieder et al CHAPTER 6: ARRAYS Introduction to Computer Science Using Ruby

  2. Arrays • A data structure is any organized means of storage • An array is a simple data structure, belonging to (instantiated from) the Array Class Figure 6.1: An ordered list of variables (c) 2012 Ophir Frieder et al

  3. One Dimensional Arrays • Arrays are like rows of numbered compartments • Arrays start counting their elements at the index zero • The nth element can be found at index n – 1 • An array is one-dimensional when it has only one index or dimension • To access an element in an array, use: array_name[index] (c) 2012 Ophir Frieder et al

  4. One Dimensional Arrays • To create a new array, use:array_name = Array.new • A simpler way to automatically create (instantiate) and initialize the same array (Example 6.2): Example 6.1: 1 arr = Array.new 2 arr[0] = 73 3 arr[1] = 98 4 arr[2] = 86 5 arr[3] = 61 6 arr[4] = 96 1 arr = [73, 98, 86, 61, 96] (c) 2012 Ophir Frieder et al

  5. One Dimensional Arrays • To use the array, access array_name[index] as if it was a variable of the data type expected (Example 6.3) 1 arr = [5,6] 2 arr[0] = arr[0] + 10 3 puts arr[0] (c) 2012 Ophir Frieder et al

  6. One Dimensional Arrays • Arrays cluster multiple data items under one name • Key advantage of using arrays: when they are used in conjunction with loops • Can use a variable for the index instead of literal numbers • You can change the index in every loop iteration and traverse through every element in the array (c) 2012 Ophir Frieder et al

  7. One Dimensional Arrays • To know when to stop traversing, get the number of elements in an array using: arr.size • New programmers often make errors dealing with the bounds of an array • Basic rules for array bounds: • The first element in an array is at index 0 • arr.size is not the highest indexed element • The last element in an array is at arr.size – 1 (c) 2012 Ophir Frieder et al

  8. One Dimensional Arrays • To traverse an array using a while loop: • Initialize the index to 0 • Increment it for every loop iteration • The condition is index < arr.size Example 6.4: 1 arr = [73, 98, 86, 61, 96] 2 index = 0 3 while (index < arr.size) 4 puts arr[index] 5 index = index + 1 6 end (c) 2012 Ophir Frieder et al

  9. One Dimensional Arrays • Running the code gives the following output: 73 98 86 61 • That same array can be output with the code: puts arr (c) 2012 Ophir Frieder et al

  10. Example: Find the Max of an Array of Positive Numbers (Example 6.5) 1 # Initialize array and loop values 2 arr = [73, 98, 86, 61, 96] 3 index = 0 4 max = 0 5 6 # Loop over each element in arr 7 while (index < arr.size) 8 if (arr[index] > max) 9 # Update max 10 max = arr[index] 11 end 12 index = index + 1 13 end 14 15 # Output calculated max 16 puts "Max ==> " + max.to_s (c) 2012 Ophir Frieder et al

  11. 1 # Initialize array and loop values 2 arr = [73, 98, 86, 61, 96] 3 index = 0 4 max = 0 5 6 # Loop over each element in arr 7 while (index < arr.size) 8 if (arr[index] > max) 9 # Update max 10 max = arr[index] 11 end 12 index = index + 1 13 end 14 15 # Output calculuated max 16 puts "Max ==> " + max.to_s Counter for the index Will store the maximum element Increments the loop (c) 2012 Ophir Frieder et al

  12. 1 # Initialize array and loop values 2 arr = [73, 98, 86, 61, 96] 3 index = 0 4 max = 0 5 6 # Loop over each element in arr 7 while (index < arr.size) 8 if (arr[index] > max) 9 # Update max 10 max = arr[index] 11 end 12 index = index + 1 13 end 14 15 # Output calculuated max 16 puts "Max ==> " + max.to_s Compares max to current element The output for the program is: Max ==> 98 (c) 2012 Ophir Frieder et al

  13. Summary • An array is a data structure that stores multiple variables, belonging to the class Array • Data stored in an array are accessed using numbers as an index starting at zero (c) 2012 Ophir Frieder et al

  14. Strings • Strings are data structures that can be viewed as one dimensional arrays of character, BUT they are NOT arrays • The most used string in programming books is “Hello World” • It does not belong to the Class Array, but to the Class String (c) 2012 Ophir Frieder et al

  15. Strings • Strings, however, look like arrays, so it is natural to have for them access mechanisms and methods similar to arrays my_arr = Array.new my_str = String.new my_arr = [1,2,3,5,8] my_str = “Hello World” my_arr.size #5 my_str.size #11 my_arr.size #3 my_str[2] # “l” my_arr[2..3] # [3,5] my_str[2..3] # “ll” my_arr[2,3] # [3,5,8] my_str[2,3] # “llo” my_arr[2..4] # [3,5,8] my_str[8..9] # “rl” my_arr[2,4] # [3,5,8] my_str[8,9] # “rld” (c) 2012 Ophir Frieder et al

  16. Strings • Strings, being elements (or objects) of the Class String, also have defined operations “Hello” + “ ” + “World” produces “Hello World” (c) 2012 Ophir Frieder et al

  17. Strings and Arrays • Arrays, being objects of the Class Array, also have defined operations, such as +, with a meaning similar to String [1,2,3] + [3,5] produces [1,2,3,3,5] (c) 2012 Ophir Frieder et al

  18. Strings and Arrays • Arrays, being objects of the Class Array, also have defined operations, such as - , which is a bit unusual [1,2,3] - [3,5] produces [1,2] [3,5] – [1,2,3] produces [5] (c) 2012 Ophir Frieder et al

  19. Strings and Arrays • What is the meaning of – for strings? “ I am not” – “I am” Should it be “ not” ???????? NO!!!!!!! The operation (method) - Is NOT defined for the Class String (c) 2012 Ophir Frieder et al

  20. Strings and Arrays Note also the following 3 * [1,2] is an error [1,2] * 3 is [1,2,1,2,1,2] 3 * “ab “ is an error “ab “ * 3 is “ab ab ab “ (c) 2012 Ophir Frieder et al

  21. Multi-Dimensional Arrays • Arrays that have more than one dimension are called multidimensional arrays • Ruby basically recognizes only one dimensional arrays, but it is very flexible • For Ruby, you must put an array inside an array • A common type is the two-dimensional array, which is used to represent matrices and coordinate systems (c) 2012 Ophir Frieder et al

  22. Multi-Dimensional Arrays • Consider the following set of grades: (c) 2012 Ophir Frieder et al

  23. Multi-Dimensional Arrays • To represent the following data, use an array of arrays: arr = [ [73,98,86,61,96], # arr[0] [60,90,96,92,77], # arr[1] [44,50,99,65,100] ] # arr[2] (c) 2012 Ophir Frieder et al

  24. Multi-Dimensional Arrays • To access an individual score, use:array[row][column] • To find Brittany’s score for her third exam, type: puts “Brittany’s Third Exam: ” + arr[1][2].to_s (Note the use of “ ” to allow the ’s) • The output should be: Brittany’s Third Exam: 96 • Traversing a multidimensional array requires a nested loop for every additional dimension (c) 2012 Ophir Frieder et al

  25. Example 6.6: Outputting Multidimensional Arrays 1 # Initialize array and loop values 2 arr = [[73, 98, 86, 61, 96], 3 [60, 90, 96, 92, 77], 4 [44, 50, 99, 65, 100]] 5 row = 0 6 column = 0 7 8 # Loop over each row 9 while (row < arr.size) 10 puts "Row: " + row.to_s 11 # Loop over each column 12 while (column < arr[row].size) 13 # Print the item at position row x column 14 puts arr[row][column] 15 column = column + 1 16 end 17 # Reset column, advance row 18 column = 0 19 row = row + 1 20 end You can also output everything using one line: puts arr The only problem is that output will have no formatting (c) 2012 Ophir Frieder et al

  26. Example 6.7: Modified Find the Max 1 # initialize the array and index/score variables 2 arr = [[73, 98, 86, 61, 96], 3 [60, 90, 96, 92, 77], 4 [44, 50, 99, 65, 10]] 5 6 row = 0 7 column = 0 8 maxscore = 0 9 maxrow = 0 10 11 # for each row 12 while (row < arr.size) 13 # for each column 14 while (column < arr[row].size) 15 # update score variables 16 if (arr[row][column] > maxscore) 17 maxrow = row 18 maxscore = arr[row][column] 19 end 20 # increment column (c) 2012 Ophir Frieder et al

  27. Example 6.7 Cont’d 21 column = column + 1 22 end 23 # reset column, increment row 24 column = 0 25 row = row + 1 26 end 27 28 # output name and high score information 29 if maxrow == 0 30 puts "Geraldo has the highest score." 31 elsif maxrow == 1 32 puts "Brittany has the highest score." 33 elsif maxrow == 2 34 puts "Michael has the highest score." 35 else 36 puts "Something didn't work correctly." 37 end 38 puts "The high score was: " + maxscore.to_s (c) 2012 Ophir Frieder et al

  28. 1 # initialize the array and index/score variables 2 arr = [[73, 98, 86, 61, 96], 3 [60, 90, 96, 92, 77], 4 [44, 50, 99, 65, 10]] 5 6 row = 0 7 column = 0 8 maxscore = 0 9 maxrow = 0 10 11 # for each row 12 while (row < arr.size) 13 # for each column 14 while (column < arr[row].size) 15 # update score variables 16 if (arr[row][column] > maxscore) 17 maxrow = row 18 maxscore = arr[row][column] 19 end 20 # increment column Initialize array arr Variables for location. Needed for traversal. Variables for the highest score and the person Loop traverses and updates the maxrowand maxscore (c) 2012 Ophir Frieder et al

  29. 21 column = column + 1 22 end 23 # reset column, increment row 24 column = 0 25 row = row + 1 26 end 27 28 # output name and high score information 29 if maxrow == 0 30 puts "Geraldo has the highest score." 31 elsif maxrow == 1 32 puts "Brittany has the highest score." 33 elsif maxrow == 2 34 puts "Michael has the highest score." 35 else 36 puts "Something didn't work correctly." 37 end 38 puts "The high score was: " + maxscore.to_s End of loop (c) 2012 Ophir Frieder et al

  30. Output: Michael has the highest score. The high score was: 99. (c) 2012 Ophir Frieder et al

  31. Heterogeneous Arrays • All our examples used homogeneous arrays • In such arrays, all elements belong to the same class • Ruby allows an arbitrary mixing of elements, creating arbitrary dimensioned heterogeneous arrays (c) 2012 Ophir Frieder et al

  32. Multi-Dimensional Arrays arr = Array.new arr[0] = “ Hi y’all” arr[1] = 3.14159265 arr[2] = 17 arr[3] = [1,2,3] arr is [“ Hi y’all” , 3.14159265 , 17, [1,2,3] ] (c) 2012 Ophir Frieder et al

  33. Summary • Arrays are structures that use a table format to store variables • Data stored in an array are accessed using numbers as an index starting at zero • An array can have an infinite number of dimensions and can contain heterogeneous data • Hashes are like arrays, but can use any variable as a key (c) 2012 Ophir Frieder et al

More Related