1 / 30

Useful resources

Useful resources. Safari Tech Books Online Free access to 250 books from library Full access starts from $9.99/month MSDN Comprehensive MS products references www.cplusplus.com No-frills C++ references Key is to know how to ask the correct question. Chapters 9 & 10 Arrays.

kiara
Download Presentation

Useful resources

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Useful resources • Safari Tech Books Online • Free access to 250 books from library • Full access starts from $9.99/month • MSDN • Comprehensive MS products references • www.cplusplus.com • No-frills C++ references • Key is to know how to ask the correct question Chapters 9 & 10 Arrays & Strings

  2. Chapters 9 & 10 Arrays

  3. Simple vs. Structured Data Type • Simple data type • Can store only one value at a time • Structured data type • Each data item is a collection of other data items • Why need structured data type • If a program needs to operate on test scores of 50 students, should it declare 50 variables? How? Chapters 9 & 10 Arrays & Strings

  4. Arrays • Definition • A collection of a fixed number of components of the same data type • Declaration dataType arrayName[intExp]; e.g. int n[5]; • One dimensional Elements are arranged in a list form 4 Bytes Array size MUST be known when compiling n[1] n[2] n[3] n[4] n[0] Chapters 9 & 10 Arrays & Strings

  5. Arrays • Accessing array components (elements) arrayName[intExp] • Initialization duringdeclaration int a[3] = {2, 5, 8};int b[] = {2, 5, 8};int c[3] = {2}; //partial initint d[3] = {2, 5}; //partial init Index, Subscript • Non-negative int • Starts at 0 • Ends at size-1 Chapters 9 & 10 Arrays & Strings

  6. Arrays • Basic processing • Using loop to traverse the array • Initializing • Reading data • Printing • Finding sum and average • Largest/smallest element • No guard against out-of-bounds subscripts • No aggregate operation • What’s in the name of array?Base address of the array Chapters 9 & 10 Arrays & Strings

  7. Passing Arrays to Functions • By reference only • Syntax • Formal parameter must have [] • Actual parameter doesn’t have [] • Constant arrays as formal parameters • Values of const formal parameters are not allowed to change during function execution • Checked at compile-time • Function cannot return an array Chapters 9 & 10 Arrays & Strings

  8. Sequential Search • Algorithm • Starts from the first element • Traverses the entire array until • Finds the search term Or • Reaches the last element • Does not assume the array is sorted Chapters 9 & 10 Arrays & Strings

  9. Selection Sort • Algorithm Sort x[n] in ascending order • Find the smallest element in the entire array and swap it with x[0] • Find the smallest element in the unsorted portion (from x[1] to x[n-1]) of the array and swap it with x[1] • Find the smallest element in the unsorted portion (from x[2] to x[n-1]) of the array and swap it with x[2] • ... • Find the smallest element in the unsorted portion (from x[n-2] to x[n-1]) of the array and swap it with x[n-2] Chapters 9 & 10 Arrays & Strings

  10. Initial state 1st pass finds x[7] to be the smallest After the 1st pass Chapters 9 & 10 Arrays & Strings

  11. After the 2nd pass 5 7 24 30 25 62 45 16 65 50 After the 3rd pass 5 7 16 30 25 62 45 24 65 50 After the 4th pass 5 7 16 24 25 62 45 30 65 50 After the 5th pass 5 7 16 24 25 62 45 30 65 50 After the 6th pass 5 7 16 24 25 30 45 62 65 50 After the 7th pass 5 7 16 24 25 30 45 62 65 50 After the 8th pass 5 7 16 24 25 30 45 50 65 62 After the 9th pass 5 7 16 24 25 30 45 50 62 65 Chapters 9 & 10 Arrays & Strings

  12. Binary Search • Requires the array to be sorted • Algorithm • Search for key in x[n] (in ascending order) • Compare key with the middle element. if key is less than the middle element, search the lower half; otherwise, search the upper half • Repeat the previous step until • Key is found Or • Impossible to divide the array again Chapters 9 & 10 Arrays & Strings

  13. first=mid+1 mid last 5 7 16 24 25 30 45 50 62 65 mid first last=mid-1 5 7 16 24 25 30 45 50 62 65 Search for 30 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first mid last 5 7 16 24 25 30 45 50 62 65 Found!!! Chapters 9 & 10 Arrays & Strings

  14. first=mid+1 mid last 5 7 16 24 25 30 45 50 62 65 mid first last =mid-1 5 7 16 24 25 30 45 50 62 65 mid first =mid+1 last 5 7 16 24 25 30 45 50 62 65 =mid-1 last first 5 7 16 24 25 30 45 50 62 65 Search for 35 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first mid last 5 7 16 24 25 30 45 50 62 65 Not Found!!! Chapters 9 & 10 Arrays & Strings

  15. Two-Dimensional Arrays • Data are stored in table form Chapters 9 & 10 Arrays & Strings

  16. Two-Dimensional Arrays • Declaration int inStock[6][5]; • Initialization during declaration int inStock[6][5] = {{10, 7, 12, 10, 4}, {18, 11, 15, 17, 10}, ... ..., { 9, 4, 7, 12, 11}}; • Accessing inStock[0][0] inStock[5][4] • Passing to functions • By reference only • Number of columns must be specified when declaring a 2-d array as formal parameter void f(int stock[][5], int rowSize) { } Chapters 9 & 10 Arrays & Strings

  17. char[] vs. string • C-strings • character arrays terminated with the null character ‘\0’ • <cstring> • string data type • Not null-terminated • <string> char cname[5] = “John”; string sname = “John”; J o h n \0 J o h n Chapters 9 & 10 Arrays & Strings

  18. More on string data type • Some useful functions • +, length(), size(), find(), substr(), … • pp. 498 / pp. 1261-1264 • Array of strings using string type • Similar as other one-dimensional arrays of built-in type Chapters 9 & 10 Arrays & Strings

  19. Increase New customer Programming Example • Customer data • Customer ID • First name • Last name • Mileage balance • New transactions • Customer ID • First name • Last name • New mileage 1R67S34 Graham Greene 20000 34TS8R3 Frederick Forsyth 30000 K2L398D Tom Clancy 4000 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000 WMK85S3 Ken Follett 1000 Chapters 9 & 10 Arrays & Strings

  20. Algorithm • Read customer data into arrays • Read one new transaction • Process the new transaction • Repeat steps 2 and 3 until the end of the file • Save the updated customer data Chapters 9 & 10 Arrays & Strings

  21. Algorithm • Read customer data into array • Declare four parallel arrays, initialize # of customers to zero • Read string1 • Read string2 • Read string3 • Read integer • Assign three strings and one integer to the current position , the index of which is equal to the current # of customers, of the four parallel arrays • Increase # of customers by 1 • Repeat steps 2-7 until the end of the file • Read one new transaction • Process the new transaction • Repeat steps 2 and 3 until the end of the file • Save the updated customer data 1R67S34 Graham Greene 20000 34TS8R3 Frederick Forsyth 3000 K2L398D Tom Clancy 4000 Chapters 9 & 10 Arrays & Strings

  22. Algorithm • Read customer data into array • Read one new transaction • Read string1 • Read string2 • Read string3 • Read integer • Process the new transaction • Repeat steps 2 and 3 until the end of the file • Save the updated customer data 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000 WMK85S3 Ken Follett 1000 Chapters 9 & 10 Arrays & Strings

  23. Algorithm • Read customer data into array • Read one new transaction • Process the new transaction • Search for the id in id array • If found, increase the corresponding component of mileage array by the new mileage; otherwise, add a new component to the end of each of the four arrays and increase the # of customers by 1 • Repeat steps 2 and 3 until the end of the file • Save the updated customer data 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000 WMK85S3 Ken Follett 1000 Chapters 9 & 10 Arrays & Strings

  24. 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000WMK85S3 Ken Follett 1000 id fName lName mileage 0 “1R67S34” “Graham“ “Greene“ 20000 1 “34TS8R3” “Frederick” “Forsyth” 30000 2 “K2L398D“ “Tom” “Clancy” 4000 3 4 5 6 ... ... ... ... 99 Chapters 9 & 10 Arrays & Strings

  25. 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000WMK85S3 Ken Follett 1000 id fName lName mileage 0 “1R67S34” “Graham“ “Greene“ 20000 1 “34TS8R3” “Frederick” “Forsyth” 30000 2 “K2L398D“ “Tom” “Clancy” 4000 3 Search Update 4 5 6 ... ... ... ... 99 Chapters 9 & 10 Arrays & Strings

  26. 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000WMK85S3 Ken Follett 1000 id fName lName mileage 0 “1R67S34” “Graham“ “Greene“ 20000 1 “34TS8R3” “Frederick” “Forsyth” 31000 2 “K2L398D“ “Tom” “Clancy” 4000 3 Update 4 Search 5 6 ... ... ... ... 99 Chapters 9 & 10 Arrays & Strings

  27. 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000WMK85S3 Ken Follett 1000 id fName lName mileage 0 “1R67S34” “Graham“ “Greene“ 22000 1 “34TS8R3” “Frederick” “Forsyth” 31000 2 “K2L398D“ “Tom” “Clancy” 4000 3 4 5 Add new components 6 Search, not found ... ... ... ... 99 Chapters 9 & 10 Arrays & Strings

  28. 34TS8R3 Frederick Forsyth 1000 1R67S34 Graham Greene 2000WMK85S3 Ken Follett 1000 id fName lName mileage 0 “1R67S34” “Graham“ “Greene“ 22000 1 “34TS8R3” “Frederick” “Forsyth” 31000 2 “K2L398D“ “Tom” “Clancy” 4000 3 “WMK85S3” “Ken” “Follet” 1000 4 5 6 ... ... ... ... 99 Chapters 9 & 10 Arrays & Strings

  29. Algorithm • Read customer data into array • Read one new transaction • Process the new transaction • Repeat steps 2 and 3 until the end of the file • Save the updated customer data • Customer ID • A space • First name • A space • Last name • A space • mileage • A new line • Repeat steps 1-8 until the last customer Chapters 9 & 10 Arrays & Strings

  30. Performance • Binary search • Every iteration cuts the size of search by half • Two comparisons per iteration Chapters 9 & 10 Arrays & Strings

More Related