chapter 8 arrays part ii j. h. wang ( 王正豪 ), ph. d. assistant professor dept. computer science...
TRANSCRIPT
![Page 1: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/1.jpg)
Chapter 8ArraysPart II
J. H. Wang (王正豪 ), Ph. D.
Assistant Professor
Dept. Computer Science and Information Engineering
National Taipei University of Technology
![Page 2: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/2.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-2
Stacks
• A stack is a data structure in which only the top element can be accessed.
• pop remove the top element of a stack
• push insert a new element at the top of the stack
![Page 3: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/3.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-3
Stacks (Cont’d)
• Example– char s[STACK_SIZE]; /* a stack of characters */– int s_top = -1; /* stack s is empty */– max_size: STACK_SIZE– STACK_EMPTY: a previously defined constant
macro
![Page 4: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/4.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-4
Figure 8.14 Functions push and pop
![Page 5: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/5.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-5
Figure 8.14 Functions push and pop (cont’d)
![Page 6: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/6.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-6
Stack Exercise
• Implement a program to output the input data in reverse order by stack.– For example, input
a b c d ewill output
e d c b a
![Page 7: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/7.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-7
Searching an Array
• Target search
• Linear search
![Page 8: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/8.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-8
Linear Search Algorithm
– Assume the target has not been found.– Start with the initial array element.
– repeat while the target is not found and there are more array elements
• if the current element matches the target– Set a flag to indicate that the target has been found.
• else– Advance to the next array element.
– if the target was found• Return the target index as the search result.
– else• Return -1 as the search result.
![Page 9: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/9.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-9
Figure 8.15 Function That Searches for a Target Value in an Array
![Page 10: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/10.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-10
Exercise
• How to find ALL elements which match the target?
![Page 11: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/11.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-11
Sorting an Array
• Selection sort
• ALGORITHM FOR SELECTION SORT– for each value of fill from 0 to n-2
• Find index_of_min, the index of the smallest element in the unsorted subarray list[fill] through list[n-1].
• if fill is not the position of the smallest element (index_of_min)
– Exchange the smallest element with the one at position fill.
![Page 12: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/12.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-12
Figure 8.16 Trace of Selection Sort
![Page 13: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/13.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-13
Figure 8.17 Function select_sort
![Page 14: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/14.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-14
Exercise
• Implement the function get_min_range.
![Page 15: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/15.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-15
Multidimensional Arrays
• Multidimensional array an array with two or more dimensions
• char tictac[3][3];
]
![Page 16: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/16.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-16
Multidimensional Arrays (Cont’d)
• In the declaration of a multidimensional array function parameter, only the first dimension, the number of rows, can be omitted.– char tictac[ ][3]
![Page 17: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/17.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-17
![Page 18: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/18.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-18
Figure 8.19 Function to Check Whether Tic-tac-toe Board Is Filled
![Page 19: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/19.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-19
Exercise
• Implement a Tic-Tac-Toe game, allowing to users to play in turn. This program will judge which one is winner.
![Page 20: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/20.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-20
Initialization of Multidimensional Arrays
• char tictac[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
![Page 21: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/21.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-21
Arrays with Several Dimensions
• assume that the college offers 100 (MAXCRS) courses at five different campuses for each grade of students
• be aware of the amount of memory space required by each large array in a program
![Page 22: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/22.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-22
Figure 8.20 Three-Dimensional Array enroll
![Page 23: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/23.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-23
Arrays with Several Dimensions (Cont’d)
![Page 24: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/24.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-24
Arrays with Several Dimensions (Cont’d)
![Page 25: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/25.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-25
Exercise
• Print the number of enrolled students for each grade.
![Page 26: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/26.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-26
Common Programming Errors
• subscript-range error.– int celsius[100];– Reference to celsius[150] may cause an error
message such as:• access violation at line no. 28
– In many situations, however, no run-time error message will be produced
– entirely the responsibility of the programmer
![Page 27: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/27.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-27
Common Programming Errors (Cont’d)
• Range for both the initial and the final values of the loop control variable.
• When using arrays as arguments to functions, be careful not to apply the address-of operator to just the array name.– Consider &celsius[10] and &celsius
• Use int z[ ] for array parameters to assist readers of your code.
![Page 28: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/28.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-28
Figure 8.21 Sales Analysis Output
![Page 29: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/29.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-29
Figure 8.22 Sales Analysis Main Function
![Page 30: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/30.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-30
Figure 8.23 Function scan_table and Helper Function initialize
![Page 31: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/31.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-31
Figure 8.23 Function scan_table and Helper Function initialize (cont’d)
![Page 32: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/32.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-32
Figure 8.23 Function scan_table and Helper Function initialize (cont’d)
![Page 33: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/33.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-33
Figure 8.24 Function display_table and Helper Function display_quarter
![Page 34: Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of](https://reader030.vdocuments.mx/reader030/viewer/2022020106/56649ecf5503460f94bdcbbc/html5/thumbnails/34.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-34
Figure 8.24 Function display_table and Helper Function display_quarter (cont’d)