BSearch const vector int A int a int b int key if a b return b1 int m a b2 if

Bsearch const vector int a int a int b int key if a b

This preview shows page 93 - 105 out of 105 pages.

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
Image of page 93
String Class Revisited void GetWords(vector<string> &List) { List.resize(0); string s; while (cin >> s) { List.push_back(s); } }
Image of page 94
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."
Image of page 95
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'
Image of page 96
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
Image of page 97
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."
Image of page 98
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
Image of page 99
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
Image of page 100
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
Image of page 101
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] -- --
Image of page 102
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; }
Image of page 103
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
Image of page 104
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
Image of page 105

You've reached the end of your free preview.

Want to read all 105 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes