Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.

Similar presentations


Presentation on theme: "Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of."— Presentation transcript:

1 Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

2 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

3 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

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-4 Figure 8.14 Functions push and pop

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-5 Figure 8.14 Functions push and pop (cont’d)

6 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 abcde will output edcba

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-7 Searching an Array Target search Linear search

8 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.

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-9 Figure 8.15 Function That Searches for a Target Value in an Array

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-10 Exercise How to find ALL elements which match the target?

11 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.

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-12 Figure 8.16 Trace of Selection Sort

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-13 Figure 8.17 Functio n select_ sort

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-14 Exercise Implement the function get_min_range.

15 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]; ]

16 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]

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-17

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-18 Figure 8.19 Function to Check Whether Tic-tac-toe Board Is Filled

19 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.

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-20 Initialization of Multidimensional Arrays char tictac[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };

21 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

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-22 Figure 8.20 Three-Dimensional Array enroll

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-23 Arrays with Several Dimensions (Cont’d)

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-24 Arrays with Several Dimensions (Cont’d)

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-25 Exercise Print the number of enrolled students for each grade.

26 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

27 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.

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-28 Figure 8.21 Sales Analysis Output

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-29 Figure 8.22 Sales Analysis Main Function

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-30 Figure 8.23 Function scan_table and Helper Function initialize

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-31 Figure 8.23 Function scan_table and Helper Function initialize (cont’d)

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-32 Figure 8.23 Function scan_table and Helper Function initialize (cont’d)

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-33 Figure 8.24 Function display_tabl e and Helper Function display_quar ter

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-34 Figure 8.24 Function display_table and Helper Function display_quarter (cont’d)


Download ppt "Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of."

Similar presentations


Ads by Google