Herwig Baumgartner B+U Architects Taipei Arts Center, Taipei
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science...
-
Upload
alyson-summers -
Category
Documents
-
view
223 -
download
1
Transcript of Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science...
Chapter 8ArraysPart II
J. H. Wang (王正豪 ), Ph. D.
Assistant Professor
Dept. Computer Science and Information Engineering
National Taipei University of Technology
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
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
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-4
Figure 8.14 Functions push and pop
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-5
Figure 8.14 Functions push and pop (cont’d)
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
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-7
Searching an Array
• Target search
• Linear search
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.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-9
Figure 8.15 Function That Searches for a Target Value in an Array
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-10
Exercise
• How to find ALL elements which match the target?
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.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-12
Figure 8.16 Trace of Selection Sort
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-13
Figure 8.17 Function select_sort
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-14
Exercise
• Implement the function get_min_range.
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];
]
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]
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-17
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-18
Figure 8.19 Function to Check Whether Tic-tac-toe Board Is Filled
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.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-20
Initialization of Multidimensional Arrays
• char tictac[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
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
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-22
Figure 8.20 Three-Dimensional Array enroll
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-23
Arrays with Several Dimensions (Cont’d)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-24
Arrays with Several Dimensions (Cont’d)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-25
Exercise
• Print the number of enrolled students for each grade.
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
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.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-28
Figure 8.21 Sales Analysis Output
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-29
Figure 8.22 Sales Analysis Main Function
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-30
Figure 8.23 Function scan_table and Helper Function initialize
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-31
Figure 8.23 Function scan_table and Helper Function initialize (cont’d)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-32
Figure 8.23 Function scan_table and Helper Function initialize (cont’d)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-33
Figure 8.24 Function display_table and Helper Function display_quarter
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 8-34
Figure 8.24 Function display_table and Helper Function display_quarter (cont’d)