Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processing Arrays Lesson 9 McManusCOP10061. Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.

Similar presentations


Presentation on theme: "Processing Arrays Lesson 9 McManusCOP10061. Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements."— Presentation transcript:

1 Processing Arrays Lesson 9 McManusCOP10061

2 Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements of an Array Two-Dimensional Arrays –Loading a Two-Dimensional Array –Printing a Two-Dimensional Array –Accumulating the Rows and Columns of a Two- Dimensional Array McManusCOP10062

3 Overview cont. Multidimensional Arrays Table Look-Up Technique –Sequential Search –Binary Search The Pointer Technique –Frequency Distribution –Cross Tabulation McManusCOP10063

4 Arrays A consecutive group of memory locations that all have the same name and the same data type. Are the simplest structured type. –Component access is by a position number (an index) that indicates the component’s position within the collection. ExamplesmyArray(3) myArray(n) McManusCOP10064

5 Simple Arrays Are declared at compile time. –Compiler reserves the appropriate amount of contiguous memory to hold the array. Occupy storage in memory –System must have enough memory –Memory may be reserved for more than one array in one statement, but not recommended. –Examples: Dim b(99) As Integer Dim s(14) As String McManusCOP10065

6 Array Initialization McManusCOP10066 Private Sub cmdPrint_Click() Dim anarray(9) As Integer ‘10 Elements Dim x As Integer ‘Used as Loop Counter lblDisplay.Text = “Index” & Space(3) & _ “Value” ‘Heading For x = 0 To anarray.GetUpperbound(0) _ Step 1 anarray(x) = x lblDisplay.Text = x & Space(7) anarray(x) Next x End Sub VB Code What will it display?

7 Array Initialization In VB, by default, all arrays are initialized to 0 and won’t allow you to change the default 0 first element. However, other languages, like Pascal, will allow you to initialize the array to something other than zero, but you have to do it... Note…not all languages have this facility… In other languages, you have to initialize the array. McManusCOP10067

8 Array Type Declaration type CharArray = array [1..7] of Char; Allocate storage for the array var LetterArray : CharArray; LetterArray LetterArray[3] = ‘c’ the element in Position 3 is “c” McManusCOP10068 ‘a’ 1 ‘b’ 2 ‘c’ 3 ‘d’ 4 ‘e’ 5 ‘f’ 6 ‘g’ 7 Pascal Code Element Index

9 Accessing Array Elements To access the array called numbers, call the array by name and include in () the index (or location) of the value to be accessed. numbers(2)  23; numbers(4)  Note: the second element in the array is not 23. Source of off-by-one errors Print numbers(2) + numbers(3) will print 51 McManusCOP10069 10152328324547 0 1 2 3 4 5 6 An array called Numbers Index

10 Some Examples Numbers( 2 ) + Numbers( 3 ) 5 + 7 = 12 Numbers( 2 + 3 ) value stored in 5 is 6 Numbers( 2 ) + 3valued stored in 2 is 5 5 + 3 = 8 Numbers( 5 - 3 + 2 ) value stored in 4 is 3 Numbers( 1 * 2 )value stored in 2 is 5 Numbers( 5 + 1 ) generates a syntax error McManusCOP100610

11 How Random Access Works The computer calculates the addresses of an array for you. McManusCOP100611 Index_Location = Base + (Index * Element_Length) Index_Location = 1000 + ( 3 * 2 ) Index_Location(3) = 1006 which is the Address for the element stored at Index 3. 1015 2328324547 0 1 2 3 4 5 6 Index 1000 Base 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes

12 Storing Data in an Array Must be read into an array one element at a time. Must be displayed one element at a time. for Index := 1 to MaxItems do Read (DataArray[Index]); for Index := 1 to MaxItems do WriteLn (Index :4, DataArray[Index] :8:1); McManusCOP100612 Pascal Code

13 One-Dimensional Arrays The simplest Array Structure Ex. Array - Age Variable Name AGE (1) AGE (2) AGE (3) AGE (4) AGE (5) AGE (6) AGE (7) AGE (8) McManusCOP100613 1 8 6 7 2 3 4 5 The Number In the Parentheses refers to the Box number in the Array, the Element Number 32 54 25 36 45 20 28 50

14 Accessing Arrays Individual array elements and entire arrays can be passed as parameters. Arrays can also be copied from one array to another. constMaxSize = 100; type IndexRange = 1..MaxSize; TestArray = array [IndexRange] of Real; var XArray, YArray : TestArray; {declares both as arrays} XArray := YArray {copies YArray to XArray} McManusCOP100614 Both arrays must be of the same data type. Pascal Code

15 Entering Data into an Array To Load an Array, use a Loop –If you know the number of elements, then use the automatic-counter loop –If you don’t know the number of elements, then use an indicator code with the Repeat/Until or the While/While-end loop McManusCOP100615

16 McManusCOP100616 Pseudocode Algorithm for Index = 1 To N Step 1 AnArray(Index) = X next Index Index = Counter N = Number of elements in AnArray AnArray(Index) = Element Index in Array X = data entered into AnArray(Index) AnArray(Index) = X Index <= N Index = Index + 1 Index = 1 False True Flowchart

