CIS 205 Intro to Programming C++ Chapter 7-Arrays & Vectors & Chapter 8-Searching and Sorting Arrays
If you want to use a vector you must
#include<vector>
(2)The size of an array is:
(number of elements) * (number of bytes for each element)
size
(number of elements)*(size of each element)
tests [i]++
// add 1 to tests[i]
tests[i++]
// increment i, no effect on tests
In the definition: int tests [5]; What is 5?
5 is the size declarator
Set first to 0 Set last to the last subscript in the array Set found to false Set position to -1 While found is not true and first is less than or equal to last Set middle to the subscript half-way between array[first] and array[last]. If array[middle] equals the desired value Set found to true Set position to middle Else If array[middle] is greater than the desired value Set last to middle - 1 Else Set first to middle + 1 End If. End While. Return position.
Algorithm for binary search
set found to false; set position to -1; set index to 0 while index < number of elts. and found is false if list[index] is equal to search value found = true position = index end if add 1 to index end while return position
Algorithm of a Linear Search
must also be reflected in the prototype,header.
Array Size
Which is the second benefit of a linear search?
Array can be in any order
Requires array elements to be in order
Binary Search
When using this search, the algorithm starts with the middle element of the array. From there, it searches the first half on one side of the middle element, and then moves onto the other searching for the correct value. The search repeats this process until it produces the correct value or there are no more elements to examine.
Binary Search
Concept of bubble sort
Compare the first two elements of an array. If out of order, exchange them and put them into order. Move to next element, and repeat the process, and exchange when necessary. Continue until the end of the array. Pass through the array again, exchanging values when necessary, and repeat until passes are made with no exchanges.
Which is the first benefit of a linear search?
Easy algorithm to understand
An Array, numlist contains the numbers 1,5,18,2,23,68,4. If prompted to search for the value 2, this type of search examines 1,5,18, and 2.
Example of Linear Search
What is the disadvantage of using a linear search?
Inefficient and slow
This type of search is also called sequential search
Linear
Starting at the first element, this algorithm sequentially steps through an array examining each element until it locates the value it is searching for
Linear Search
Two Algorithms discussed in the chapter are:
Linear and Binary
____ occurs when you use array subscripts that are off by one.
Off-by-one error
two or more arrays that contain related data
Parallel arrays
size declarator
Shows the number of elements in the array
can define one array for multiple sets of data
Two-Dimensional Arrays
is like a table in a spreadsheet
Two-Dimensional Arrays
uses two size declarators in definition
Two-Dimensional Arrays
How are arrays declared?
Using [ ] operator
types of sort
alphabetical, ascending numeric, descending numeric
You can define arrays with
any number of dimensions.
Values
are stored in adjacent memory locations
Sort
arranges values into an order
____ can be used as regular variables
array elements
Array numlist2 contains 2,3,5,11,17,23,29. If you search for the value 11, what happens in the first iteration of the search?
binary search examines 11, and stops, since it is the middle element of the array.
Array numlist2 contains 2,3,5,11,17,23,29. If you search for the value 7, what happens in the search?
binary search examines 11,3,5, and stops since the value is not in the middle element or the lower side of the element.
two sorting algorithms
bubble sort and selection sort
Array names in functions are like reference variables;
changes made to array in a function are reflected in actual array in calling function
you can display the contents of a ____ by sending its name to cout
character array
To remove all contents of vector, use ____ member function:
clear
In a two dimensional array, the Second declarator is number of____.
columns
Array numlist3 finished the second pass of bubble sort. It originally displayed 17,23,5, and 11. After first pass it displays 17,5,11, and 23. After second pass it displays 5,11,17, and 23. What happens in the third pass of bubble sort?
compare 5 and 11; in order, do not exchange. compare 11 and 17; in order, do not exchange. compare 17 and 23; in order, do not exchange. The array is in order.
Array numlist3 contains 17,23,5, and 11. What happens in the first pass of bubble sort?
compare values 17 and 23; they're in order, so no exchange. compare values 23 and 5; not in order, so they get exchanged. compare values 23 and 11; not in order, exchange them. The array now displays 17,5,11,23.
Array numlist3 finished the first pass of bubble sort. It originally displayed 17,23, 5, and 11. It now displays 17,5,11,23. What happens in the second pass of bubble sort?
compare values 17 and 5; not in order, exchange them. compare values 17 and 11; not in order, exchange them. Finally, compare 17 and 23; in correct order, there is no exchange.
You can access element with
constant or literal subscript
use a ____ to keep track of the number of items stored in the array.
counter variable
benefits of bubble sort-
easy to understand and implement
to compare two arrays, you must compare ____.
element by element
Second of three array sections when using binary search
elements on one side of the middle element
Third of three array sections when using binary search
elements on the other side of the middle element
To determine if vector is empty, use ____ member function:
empty
for (dataType rangeVariable : array) statement;
general format of the range-based for loop
all elements initialized to 0 by default
global array
values are stored in the array
in the order in which they appear in the list
arrays must be accessed via
individual elements
arrays can be initialized with an
initialization list
In the definition: int tests [5]; What is the data type?
int
You can use ____ as subscript
integer expression
the range-based for loop
is a loop that iterates once for each element in an array.
the last element's subscript is n-1, where n
is the number of elements in the array
If using C++ 11, you can initialize a vector with a
list of values
all elements uninitialized by default
local array
Search
locate an item in a list of information
concept for selection sort in ascending order-
locate smallest element in array. exchange it with element in position 0. locate next smallest element in array. exchange it with element in position 1. continue until all elements are arranged in order
disadvantage of selection sort
may not be as easy as bubble sort to understand
First of three array sections when using binary search
middle element
benefit of selection sort
more efficient than bubble sort, since it uses fewer exchanges.
benefit(s) of using a binary search:
much more efficient than linear search.
the last elements subscript is
n-1
____ are commonly used as size declarators
named constants
array elements can be treated as
ordinary variables of the same type as the array
When passing an array to function, it is common to
pass array size so that function knows how many elements to process
Use ____ member function to remove last element from vector:
pop_back
use ____ member function to add element to a full array or to an array that had no defined size
push_back
In C++ 11 you can use the ____ to display an array's contents
range-based for loop
Disadvantage(s) of using a binary search:
requires that array elements be sorted
Two-dimensional arrays are initialized
row-by-row
In a two dimensional array, the First declarator is number of ____.
rows
use ____ member to determine size of a vector
size
disadvantage of bubble sort-
slow for large arrays
Each element in an array is assigned a unique ____.
subscript
____ start at 0.
subscripts
In the definition: int tests [5]; What is the name of the array?
tests
the initialization list cannot exceed
the array size
the range based for loop automatically knows
the number of elements in an array
You can determine array size by
the size of the initialization list
if array is initialized with fewer initial values than the size declarator, the remaining elements will be set
to 0
(1) The size of an array is:
total number of bytes allocated for it
to define a function that takes an array parameter, ____
use empty [] for array argument
How do you pass an array to a function?
use the array name
Array
variable that can store multiple values of the same type
A data type defined in the Standard Template Library
vector
automatically adds space as more is needed-no need to determine size at definition
vector
can hold values of any type
vector
declare a vector with initial size of 30
vector <int> scores (30);
declare a vector to hold int element
vector <int> scores;
declare a vector initialized to size and contents of another vector
vector<int> finals(scores);
declare a vector with initial size of 30 and all elements set to 0
vector<int> scores (30,0);
sorting and searching algorithms can be applied to ____ as well as arrays.
vectors