Download presentation
Presentation is loading. Please wait.
Published byTrevor Harvey Modified over 8 years ago
1
Processing Arrays Lesson 8 McManusCOP10001
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 McManusCOP10002
3
Overview cont. Multidimensional Arrays Table Look-Up Technique –Sequential Search –Binary Search The Pointer Technique –Frequency Distribution –Cross Tabulation McManusCOP10003
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) McManusCOP10004
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 (99) As Integerin VB declares an array of 100 Integer elements Dim s(14) As Stringin VB declares an array of 15 String elements Int age[12];in C ++ declares an array of 12 Integer elements McManusCOP10005
6
Array Initialization McManusCOP10006 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) = 5 lblDisplay.Text = x & Space(7)& anarray(x) Next x End Sub VB Code What will it display?
7
A Different Method of Initialization McManusCOP10007 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?
8
Array Initialization –In VB, by default, the first array index is initialized to 0 and VB won’t allow you to change the default. –However, other languages, like Pascal, will allow the programmer 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. McManusCOP10008
9
1234567 abccdef 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" McManusCOP10009 Pascal Code Index Element
10
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 McManusCOP100010 152328324547 0 1 2 3 4 5 6 An array called Numbers Index
11
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( 2 * 3 )value stored in 6 is 4 Numbers( 9 + 2 ) generates a syntax error McManusCOP100011
12
How Random Access Works The computer calculates the addresses of an array for you. McManusCOP100012 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. Index Elements 1000 Base 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes 0123456 10152328324547
13
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); McManusCOP100013 Pascal Code
14
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 McManusCOP100014 The Number in the Parentheses refers to the Box number in the Array, the Element Number Index 12345678 3254253645202850 Elements
15
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} McManusCOP100015 Both arrays must be of the same data type. Pascal Code
16
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 (For 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 McManusCOP100016
17
McManusCOP100017 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 Pseudocode
18
McManusCOP100018 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 Pseudocode Flowchart
19
McManusCOP100019 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 Pseudocode
20
Printing an Array McManusCOP100020 Algorithm For Index= 1 To N Step 1 Print AnArray(Index) Next Index N = Total number of elements AnArray(Index) = ith element of Array 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 Flowchart Pseudocode
21
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 McManusCOP100021
22
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) In VB int TwoDimArray[ r ],[ c ] In C ++ McManusCOP100022
23
Two-Dimensional Array McManusCOP100023 Rows Columns 1 st Element Subscripts (indexes) 0,3 1,1 1,3 0,1 0,2 1,2 1,0 0,0 18 43 95 9 31 56 12 78
24
Loading a Two-Dimensional Array You load a Two-dimensional Array with nested loops—one loop for each dimension –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 McManusCOP100024
25
Loading a Two-Dimensional Array Algorithm For R = 0 To 4 Step 1 Sum = Sum + Sales(R,C) For C = 0 To 4 Step 1 Total = Total + Sales(R,C) Next C Next R McManusCOP100025 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 Pseudocode
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. McManusCOP100026
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 –V for Volume These arrays are processed much like 2- dimensional arrays McManusCOP100027 For each additional dimension, an additional loop is used to manipulate the dimension
28
Arrays and Lists How do they differ? McManusCOP100028
29
Arrays vs. Lists Arrays Arrays use Random Access Arrays use Indices to access elements Arrays are declared at Compile Time Array elements are stored in contiguous memory Lists Lists use Sequential Access Lists use Pointers Lists are declared at Run Time List elements are stored in any available location McManusCOP100029
30
Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. Robert Herrick McManusCOP100030
31
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 McManusCOP100031
32
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 McManusCOP100032
33
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. McManusCOP100033 [First] [Middle] [Last] Item FM-1MM+1L
34
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 McManusCOP100034
35
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 McManusCOP100035
36
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 McManusCOP100036
37
Frequency Distribution Example McManusCOP100037 IndexValues (Age Groups) 11-20 221-30 331-40 441-50 551-60 IndexFrequency (# in each Group) 13 25 34 42 51
38
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 McManusCOP100038
39
Cross Tabulation Example Yes (1) No (2) Females (1) 3225 Males (2) 5142 McManusCOP100039
40
Next? McManusCOP100040
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.