Programming Logic and Design Chap.
8 Arrays Complete Answers
‘
Each element of the array has a unique number called a subscript that identifies it
the subscript always starts at 0
Assigning values can be done individually using a subscript
Include example
What is a more effective way to step through the array - ANSWER-Set numbers[0] =
20 Set numbers[1] = 30
Set numbers[2] = 40 Set numbers[3] = 50 Set numbers[4[ = 60
But, it is much more efficient to use a Loop to step through the array
Arrays can be initialized to 0 or specific values
What should be done to avoid use of an invalid subscript - ANSWER-Array bounds
checking should be performed to avoid use of an invalid subscript
A common error is running a loop one time more than is
necessary, exceeding the bound of the array
Off-by-one Error
What is a partial filled array - ANSWER-To avoid processing the unfilled elements,
you must have an accompanying integer variable that holds the number of items
stored in the array
When the array is empty, 0 is stored in this variable
The variable is incremented each time an item is added to
the array
The variable's value is used as the array's size when stepping through the array
Other information - ANSWER-The For Each Loop
Some languages provide a For Each loop
It works with an array, iterating once for each array
element
During each iteration, the loop copies an element's value to a variable.
, For Each example: - ANSWER-Constant Integer SIZE = 5
Declare Integer numbers[SIZE] = 5, 10, 15, 20, 25
Declare Integer num
For Each num In numbers
Display num
End For
What is a sequential teach algorithm - ANSWER-A sequential search algorithm is a
simple technique for finding an item in a string or numeric array
Uses a loop to sequentially step through an array
Compares each element with the value being searched for
More information - ANSWER-Totaling the values in an array and calculating
average
Loops are used to accumulate the values
Then, the total is simply divided by the size
Finding the highest and lowest values in an array - ANSWER-The highest
Create a variable to hold the highest value
Assign the value at element 0 to the highest
Use a loop to step through the rest of the elements
Each iteration, a comparison is made to the highest variable
If the element is greater than the highest value, that value is then the assigned to the
highest variable
The lowest
Same process, but checks if the element is less than the lowest value
Copying an array can be done using loops - ANSWER-Copying an array can be
done using loops
For index = 0 to SIZE - 1
Set secondArray[index] = firstArray[index]
End For
Passing an Array as an Argument
8 Arrays Complete Answers
‘
Each element of the array has a unique number called a subscript that identifies it
the subscript always starts at 0
Assigning values can be done individually using a subscript
Include example
What is a more effective way to step through the array - ANSWER-Set numbers[0] =
20 Set numbers[1] = 30
Set numbers[2] = 40 Set numbers[3] = 50 Set numbers[4[ = 60
But, it is much more efficient to use a Loop to step through the array
Arrays can be initialized to 0 or specific values
What should be done to avoid use of an invalid subscript - ANSWER-Array bounds
checking should be performed to avoid use of an invalid subscript
A common error is running a loop one time more than is
necessary, exceeding the bound of the array
Off-by-one Error
What is a partial filled array - ANSWER-To avoid processing the unfilled elements,
you must have an accompanying integer variable that holds the number of items
stored in the array
When the array is empty, 0 is stored in this variable
The variable is incremented each time an item is added to
the array
The variable's value is used as the array's size when stepping through the array
Other information - ANSWER-The For Each Loop
Some languages provide a For Each loop
It works with an array, iterating once for each array
element
During each iteration, the loop copies an element's value to a variable.
, For Each example: - ANSWER-Constant Integer SIZE = 5
Declare Integer numbers[SIZE] = 5, 10, 15, 20, 25
Declare Integer num
For Each num In numbers
Display num
End For
What is a sequential teach algorithm - ANSWER-A sequential search algorithm is a
simple technique for finding an item in a string or numeric array
Uses a loop to sequentially step through an array
Compares each element with the value being searched for
More information - ANSWER-Totaling the values in an array and calculating
average
Loops are used to accumulate the values
Then, the total is simply divided by the size
Finding the highest and lowest values in an array - ANSWER-The highest
Create a variable to hold the highest value
Assign the value at element 0 to the highest
Use a loop to step through the rest of the elements
Each iteration, a comparison is made to the highest variable
If the element is greater than the highest value, that value is then the assigned to the
highest variable
The lowest
Same process, but checks if the element is less than the lowest value
Copying an array can be done using loops - ANSWER-Copying an array can be
done using loops
For index = 0 to SIZE - 1
Set secondArray[index] = firstArray[index]
End For
Passing an Array as an Argument