17 McManusCOP100617 Algorithm Index = 0 Repeat Index = Index + 1 AnArray(Index) = X Until Index > 10 X = data entered into AnArray(Index) Index = 0 Index = Index + 1 AnArray(Index) = X Until Index > 10 True False

18 McManusCOP100618 Algorithm Index = 1 AnArray(Index) = Readvalue While AnArray(Index) <> Sentinel AnArray(Index) = Readvalue End While Sentinel = end test value Readvalue = value being read in from user Index= 1 AnArray (Index) = Readvalue True False AnArray (Index) = Readvalue AnArray (Index) <> Sentinel Flowchart

19 Printing an Array McManusCOP100619 Algorithm For Index= 1 To N Step 1 Print AnArray(Index) Next Index N = Total number of elements AnArray(Index) = i th element of Array Flowchart Print AnArray (Index) Index <= N Index = Index + 1 Index = 1 False True implicit Implicit Step Note: Not all languages begin with 1 as the default first element. Some, like VB, begin with 0

20 Two-Dimensional Arrays A two-dimensional array is a block of memory locations associated with a single memory variable name and designated by row and column numbers Each element is written as A (Row#,Column#) –The row number is always first, and the column number second –Note: This is different than in Excel McManusCOP100620

21 Two-Dimensional Arrays For a two-dimensional array, information is stored in rows and columns. –Requires two indexes: The first, by convention, represents the row The second represents the column. Cannot use the same index for both. –Ex. mArray (x, y) or mArray(row, col) McManusCOP100621

22 Two-Dimensional Array McManusCOP100622 Rows Columns 1 st Element Subscripts 0,3 1,1 1,3 0,1 0,2 1,2 1,0 0,0 18 43 95 9 31 56 12 78

23 Loading a Two-Dimensional Array You load a Two-dimensional Array with nested loops –When the data is loaded row by row, the outer loop represents the row, and the inner loop represents the column –This order of the loops allows the row number to stay constant while the column number varies McManusCOP100623

24 Loading a Two-Dimensional Array Algorithm For R = 0 To 4 Sum = Sum + Sales(R,C) For C = 0 To 4 Total = Total + Sales(R,C) Next C Next R McManusCOP100624 Flowchart Enter Exit C = 0 to 4 Sum = Sum + Sales(R,C) Total = Total + Sales(R,C) C R = 0 to 4 R 0,3 1,1 1,3 0,1 0,2 1,2 1,0 0,0 8 7 6 4 3 2 1 9 2,3 3,1 3,3 2,1 2,2 3,2 3,0 2,0 18 17 16 14 13 12 11 19 4,1 4,3 4,2 4,0 23 22 21 24 0,4 1,4 5 10 2,4 3,4 15 20 4,4 25 rows columns An array called Sales

25 Printing Two-Dimensional Arrays Use nested loops to print a two- dimensional array Normally, the array is printed in row by column order McManusCOP100625

26 Multidimensional Arrays arrays with 3 or more dimensions –can facilitate an understanding of the data, –improve the readability of algorithms, and –facilitate processing since the data can be processed through the use of three or more nested loops. McManusCOP100626

27 Multidimensional Arrays The recommended variable names for the indexes for a 3-dimensional array would be –R for row, –C for column, –D for Depth. –For a 4th dimension V for volume is used These arrays are processed much like 2- dimensional arrays McManusCOP100627

28 Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. Robert Herrick McManusCOP100628

29 Table Look-up Technique A common application for arrays is using a value to look up another value in a table Two (of many) methods for looking up values in an array or table –The Sequential search method –The Binary search method McManusCOP100629

30 Sequential Search This method is used when you don’t know the element number, but you do know the value of the element The methodology is simple. –Given the search value of the element you want to find, element one is tested to see if it matches the search variable. If it does, the flow of the program drops out of the loop. If it doesn’t, the element number is incremented and loops back to test again McManusCOP100630

31 Binary Search This is a High speed search This search technique involves comparing the mid-element of all or part of the array. –If it compares then it drops you out of the loop. –If it does not, the program checks to see if the value is lower or higher than the middle value. McManusCOP100631

32 Binary Search cont. The boundaries are then reset to select a new section of the array. The program continues to divide the array in half until the desired element is found A 1,000 element array would take less than 10 comparisons McManusCOP100632

33 The Pointer Technique Method using arrays to specify the value of an element in one array as the element number in another array –The value of the element in the first array points to element in the second array –Frequency Distribution and Cross- Tabulation use this technique McManusCOP100633

34 Frequency Distribution a tally of one type of value in an array –Example: how many students in a school are in each class or how many of a company’s customers live in each zip code area The result of a frequency distribution is a one dimensional array that contains the value for each element number McManusCOP100634

35 Cross-Tabulation This uses the pointer technique for calculating statistics from a questionnaire –Example: How many students are in each major and each class The result of this cross-tabulation is a two- dimensional array containing the tally for the combination of the majors and classes –The majors would be the rows and the classes would be the columns McManusCOP100635

36 Next? McManusCOP100636


Download ppt "Processing Arrays Lesson 9 McManusCOP10061. Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements."

Similar presentations


Ads by Google