Download presentation
Presentation is loading. Please wait.
Published byVernon Scott Modified over 9 years ago
1
Copyright ©2005 Department of Computer & Information Science Multidimensional Arrays
2
Copyright ©2005 Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to program parallel arrays.Understand how to program parallel arrays. Understand how to create two- dimensional arrays.Understand how to create two- dimensional arrays. Understand how to access elements in a two-dimensional array.Understand how to access elements in a two-dimensional array.
3
Copyright ©2005 Department of Computer & Information Science Parallel (Corresponding) Arrays Parallel arrays give you the ability to store multiple pieces of information about a single entity in an application.Parallel arrays give you the ability to store multiple pieces of information about a single entity in an application. The subscripts of each array should correspond to one another for individual entities.The subscripts of each array should correspond to one another for individual entities.
4
Copyright ©2005 Department of Computer & Information Science Conceptual Example of Parallel Arrays 0047254 1038546 2024136 3057866 4015543 5025769 6025119 7035176 8036585 9028116 strSID0John1Mary 2Elizabeth 3Omar 4Charles 5Ravi 6Katherine 7Hannah 8Alexander 9William0.931.86 2.74 3.92 4.56 5.82 6.89 7.82 8.75 9.77 strStudents fltExam1Scores
5
Copyright ©2005 Department of Computer & Information Science Creating Parallel Arrays To create parallel arrays, just create them as you would any other array: var strSID = new Array(); var strStudents = new Array(); var fltExamScores = new Array();To create parallel arrays, just create them as you would any other array: var strSID = new Array(); var strStudents = new Array(); var fltExamScores = new Array(); The arrays are separate arrays in memory, treat them as one in your application …The arrays are separate arrays in memory, treat them as one in your application …
6
Copyright ©2005 Department of Computer & Information Science Assigning Values to Parallel Arrays To assign values for a single entity to parallel arrays, use the same index number for each assignment: strSID[5] = “025769”; strStudents[5] = “Ravi”; fltExamScores[5] =.82;To assign values for a single entity to parallel arrays, use the same index number for each assignment: strSID[5] = “025769”; strStudents[5] = “Ravi”; fltExamScores[5] =.82;
7
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called multidimensionalArrays_01. html.
8
Copyright ©2005 Department of Computer & Information Science Another Approach … Another way that we can model tables of data is to use a multidimensional array.Another way that we can model tables of data is to use a multidimensional array. A multidimensional array is an "array of arrays." When we use one, we need to specify multiple index numbers to assign/read elements.A multidimensional array is an "array of arrays." When we use one, we need to specify multiple index numbers to assign/read elements.
9
Copyright ©2005 Department of Computer & Information Science Creating a 2-D Array To create a 2-D array, we first declare a simple array: var arr2D_Array = new Array();To create a 2-D array, we first declare a simple array: var arr2D_Array = new Array(); Then, for each "row" that we add to the 2D array, we create a new array in an index of the parent array: arr2D_Array[0] = new Array();Then, for each "row" that we add to the 2D array, we create a new array in an index of the parent array: arr2D_Array[0] = new Array();
10
Copyright ©2005 Department of Computer & Information Science Accessing 2D Array Elements To read or assign values to elements in a 2D array, we need to specify two subscripts. The first is the "row" number (from the parent); the second represents the "column" number (from the child): arr2D_Array[0][0] = "Hi";To read or assign values to elements in a 2D array, we need to specify two subscripts. The first is the "row" number (from the parent); the second represents the "column" number (from the child): arr2D_Array[0][0] = "Hi";
11
Copyright ©2005 Department of Computer & Information Science Schematic Model of a 2D Array 01234 0[0][0][0][1][0][2][0][3][0][4] 1[1][0][1][1][1][2][1][3][1][4] 2[2][0][2][1][2][2][2][3][2][4] Child (Second) Subscript Parent (First) Subscript
12
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called multidimensionalArrays_02. html.
13
Copyright ©2005 Department of Computer & Information Science Summary We can use parallel arrays to store information about a single entity across multiple arrays.We can use parallel arrays to store information about a single entity across multiple arrays. In a set of parallel arrays, we store values related to a single entity so they share the same subscript number across the arrays in the parallel set.In a set of parallel arrays, we store values related to a single entity so they share the same subscript number across the arrays in the parallel set. continued …
14
Copyright ©2005 Department of Computer & Information Science Summary Multidimensional arrays are arrays of arrays.Multidimensional arrays are arrays of arrays. We can use a two-dimensional array to model a table of data. The rows represent the subscripts from the parent array; the columns represent the subscripts from the children arrays.We can use a two-dimensional array to model a table of data. The rows represent the subscripts from the parent array; the columns represent the subscripts from the children arrays. continued …
15
Copyright ©2005 Department of Computer & Information Science Summary In a two-dimensional array, we access array elements by specifying two subscripts – the first representing the "row" number (from the parent) and the second representing the "column" number (from the child).In a two-dimensional array, we access array elements by specifying two subscripts – the first representing the "row" number (from the parent) and the second representing the "column" number (from the child).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.