Using Parallel Arrays in Programming

AffordableFluorite avatar
AffordableFluorite
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is the purpose of declaring a named numeric constant when working with arrays?

To avoid 'magic numbers' and improve readability

What is the main advantage of using constants as array element values?

It improves code readability and maintainability

What is a flag variable used for in searching an array?

To indicate whether the desired value has been found

What is the purpose of setting a subscript variable to 0 when searching an array?

To start the search from the first element of the array

What happens when the desired value is not found in the array during a search?

The search terminates and the flag remains False

What is the purpose of examining each element in the array during a search?

To determine whether the desired value is present

What is the benefit of using named constants in array indexing?

It improves code readability and maintainability

What happens when the desired value is found in the array during a search?

The flag variable is set to True

What is the purpose of initializing a flag variable to False during a search?

To assume the desired value is not present in the array

What is the purpose of incrementing the subscript variable during a search?

To examine the next element in the array

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 and Redim 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.

This quiz covers the concept of parallel arrays in programming, including their representation in memory, as shown in Figure 6-9.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser