Arrays: Difference between revisions
(edit) |
(edit) |
||
Line 1: | Line 1: | ||
An array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school. | |||
An array allows you to refer to these related values by the same name and to use a number, called an index or subscript, to tell them apart. The individual values are called the elements of the array. They are contiguous from index 1 through the highest index value. | |||
==Example== | |||
The following example declares an array variable to hold the number of students in each grade in a grammar school. | |||
Dim students(7) | |||
The array students in the preceding example contains 7 elements. The indexes of the elements range from 1 through 7. Having this array is simpler than declaring 7 different variables. | |||
The following illustration shows the array students. For each element of the array: | |||
* The index of the element represents the grade (index 1 represents kindergarten). | |||
* The value contained in the element represents the number of students in that grade. | |||
[[File:Arrays.gif]] | |||
00010 Dim students(7) | |||
00020 let students(1)=25 | |||
00030 let students(2)=22 | |||
00040 let students(3)=29 | |||
00050 let students(4)=33 | |||
00060 let students(5)=24 | |||
00070 let students(6)=26 | |||
00080 let students(7)=27 | |||
00090 let MsgBox("Students in kindergarten = " & Str$(students(1))) | |||
00100 let MsgBox("Students in first grade = " & Str$(students(2))) | |||
00110 let MsgBox("Students in sixth grade = " & Str$(students(7))) | |||
==Dimensions== | |||
A dimension is a direction in which you can vary the specification of an array's elements. An array that holds the sales total for each day of the month has one dimension (the day of the month). An array that holds the sales total by department for each day of the month has two dimensions (the department number and the day of the month). | |||
You specify an element of an array by supplying an index or subscript for each of its dimensions. The elements are contiguous along each dimension from index 1 through the highest index for that dimension. | |||
The following illustrations show the conceptual structure of arrays with different dimensions. Each element in the illustrations shows the index values that access it. For example, you can access the first element of the second row of the two-dimensional array by specifying indexes (2, 1). | |||
One-dimensional array | |||
[[File:1-dim-array.png]] | |||
Two-dimensional array | |||
[[File:2-dim-array.png]] | |||
Three-dimensional array | |||
[[File:3-dim-array.png]] | |||
===One Dimension=== | |||
Many arrays have only one dimension, such as the number of people of each age. The only requirement to specify an element is the age for which that element holds the count. Therefore, such an array uses only one index. The following example declares a variable to hold a one-dimensional array of age counts for ages 0 through 120. | |||
Dim ageCounts(120) | |||
===Two Dimensions=== | |||
Some arrays have two dimensions, such as the number of offices on each floor of each building on a campus. The specification of an element requires both the building number and the floor, and each element holds the count for that combination of building and floor. Therefore, such an array uses two indexes. The following example declares a variable to hold a two-dimensional array of office counts, for buildings 1 through 40 and floors 1 through 5. | |||
Dim officeCounts(40, 5) | |||
A two-dimensional array is also called a rectangular array. | |||
===Three Dimensions=== | |||
A few arrays have three dimensions, such as values in three-dimensional space. Such an array uses three indexes, which in this case represent the x, y, and z coordinates of physical space. The following example declares a variable to hold a three-dimensional array of air temperatures at various points in a three-dimensional volume. | |||
Dim airTemperatures(99, 99, 24) | |||
===More than Three Dimensions=== | |||
Although an array can have many dimensions, it is rare to have more than three. | |||
'''Note''' that when you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care. | |||
'''Mat''' is short for '''matrix'''. A matrix is another word for an '''array'''. An array of [[Variable|variables]] in this case. An array is a series of variables. | '''Mat''' is short for '''matrix'''. A matrix is another word for an '''array'''. An array of [[Variable|variables]] in this case. An array is a series of variables. | ||
Revision as of 14:13, 8 January 2012
An array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school.
An array allows you to refer to these related values by the same name and to use a number, called an index or subscript, to tell them apart. The individual values are called the elements of the array. They are contiguous from index 1 through the highest index value.
Example
The following example declares an array variable to hold the number of students in each grade in a grammar school.
Dim students(7)
The array students in the preceding example contains 7 elements. The indexes of the elements range from 1 through 7. Having this array is simpler than declaring 7 different variables.
The following illustration shows the array students. For each element of the array:
- The index of the element represents the grade (index 1 represents kindergarten).
- The value contained in the element represents the number of students in that grade.
00010 Dim students(7) 00020 let students(1)=25 00030 let students(2)=22 00040 let students(3)=29 00050 let students(4)=33 00060 let students(5)=24 00070 let students(6)=26 00080 let students(7)=27 00090 let MsgBox("Students in kindergarten = " & Str$(students(1))) 00100 let MsgBox("Students in first grade = " & Str$(students(2))) 00110 let MsgBox("Students in sixth grade = " & Str$(students(7)))
Dimensions
A dimension is a direction in which you can vary the specification of an array's elements. An array that holds the sales total for each day of the month has one dimension (the day of the month). An array that holds the sales total by department for each day of the month has two dimensions (the department number and the day of the month).
You specify an element of an array by supplying an index or subscript for each of its dimensions. The elements are contiguous along each dimension from index 1 through the highest index for that dimension.
The following illustrations show the conceptual structure of arrays with different dimensions. Each element in the illustrations shows the index values that access it. For example, you can access the first element of the second row of the two-dimensional array by specifying indexes (2, 1).
One-dimensional array
Two-dimensional array
Three-dimensional array
One Dimension
Many arrays have only one dimension, such as the number of people of each age. The only requirement to specify an element is the age for which that element holds the count. Therefore, such an array uses only one index. The following example declares a variable to hold a one-dimensional array of age counts for ages 0 through 120.
Dim ageCounts(120)
Two Dimensions
Some arrays have two dimensions, such as the number of offices on each floor of each building on a campus. The specification of an element requires both the building number and the floor, and each element holds the count for that combination of building and floor. Therefore, such an array uses two indexes. The following example declares a variable to hold a two-dimensional array of office counts, for buildings 1 through 40 and floors 1 through 5.
Dim officeCounts(40, 5)
A two-dimensional array is also called a rectangular array.
Three Dimensions
A few arrays have three dimensions, such as values in three-dimensional space. Such an array uses three indexes, which in this case represent the x, y, and z coordinates of physical space. The following example declares a variable to hold a three-dimensional array of air temperatures at various points in a three-dimensional volume.
Dim airTemperatures(99, 99, 24)
More than Three Dimensions
Although an array can have many dimensions, it is rare to have more than three.
Note that when you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.
Mat is short for matrix. A matrix is another word for an array. An array of variables in this case. An array is a series of variables.
Examples
Mat A would refer to the whole array of A(1) to A(10) (if 10 is the highest item in the array)
each item in the array can contain a different value. for example
- A(1)=10
- A(2)=30
- A(3)=25
- A(4)=40
one thing you can do with a mat statement is refer to the whole array, so if you said PRINT MAT A the system would return
- 10
- 30
- 25
- 40
another thing you can do with a mat statement is change how large the array is MAT A(5) would change the array to have 5 items in it. so after executing MAT A(5) if you did PRINT MAT A you would get
- 10
- 30
- 25
- 40
- 0
(because the 5th item never got set to anything)
you can also use a mat statement to refer to a range within an array, for example PRINT MAT A(2:4) would return
- 30
- 25
- 40
Arrays can be sorted thus changing the order of the items contained within the arrays.
also arrays can have more than one dimension.
- X(1,1)=5
- X(1,2)=10
- X(2,1)=15
- X(2,2)=20
these are best envisioned like a spread sheet... having two numbers to identify which cell a value is contained in.