Podcast
Questions and Answers
What is the purpose of declaring a named numeric constant when working with arrays?
What is the purpose of declaring a named numeric constant when working with arrays?
What is the main advantage of using constants as array element values?
What is the main advantage of using constants as array element values?
What is a flag variable used for in searching an array?
What is a flag variable used for in searching an array?
What is the purpose of setting a subscript variable to 0 when searching an array?
What is the purpose of setting a subscript variable to 0 when searching an array?
Signup and view all the answers
What happens when the desired value is not found in the array during a search?
What happens when the desired value is not found in the array during a search?
Signup and view all the answers
What is the purpose of examining each element in the array during a search?
What is the purpose of examining each element in the array during a search?
Signup and view all the answers
What is the benefit of using named constants in array indexing?
What is the benefit of using named constants in array indexing?
Signup and view all the answers
What happens when the desired value is found in the array during a search?
What happens when the desired value is found in the array during a search?
Signup and view all the answers
What is the purpose of initializing a flag variable to False during a search?
What is the purpose of initializing a flag variable to False during a search?
Signup and view all the answers
What is the purpose of incrementing the subscript variable during a search?
What is the purpose of incrementing the subscript variable during a search?
Signup and view all the answers
Study Notes
Using Parallel Arrays
- Parallel arrays contain related data, with each element in one array associated with an element in a second array.
- A subscript relates the arrays, making elements at the same position in each array logically related.
- Parallel arrays are useful, especially when an indirect relationship exists between an item's number and its price.
Improving Search Efficiency
- To improve search efficiency, a program should stop searching the array when a match is found.
- Setting a variable to a specific value instead of letting normal processing set it can improve efficiency.
- The larger the array, the better the improvement with early exit.
VBA (Visual Basic for Applications)
- In VBA, arrays can be declared with a fixed size, e.g.,
Dim employee(100) As String
. - Option Base 1 can be used to change the starting index of the array from 0 to 1.
- Dynamic indexing allows the array size to be determined at runtime, e.g.,
Dim employee() As String
andRedim employee(1 to nEmployees)
. - Multiple dimensions can be used, e.g.,
Dim employee(100,100) As String
. - Elements of a two-dimensional array can be accessed using nested loops.
Arrays in General
- An array is a named series or list of values in memory with the same data type and different subscripts.
- A variable can be used as a subscript to the array to replace multiple nested decisions.
- Constants can be used to hold an array's size, avoiding "magic numbers".
- Searching through an array requires initializing a subscript, using a loop to test each element, and setting a flag when a match is found.
Using Constants
- Named numeric constants can be used to declare the size of an array, making the code more readable.
- Constants can be used as array element values, e.g.,
string MONTH = "January", "February", ...
. - Constants are created automatically in many languages.
Searching an Array
- A flag is a variable that indicates whether an event occurred.
- Technique for searching an array involves setting a subscript variable, initializing a flag variable, examining each element, and setting the flag to True when a match is found.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the concept of parallel arrays in programming, including their representation in memory, as shown in Figure 6-9.