1/53
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is an array in C++?
An array is a collection of variables of the same data type stored in consecutive memory locations. Each element is accessed using an index that begins at 0.
Why are arrays useful?
Arrays allow many related values to be stored under one variable name, making programs simpler, easier to maintain, and more efficient than using many separate variables.
How do you declare an array?
Specify the data type, array name, and size. Example: int numbers[10]; creates an array capable of storing 10 integers.
What is an array element?
An array element is an individual value stored within an array. Each element has a unique index.
What is an array index?
An array index identifies the position of an element. C++ arrays always begin indexing at 0.
What is the first and last index of an array?
The first index is always 0. The last index is always one less than the array size. For an array of size n, the last index is n-1.
What happens if an array index is out of bounds?
Accessing an invalid index results in undefined behavior. The program may crash, produce incorrect results, or appear to work while corrupting memory.
How do you initialize an array?
An array may be initialized when declared. Example: int nums[5] = {2,4,6,8,10};
What happens if an array is partially initialized?
Any unspecified elements are automatically initialized to 0 for numeric arrays.
How do you access an array element?
Use the array name followed by the index in brackets. Example: numbers[3] accesses the fourth element.
How do you modify an array element?
Assign a value using its index. Example: numbers[2] = 25;
How do you process every element of an array?
Use a loop that starts at index 0 and continues until the last valid index.
What is the standard for loop used to traverse an array?
for(int i = 0; i < size; i++) processes every element exactly once.
What is a range-based for loop?
A range-based for loop automatically visits every element in an array or container without using an index. Example: for(int value : numbers)
When should a traditional for loop be used instead of a range-based loop?
Use a traditional loop when you need the element's index or must modify specific positions.
How do you calculate the sum of an array?
Initialize an accumulator to 0, loop through every element, and add each value to the accumulator.
How do you calculate the average of an array?
Add all elements together and divide the total by the number of elements. Use type casting if a decimal result is required.
How do you find the largest element in an array?
Initialize the largest value to the first element, then compare every remaining element, updating the largest whenever a larger value is found.
How do you find the smallest element in an array?
Initialize the smallest value to the first element, then compare every remaining element, updating the smallest whenever a smaller value is found.
What is linear search?
Linear search examines each element one at a time until the target is found or the end of the array is reached.
When should linear search be used?
Linear search works on both sorted and unsorted arrays and is easy to implement.
What is the worst-case time complexity of linear search?
O(n) because every element may need to be examined.
What is binary search?
Binary search repeatedly divides a sorted array in half until the target is found or no elements remain.
What requirement must be met before using binary search?
The array must already be sorted.
What is the worst-case time complexity of binary search?
O(log n) because the search space is cut in half during each comparison.
How does binary search work?
Compare the target with the middle element. If equal, the search is complete. If smaller, search the left half. If larger, search the right half.
What is sorting?
Sorting arranges data into a specific order such as ascending or descending.
Why is sorting important?
Sorting makes information easier to read and allows algorithms like binary search to work efficiently.
What is selection sort?
Selection sort repeatedly finds the smallest remaining element and swaps it into its correct position.
How does selection sort work?
Each pass selects the minimum element from the unsorted portion of the array and swaps it with the first unsorted position.
What is the time complexity of selection sort?
O(n²) because every element is compared with the remaining unsorted elements.
What is swapping?
Swapping exchanges the values of two variables using a temporary variable.
How do you swap two variables?
Store the first value in a temporary variable, assign the second value to the first variable, then assign the temporary value to the second variable.
What is parallel array processing?
Parallel arrays store related information in separate arrays using the same index. The same index represents related data in each array.
Give an example of parallel arrays.
names[3] and grades[3] use the same index so names[i] corresponds to grades[i].
Can arrays store different data types?
No. Every element within a single array must have the same data type.
Can arrays be passed to functions?
Yes. Arrays are passed by reference automatically, allowing the function to access the original array.
Why must the array size often be passed to a function?
The function does not automatically know how many elements are stored in the array, so the size is usually passed as another parameter.
Can a function return an array?
Traditional C++ arrays cannot be returned directly. Instead, use reference parameters, dynamic memory, or containers like vector.
What is the difference between passing an array and passing a normal variable?
Normal variables are passed by value unless specified otherwise. Arrays automatically allow the function to work with the original data.
How do you copy one array into another?
Use a loop to copy each element individually because traditional arrays cannot be assigned using the = operator.
Can two arrays be compared with ==?
No. Individual elements must be compared one at a time unless using std::array or vector.
What are common mistakes when using arrays?
Common mistakes include using invalid indexes, forgetting arrays start at 0, exceeding the array size, failing to initialize elements, incorrect loop bounds, and off-by-one errors.
What is an off-by-one error in an array?
An off-by-one error occurs when a loop processes one element too many or one too few, usually because of an incorrect stopping condition.
What is a multidimensional array?
A multidimensional array is an array containing other arrays. A two-dimensional array is commonly used to represent tables, matrices, or grids.
How do you declare a two-dimensional array?
Specify rows and columns. Example: int table[3][4];
How do you access a two-dimensional array element?
Use two indexes. Example: table[2][1] accesses row 2, column 1.
How are two-dimensional arrays processed?
Typically with nested loops, where the outer loop processes rows and the inner loop processes columns.
What are the benefits of arrays?
Arrays simplify data storage, reduce code duplication, improve efficiency, and make algorithms such as searching and sorting possible.
What are the limitations of traditional arrays?
Arrays have a fixed size, store only one data type, cannot easily grow or shrink, and require manual copying when resizing.
How should array algorithms be tested?
Test empty arrays if allowed, arrays with one element, already sorted arrays, reverse sorted arrays, duplicate values, negative numbers, and boundary indexes.
What is the relationship between arrays and memory?
Array elements are stored in consecutive memory locations, allowing fast indexed access.
Why do arrays start at index 0 instead of 1?
C++ defines the first element as an offset of zero from the starting memory address, making indexing efficient.
What is a common exam mistake involving arrays?
Using <= instead of < in loop conditions, which attempts to access one element beyond the end of the array.