Professional Documents
Culture Documents
// constant
string
double
int
names[CLASSIZE];
grades[CLASSIZE*5];
list[200];
following example:
string dayNames [] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday","Friday", "Saturday"};
dayNames is an array with 7 elements of type string
not necessary to specify size (7), since the number of elements make
each other by =
LHS vector becomes the same as the RHS vector
// illegal
// legal array element assignment
parameter declaration
and we do not specify the array size in array parameter
however array size is generally passed as another
reference parameter
book)
Why did we use const in Print?
to avoid accidental changes in array list
F0 = 1
F1 = 1
Fn = Fn-1 + Fn-2
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
You can see many examples of Fibonacci numbers in nature
Arrays of structs
We can define vectors of structs
id
gpa
1250
name
lastname
struct student
{
unsigned int id;
string name, lastname;
double gpa;
};
id
1251
gpa
3.2
name
lastname
student class[11];
// a vector with 11 students
id
class[1].gpa = 3.2;
1260
name
10
lastname
gpa
Vector of struct
Example
define a struct for a track on a CD
track number and title are fields
define a vector for 10 tracks
shuffle these 10 tracks at random
see shuffle.cpp (in book, but this version is slightly
modified)
Write a function that takes an array of integers as parameter and returns true
if the array is sorted in ascending manner, false otherwise
may not process all array elements
Searching a vector
We can search for one occurrence, return true/false or the index
of occurrence
Search the vector starting from the beginning
Stop searching when match is found
count
Search entire vector
Similar to one occurrence search, but do not stop after first
occurrence
Counting search
int countmatches(const string[] a, int boyut,
const string & s)
// post: returns # occurrences of s in a
{
int count = 0;
int k;
for(k=0; k < boyut; k++)
{
if (a[k] == s)
{
count++;
}
}
return count;
}
first occurrence?
Collecting search
Collect the occurrences in another vector
void collect(const string[] a, int boyut,
string[] matches)
// pre: matches is empty
// post: matches contains all elements of a with
//
first letter 'A'
{
int k;
for(k=0; k < boyut; k++)
{
if (a[k].substr(0,1) == "A")
{
matches[k] = a[k];
}
}
}
Binary search
Alternative to sequential search for sorted vectors
If a vector is sorted we can use the sorted property to
we need two index values, low and high, for the search space
10
11
12 13
14
mid=7
mid=3
low=4
high=6
high=6
mid=5
=>
FOUND
high=14
10
11
12 13
14
mid=7
mid=3
high =14
high=6
low=4
high=6
mid=5
low=4 high=4
mid=4
low=5 high=4
// not in list
Sorting
One of the fundamental operations in Computer
Science
Given a randomly ordered array, sort it
ascending
descending
Selection Sort
N is the number of elements in vector/array
Find smallest element, move into 0th vector/array location
examine all N elements
0 .. N-1
1 .. N-1
2 .. N-1
Generalize
for kth element, 0 <= k <= N-2
- find the minimum between kth and last element (element
with index N-1) of array
- swap the kth element with the minimum one
non-decreasing order
Insertion Sort
Insert 1st element before or after 0th
first 2 sorted
proper location
first 3 sorted
Generalize
insert kth element (element with index k) within first
k elements
first k+1 sorted