BSearch(
const
vector<
int
> &A,
int
a,
int
b,
int
key)
{
if
(a > b)
return
b+1;
int
m = (a + b)/2
if
(A[m] == key)
return
m;
else if
(a == b)
return
–1;
else if
(A[m] < key)
return
BSearch(A, m+1, b, key);
else
// A[m] > key
return
BSearch(A, a, m-1, key);
}
Run time is proportional to the log of the number of elements

String Class Revisited
void GetWords(vector<string> &List)
{
List.resize(0);
string s;
while (cin >> s)
{
List.push_back(s);
}
}

Using GetWords()
Suppose standard input contains
A list of words to be read.
vector<string> A;
GetWords(A);
Would set
A
in the following manner:
A[0]: "A"
A[1]: "list"
A[2]: "of"
A[3]: "words"
A[4]: "to"
A[5]: "be"
A[6]: "read."

String Class As Container Class
A string can be viewed as a container because it holds a
sequence of characters
Subscript operator is overloaded for string objects
Suppose
t
is a string object representing
"purple"
Traditional
t
view
t: "purple"
Alternative view
t[0]: 'p'
t[1]: 'u'
t[2]: 'r'
t[3]: 'p'
t[4]: 'l'
t[5]: 'e'

Example
#include
<cctype>
using namespace
std;
...
string t = "purple";
t[0] = 'e';
t[1] = 'o';
cout << t << endl;
// t: people
for
(
int
i = 0; i < t.size(); ++i)
{
t[i] = toupper(t[i]);
}
cout << t << endl;
// t: PEOPLE

Reconsider A
Where
vector<string> A;
Is set in the following manner
A[0]: "A"
A[1]: "list"
A[2]: "of"
A[3]: "words"
A[4]: "to"
A[5]: "be"
A[6]: "read."

Counting o’s
The following counts number of o’s within
A
count = 0;
for (int i
= 0; i < A.size(); ++i)
{
for (int j = 0; A[i].size(); ++j)
{
if (A[i][j] == 'o')
{
++count;
}
}
}
To reference
j
th character of
A[i]
we need double subscripts
Size of
A[i]
Size of
A

Explicit Two-Dimensional List
Consider definition
vector< vector<int> > A;
Then
A
is a
vector< vector<int> >
It is a vector of vectors
A[i]
is a
vector<int>
i
can vary from
0
to
A.size() - 1
A[i][j]
is a
int
j
can vary from
0
to
A[i].size() - 1

Multi-Dimensional Arrays
Syntax
btype mdarray[size_1][size_2] ... [size_k]
Where
k - dimensional array
mdarray
:
array identifier
size_i
:
a positive constant expression
btype
:
standard type or a previously defined user type
and is the base type of the array elements
Semantics
mdarray
is an object whose elements are indexed by
a
sequence of
k
subscripts
the
i
-th subscript is in the range
0
...
size_i - 1

Memory Layout
Multidimensional arrays are laid out in row-major order
Consider
int M[2][4];
M
is two-dimensional array that consists of 2 subarrays each
with 4 elements.
2 rows of 4 elements
The array is assigned to a contiguous section of memory
The first row occupies the first portion
The second row occupies the second portion
...
...
M[0][0]
M[0][3]
--
--
M[1][0]
M[1][3]
--
--

Identity Matrix Initialization
const int MaxSize = 25;
float A[MaxSize][MaxSize];
int nr = PromptAndRead();
int nc = PromptAndRead();
assert((nr <= MaxSize) && (nc <= MaxSize));
for (int r = 0; r < nr; ++r)
{
for (int c = 0; c < nc; ++c)
{
A[r][c] = 0;
}
A[r][r] = 1;
}

Matrix Addition Solution
void
MatrixAdd(
const float
A[][MaxCols],
const float
B[][MaxCols],
float
C[][MaxCols],
int
m,
int
n)
{
for
(
int
i = 0; i < m; ++i )
{
for
(
int
j = 0; j < n; ++j) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
Notice only first
brackets are empty

Home works
Home works
Exercises
9.43
Exercises:
Read the section 9.11 of textbook (Page525~548)
carefully and complete the game Maze Runner
End of Chapter 9
End of Chapter 9

#### You've reached the end of your free preview.

Want to read all 105 pages?

- Winter '03
- Pro.Lei
- C++ Programming, Array, STL