380 likes | 582 Vues
Processing Arrays. Lesson 8. 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
E N D
Processing Arrays Lesson 8 COP1000
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 COP1000
Overview cont. • Multidimensional Arrays • Table Look-Up Technique • Sequential Search • Binary Search • The Pointer Technique • Frequency Distribution • Cross Tabulation COP1000
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. • Examples myArray(3) myArray(n) COP1000
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 COP1000
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 Array Initialization What will it display? VB Code COP1000
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. COP1000
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" Index Element Pascal Code COP1000
10 15 23 28 32 45 47 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) 32 Note: the second element in the array is not 23. Source of off-by-one errors Print numbers(2) + numbers(3) will print 51 An array called Numbers 0 1 2 3 4 5 6 Index COP1000
Some Examples Numbers( 2 ) + Numbers( 3 ) 5 + 7 = 12 Numbers( 2 + 3 ) value stored in 5 is 6 Numbers( 2 ) + 3 valued stored in 2 is 5 5 + 3 = 8 Numbers( 5 - 3 + 2 ) value stored in 4 is 3 Numbers( 2 * 2 ) value stored in 4 is 3 Numbers( 5 + 1 ) generates a syntax error COP1000
How Random Access Works • The computer calculates the addresses of an array for you. IndexElements 1000Base 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes • 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. COP1000
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); Pascal Code COP1000
One-Dimensional Arrays • The simplest Array Structure Ex. Array - Age Variable Name AGE (1) = 32 AGE (2) = 54 AGE (3) = 25 AGE (4) = 36 AGE (5) = 45 AGE (6) = 20 AGE (7) = 28 AGE (8) = 50 The Number in the Parentheses refers to the Box number in the Array, the Element Number Index Elements COP1000
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 {the code: copies YArray to XArray} Both arrays must be of the same data type. Pascal Code COP1000
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 COP1000
Automatic-Counter Loop Pseudocode Flowchart Index = 1 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 = Index + 1 Index <= N True False COP1000
Algorithm Index = 0 Repeat Index = Index + 1 AnArray(Index) = X Until Index > 10 X = data entered into AnArray(Index) Repeat/Until LoopUsing Definite Looping Pseudocode Flowchart Index = 0 Index = Index + 1 AnArray(Index) = X Until Index > 10 False True COP1000
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 While/While-End LoopUsing Indefinite Looping Pseudocode Flowchart Index= 1 AnArray(Index) = Readvalue AnArray(Index) <> Sentinel AnArray(Index) = Readvalue True False COP1000
Algorithm For Index= 1 To N Step 1 Print AnArray(Index) Next Index N = Total number of elements AnArray(Index) = ith element of Array Index = 1 implicit Index <= N True Print AnArray(Index) False Index = Index + 1 Implicit Step Printing an Array Pseudocode Flowchart Note: Not all languages begin with 1 as the default first element.Some, like VB, begin with 0 COP1000
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 COP1000
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) COP1000
12 56 31 9 0,0 0,2 0,1 0,3 95 43 78 18 1,1 1,3 1,0 1,2 Two-Dimensional Array 1st Element Columns Rows Subscripts COP1000
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 COP1000
Enter 1 2 3 4 5 R = 0 to 4 0,0 0,2 0,1 0,3 0,4 6 7 9 10 8 1,1 1,3 1,4 1,0 Sum = Sum + Sales(R,C) 1,2 11 12 13 14 15 2,0 2,2 2,1 2,3 2,4 16 17 19 20 18 C = 0 to 4 3,1 3,3 3,4 3,0 3,2 21 22 24 25 23 4,1 4,3 4,4 4,0 4,2 Total = Total + Sales(R,C) C R Exit Loading a Two-Dimensional Array Flowchart 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 Pseudocode columns rows An array called Sales COP1000
Printing Two-Dimensional Arrays • Use nested loops to print a two-dimensional array • Normally, the array is printed in row by column order COP1000
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. COP1000
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 COP1000
Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. Robert Herrick COP1000
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 COP1000
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 COP1000
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. [First] [Middle] [Last] Item COP1000
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 COP1000
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 • Techniques • Frequency Distribution and • Cross-Tabulation COP1000
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 COP1000
Frequency Distribution Example COP1000
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 COP1000
Cross Tabulation Example COP1000
Next? DATA STRUCTURES COP1000