cecs 100 ch8 – Flashcards
Unlock all answers in this set
Unlock answersquestion
Can you store a mixture of data types in an array?
answer
No, you cannot. All the items in an array must be of the same data type.
question
What is an array size declarator?
answer
A nonnegative integer that specifies the size of an array.
question
In most languages, can the size of an array be changed while the program is running?
answer
No.
question
What is an array element?
answer
An individual storage location in an array
question
What is a subscript?
answer
A number that identifies a specific element in an array.
question
What does "array bounds checking" mean?
answer
Many languages support array bounds checking, which means they do not allow a program to use an invalid array subscript.
question
What is an off-by-one error?
answer
An off-by-one error occurs when a loop iterates one time too many or one time too few.
question
What is a search algorithm?
answer
An algorithm developed for the purpose of locating a specific item in a larger collection of data, such as an array.
question
Which array element does the sequential search algorithm first look at?
answer
The first element in the array
question
What does the loop do in the sequential search algorithm? What happens when the value being searched for is found?
answer
The loop sequentially steps through each element in the array, comparing the elements to the value being searched for. When the value is found, the loop stops.
question
How many elements does the sequential search algorithm look at in the case that the search value is not found in the array?
answer
It looks at every element in the array.
question
How do you look for a partial string match when searching an array of strings for a value?
answer
You use a function similar to the contains function described in this chapter. The contains function returns true if a string is found inside another string, or false otherwise.
question
Briefly describe how you calculate the total of the values in an array.
answer
To calculate the total of the values in an array, you use a loop with an accumulator variable. The loop steps through the array, adding the value of each array element to the accumulator.
question
Briefly describe how you get the average of the values in an array.
answer
The first step in calculating the average of the values in an array is to get the sum of the values. You use the algorithm for totaling the values in an array to perform this. The second step is to divide the sum by the number of elements in the array.
question
Describe the algorithm for finding the highest value in an array.
answer
You create a variables to hold the highest value. In the examples shown in this book, the variable is named highest. Then, you assign the value at element 0 to the highest variable. Next, you use a loop to step through the rest of the array elements, beginning at element 1. Each time the loop iterates, it compares an array element to the highest variable. If the array element is greater than the highest variable, then the value in the array element is assigned to the highest variable. When the loop finishes, the highest variable will contain the highest value in the array.
question
Describe the algorithm for finding the lowest value in an array.
answer
You create a variables to hold the lowest value. In the examples shown in this book, the variable is named lowest. Then, you assign the value at element 0 to the lowest variable. Next, you use a loop to step through the rest of the array elements, beginning at element 1. Each time the loop iterates, it compares an array element to the lowest variable. If the array element is less than the lowest variable, then the value in the array element is assigned to the lowest variable. When the loop finishes, the lowest variable will contain the lowest value in the array.
question
How do you copy the contents of one array to another array?
answer
You assign the individual elements of the array that you are copying to the elements of the other array. This is usually the best loop.
question
How do you establish a relationship between the data stored in two parallel arrays?
answer
You use the same subscript to access data items in the two arrays.