Chapter 8 Arrays Dr Jiungyao Huang Dept Comm

  • Slides: 75
Download presentation
Chapter 8 Arrays Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail

Chapter 8 Arrays Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu. edu. tw TA: 鄭筱親 陳昱豪 Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 1 -

本章重點 v. Declaring and referencing arrays v. When the array name with no subscript,

本章重點 v. Declaring and referencing arrays v. When the array name with no subscript, it represents an address, the address of the initial array element (陣列與指標的曖昧關係) vconst 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 2

Outline v 8. 1 DECLARING AND REFERENCING ARRAYS v 8. 2 ARRAY SUBCRIPTS v

Outline v 8. 1 DECLARING AND REFERENCING ARRAYS v 8. 2 ARRAY SUBCRIPTS v 8. 3 USING FOR LOOPS FOR SEQUENTIAL ACCESS v 8. 4 USING ARRAY ELEMENTS AS FUNCTION ARGUMENTS v 8. 5 ARRAY ARGUMENTS v 8. 6 SEARCHING AND SORTING AN ARRAY v 8. 7 MULTIDIMENSIONAL ARRAYS v 8. 8 ARRAY PROCESSING ILLUSTRATED ²CASE STUDY: v. ANALYSIS OF SALES DATA v 8. 9 COMMON PROGRAMMING ERRORS 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 3

8. 1 DECLARING AND REFERENCING ARRAYS v To group data items together is more

8. 1 DECLARING AND REFERENCING ARRAYS v To group data items together is more efficient and comprehensible v Data structure ² A composite of related data items stored under the same name v Array ² A collection of data items of the same type v Array element ² A data item that is part of an array v Subscripted variable ² A variable followed by a subscript in brackets, designating an array element v Array subscript ² A value or expression enclosed in brackets after the array name, specifying which array element to access 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 4

Figure 8. 1 The Eight Elements of Array x 中正大學通訊 程系 潘仁義老師 Advanced Network

Figure 8. 1 The Eight Elements of Array x 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 5

PARALLEL ARRAYS v. Two or more arrays with the same number of elements used

PARALLEL ARRAYS v. Two or more arrays with the same number of elements used for storing related information about a collection of data objects id [0] id [1] id [2] id [49] 5503 4556 5691 … 9146 gpa [0] gpa [1] gpa [2] gpa [49] 2. 71 3. 09 2. 98 … 1. 92 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 6

EXAMPLE 8. 3 #define NUM_QUEST 10 #define NUM_CLASS_DAYS 5 typedef enum {monday, tuesday, wednesday,

EXAMPLE 8. 3 #define NUM_QUEST 10 #define NUM_CLASS_DAYS 5 typedef enum {monday, tuesday, wednesday, thursday, friday} class_days_t; char answer [NUM_QUEST]; int score [NUM_CLASS_DAYS]; 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 7

Figure 8. 2 Arrays answer and score 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab

Figure 8. 2 Arrays answer and score 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 8

ARRAY INITIALIZATION v. Initialize a 25 -elements array with the prime numbers less than

ARRAY INITIALIZATION v. Initialize a 25 -elements array with the prime numbers less than 100 int prime_lt_100 [ ] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 9

ARRAY DECLARATION v. SYNTAX: ²element-type aname [size]; v. EXAMPLE: #define A_SIZE 5 double a[A_SIZE];

ARRAY DECLARATION v. SYNTAX: ²element-type aname [size]; v. EXAMPLE: #define A_SIZE 5 double a[A_SIZE]; char vowels [ ] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 10

8. 2 ARRAY SUBCRIPTS v. SYNTAX: ²aname [subscript] v. EXAMPLE: int prime[] = {2,

8. 2 ARRAY SUBCRIPTS v. SYNTAX: ²aname [subscript] v. EXAMPLE: int prime[] = {2, 3, 5, 7} , i = 1; printf(“%d”, prime[i]); /*result is 3*/ 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 11

8. 3 USING FOR LOOPS FOR SEQUENTIAL ACCESS v. Use an indexed for loop,

8. 3 USING FOR LOOPS FOR SEQUENTIAL ACCESS v. Use an indexed for loop, a counting loop whose loop control variable runs from zero to one less than the array size. v. Use the loop counter as an array index (subscript) gives access to each array element in turn. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 12

EXAMPLE 8. 7 v. The array square will be used to store the squares

EXAMPLE 8. 7 v. The array square will be used to store the squares of the integers 0 through 10. v. The name SIZE has been defined to be 11. vint square[SIZE], i; vfor (i=0; i<SIZE; ++i) square [i]= i * i; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 0 1 4 9 16 25 49 36 64 81 100 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 13

STATISTICAL COMPUTATIONS USING ARRAYS v. One common use of arrays is for storage of

STATISTICAL COMPUTATIONS USING ARRAYS v. One common use of arrays is for storage of a collection of related data values. v. Once the values are stored, we can perform some simple statistical computations. v. Figure 8. 3 first for loop vfor (i=0; i<MAX_ITEM; ++i); scanf(“%lf”, &x[i]); 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 14

Figure 8. 3 Program to Print a Table of Differences 中正大學通訊 程系 潘仁義老師 Advanced

Figure 8. 3 Program to Print a Table of Differences 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 15

Figure 8. 3 Program to Print a Table of Differences (cont’d) 中正大學通訊 程系 潘仁義老師

Figure 8. 3 Program to Print a Table of Differences (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 16

8. 4 USING ARRAY ELEMENTS AS FUNCTION ARGUMENTS v. EXAMPLE 8. 9: ²The function

8. 4 USING ARRAY ELEMENTS AS FUNCTION ARGUMENTS v. EXAMPLE 8. 9: ²The function prototype below shows one type double input parameter(arg_1) and two double * output parameters(arg 2_p and arg 3_p). ²Figure 8. 4 vvoid do_it (double arg_1, double *arg 2_P, double *arg 3_p); vdo_it (p, &q, &r); vdo_it (x[0], &x[1], &x[2]); 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 17

Figure 8. 4 Data Area for Calling Module and Function do_it 中正大學通訊 程系 潘仁義老師

Figure 8. 4 Data Area for Calling Module and Function do_it 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 18

8. 5 ARRAY ARGUMENTS v. Formal array parameters ²When an array name with no

8. 5 ARRAY ARGUMENTS v. Formal array parameters ²When an array name with no subscript appears in the argument list of a function call, the address of the initial array element is actually stored in the function’s corresponding formal parameter. ²The function manipulates the original array, not its own personal copy, so an assignment changes the original array 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 19

EXAMPLE 8. 10 v The statement list[i]=in_value stores in_value in element i of its

EXAMPLE 8. 10 v The statement list[i]=in_value stores in_value in element i of its actual array argument v In function fill_array, the array parameter is declared as int list[ ] v The argument declaration does not indicate the size of list. v Since we do not provide the size, we have the flexibility to pass to the function an array of any number of integers. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 20

Figure 8. 5 Function fill_array 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 21

Figure 8. 5 Function fill_array 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 21

ARGUMENT CORRESPONDENCE FOR ARRAY PARAMETERS vfill_array(y, 10, num) ²store the value num in the

ARGUMENT CORRESPONDENCE FOR ARRAY PARAMETERS vfill_array(y, 10, num) ²store the value num in the ten elements of array y vfill_array(x, 5, 1) ²store 1 in five elements of array x v. Because C stores the address of the type int variable x[0] in list, the call of fill_array ²fill_array(&x[0], 5, 1) execute exactly like the call above 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 22

Figure 8. 6 Data Areas Before Return from fill_array (x, 5, 1); 中正大學通訊 程系

Figure 8. 6 Data Areas Before Return from fill_array (x, 5, 1); 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 23

USE OF *list INSTEAD OF list[ ] IN A FORMAL PARAMETER LIST v. Use

USE OF *list INSTEAD OF list[ ] IN A FORMAL PARAMETER LIST v. Use either parameter declaration ²int list[ ] ²int *list v. The first tells us that the actual argument is an array. v. The second declaration would be equally valid for an integer array parameter. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 24

EXAMPLE 8. 11 v. Function get_max in Fig. 8. 7 can be called to

EXAMPLE 8. 11 v. Function get_max in Fig. 8. 7 can be called to find the largest value in an array. v. Use list as an array input parameter. v. If x is a five-element array of type int values, the statement ²x_large = get_max(x, 5) used to search for largest element 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 25

Figure 8. 7 Function to Find the Largest Element in an Array 中正大學通訊 程系

Figure 8. 7 Function to Find the Largest Element in an Array 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 26

ARRAY AS INPUT ARGUMENTS v. ANSI C provides a qualifier in the declaration of

ARRAY AS INPUT ARGUMENTS v. ANSI C provides a qualifier in the declaration of the array formal parameter in order to notify the C compiler that the array is only an input to the function and that the function does not intend to modify the array. ²const 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 27

ARRAY INPUT PARAMETER v SYNTAX: ²const element-type array-name [ ] ²const element-type *array-name v

ARRAY INPUT PARAMETER v SYNTAX: ²const element-type array-name [ ] ²const element-type *array-name v EXAMPLE: int get_min_sub(const double data[ ], int data_size) { int i, small_sub; small_sub=0; for (i=1; i<data_size; ++i) if (data[i] < data[small_sub]) small_sub=i; return(small_sub); } 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 28

Figure 8. 8 Diagram of a Function That Computes an Array Result In C,

Figure 8. 8 Diagram of a Function That Computes an Array Result In C, it is not legal for a function’s return type to be an array. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 29

EXAMPLE 8. 12 v. The sum of arrays ar 1 and ar 2 is

EXAMPLE 8. 12 v. The sum of arrays ar 1 and ar 2 is defined as arsum such that arsum[i] is equal to ar 1[i] + ar 2[i] for each subscript i. vn specifies how many corresponding elements are summed. v. The formal parameter list declaration ²const double ar 1[ ] ²const double ar 2[ ] ²double arsum[ ] ²int n 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 30

Figure 8. 9 Function to Add Two Arrays 中正大學通訊 程系 潘仁義老師 Advanced Network Technology

Figure 8. 9 Function to Add Two Arrays 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 31

Figure 8. 10 Function Data Areas for add_arrays(x, y, x_plus_y, 5); 中正大學通訊 程系 潘仁義老師

Figure 8. 10 Function Data Areas for add_arrays(x, y, x_plus_y, 5); 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 32

x_plus_y after call to add_arrays v. We assume that a calling function has declared

x_plus_y after call to add_arrays v. We assume that a calling function has declared three five-element arrays x, y, and x_plus_y and has filled x and y with data. v. The call ²add_arrays(x, y, x_plus_y, 5) lead to & Not used x_plus_y after call to add_arrays 3. 5 6. 7 4. 7 9. 1 12. 2 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 33

PARTIALLY FILLED ARRAYS v. In order to reuse an array for processing more than

PARTIALLY FILLED ARRAYS v. In order to reuse an array for processing more than one data set, the programmer declares an array large enough to hold the largest data set anticipated. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 34

EXAMPLE 8. 13 v. The purpose of function fill_to_sentinel is to fill a type

EXAMPLE 8. 13 v. The purpose of function fill_to_sentinel is to fill a type double array with data until the designated sentinel value is encountered in the input data. (Figure 8. 11) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 35

Figure 8. 11 Diagram of Function fill_to_sentinel 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab

Figure 8. 11 Diagram of Function fill_to_sentinel 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 36

Figure 8. 12 Function Using a Sentinel. Controlled Loop to Store Input Data in

Figure 8. 12 Function Using a Sentinel. Controlled Loop to Store Input Data in an Array 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 37

Figure 8. 13 Driver for Testing fill_to_sentinel 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab

Figure 8. 13 Driver for Testing fill_to_sentinel 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 38

STACKS v. Stack ²A data structure in which only the top element can be

STACKS v. Stack ²A data structure in which only the top element can be accessed. vpop ²Remove the top element of a stack vpush ²Insert a new element at the top of the stack v應用 ²老鼠走迷宮,算式計算,呼叫副程式,改遞迴 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 39

EXAMPLE vchar s[STACK_SIZE]; v. Int s_stop = -1; vpush(s, ‘ 2’, &s_stop, STACK_SIZE); vpush(s,

EXAMPLE vchar s[STACK_SIZE]; v. Int s_stop = -1; vpush(s, ‘ 2’, &s_stop, STACK_SIZE); vpush(s, ‘+’, &s_stop, STACK_SIZE); vpush(s, ‘C’, &s_stop, STACK_SIZE); C + 2 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 40

Figure 8. 14 Functions push and pop 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab

Figure 8. 14 Functions push and pop 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 41

Figure 8. 14 Functions push and pop (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology

Figure 8. 14 Functions push and pop (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 42

8. 6 SEARCHING AND SORTING AN ARRAY v. Array search ²In order to search

8. 6 SEARCHING AND SORTING AN ARRAY v. Array search ²In order to search an array, we need to know the search target. ²The search loop should be exited when the target value is found that the process is called a linear search. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 43

ALGORITHM 1. Assume the target has not been found. 2. Start with the initial

ALGORITHM 1. Assume the target has not been found. 2. Start with the initial array element. 3. Repeat while the target is not found and there are more array elements 4. if the current element matches the target 5. Set a flag to indicate that the target has been found else 6. Advance to the next array element 7. if the target was found 8. Return the target index as the search result else 9. Return -1 as the search result 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 44

Figure 8. 15 Function That Searches for a Target Value in an Array 中正大學通訊

Figure 8. 15 Function That Searches for a Target Value in an Array 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 45

SORTING AN ARRAY ALGORITHM FOR SELECTING SORT 1. for each value of fill from

SORTING AN ARRAY ALGORITHM FOR SELECTING SORT 1. for each value of fill from 0 to n-2 2. Find index_of_min, the index of the smallest in the unsorted subarray list[fill] through list[n 1] 3. if fill is not the position of the smallest element (index_of_min) 4. Exchange the smallest with the one at position fill 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 46

Figure 8. 16 Trace of Selection Sort 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab

Figure 8. 16 Trace of Selection Sort 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 47

Figure 8. 17 Function select_sort 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 48

Figure 8. 17 Function select_sort 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 48

8. 7 MULTIDIMENSIONAL ARRAYS v. Multidimensional array ²An array with two or more dimensions

8. 7 MULTIDIMENSIONAL ARRAYS v. Multidimensional array ²An array with two or more dimensions v. A two-dimensional object that many are familiar with is a tic-tac-toe board. v. The array declaration char tictac[3][3]; 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 49

Figure 8. 18 A Tic-tac-toe Board Stored as Array tictac 中正大學通訊 程系 潘仁義老師 Advanced

Figure 8. 18 A Tic-tac-toe Board Stored as Array tictac 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 50

Multidimensional Array Declaration v. SYNTAX: element-type aname [size 1] [size 2] …[sizen] element-type aname

Multidimensional Array Declaration v. SYNTAX: element-type aname [size 1] [size 2] …[sizen] element-type aname [ ] [size 2] …[sizen] // prototype v. EXAMPLES: double table [NROWS][NCOLS]; void process_matrix(int in[ ][4], int out[ ][4], int nrows) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 51

Initialization of Multidimensional Arrays v. You can initialize multidimensional arrays in their declarations just

Initialization of Multidimensional Arrays v. You can initialize multidimensional arrays in their declarations just like you initialize onedimensional arrays. v. Instead of listing all table values in one list, the values are usually grouped by rows. v. Example: char array[2][2] = { { 'a‘ , ‘b'}, {‘c', ‘d'} }; 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 52

EXAMPLE 8. 15 v. Function filled checks whether a tic-tac-toe board is completely filled.

EXAMPLE 8. 15 v. Function filled checks whether a tic-tac-toe board is completely filled. v. If board contains no cells with the value ‘ ‘ , the function returns 1 for true; otherwise, it returns 0 for false. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 53

Figure 8. 19 Function to Check Whether Tic-tac-toe Board Is Filled 中正大學通訊 程系 潘仁義老師

Figure 8. 19 Function to Check Whether Tic-tac-toe Board Is Filled 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 54

ARRAYS WITH SEVERAL DIMENSIONS v. The array enroll declared ²int enroll [MAXCRS][5][4] campus course

ARRAYS WITH SEVERAL DIMENSIONS v. The array enroll declared ²int enroll [MAXCRS][5][4] campus course year campus course 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 55

Figure 8. 20 Three-Dimensional Array enroll 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 56

Figure 8. 20 Three-Dimensional Array enroll 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 56

8. 8 ARRAY PROCESSING ILLUSTRATED CASE STUDY:Analysis of Sales Data(1/4) (自修) v Step 1:

8. 8 ARRAY PROCESSING ILLUSTRATED CASE STUDY:Analysis of Sales Data(1/4) (自修) v Step 1: Problem ²The sales manager of your company needs a sales analysis program to track sales performance by salesperson and by quarter. ²The program will read all sales transactions from a text file. ²The data for each transaction will be the salesperson’s number, the quarter in which the sale took place, and the sales amount. ²The sales transactions are in no particular order. ²After scanning all sales transactions, the program should display a table in the form shown in Fig. 8. 21, which totals by person and by quarter. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 57

Figure 8. 21 Sales Analysis Output 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 58

Figure 8. 21 Sales Analysis Output 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 58

CASE STUDY:Analysis of Sales Data(2/4) Step 2: Analysis v Data requirements ²New type vquarter_t

CASE STUDY:Analysis of Sales Data(2/4) Step 2: Analysis v Data requirements ²New type vquarter_t {fall, winter, spring, summer} ²Problem constants v. NUM_SALES_PEOPLE 5 v. NUM_QUARTERS 4 ²Problem inputs v. Sales transaction file vdouble sales [NUM_SALES_PEOPLE][NUM_QUARTERS] ²Problem outputs vdouble person_totals [NUM_SALES_PEOPLE] vdouble quarter_totals [NUM_QUARTERS] 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 59

CASE STUDY:Analysis of Sales Data(3/4) Step 3: Design ²Initial algorithm 1. Scan sales data,

CASE STUDY:Analysis of Sales Data(3/4) Step 3: Design ²Initial algorithm 1. Scan sales data, posting by salesperson and quarter, and returning a value to show success or failure of the data scan. 2. if the data scan proceeded without error 3. Compute salesperson totals (row sums) 4. Compute quarterly totals (column sums) 5. Display the sales table along with the row and column sums 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 60

CASE STUDY:Analysis of Sales Data(4/4) Step 4: Implementation (Figure 8. 22、Figure 8. 23) Step

CASE STUDY:Analysis of Sales Data(4/4) Step 4: Implementation (Figure 8. 22、Figure 8. 23) Step 5: Testing 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 61

Figure 8. 22 Sales Analysis Main Function 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab

Figure 8. 22 Sales Analysis Main Function 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 62

Figure 8. 23 Function scan_table and Helper Function initialize 中正大學通訊 程系 潘仁義老師 Advanced Network

Figure 8. 23 Function scan_table and Helper Function initialize 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 63

Figure 8. 23 Function scan_table and Helper Function initialize (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced

Figure 8. 23 Function scan_table and Helper Function initialize (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 64

Figure 8. 23 Function scan_table and Helper Function initialize (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced

Figure 8. 23 Function scan_table and Helper Function initialize (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 65

Figure 8. 24 Function display_table and Helper Function display_quarter 中正大學通訊 程系 潘仁義老師 Advanced Network

Figure 8. 24 Function display_table and Helper Function display_quarter 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 66

Figure 8. 24 Function display_table and Helper Function display_quarter (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced

Figure 8. 24 Function display_table and Helper Function display_quarter (cont’d) 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 67

Common Programming Errors (1/4) v. The most common error in using arrays is a

Common Programming Errors (1/4) v. The most common error in using arrays is a subscript-range error. v. Ecample: int array[10]; array[10] = 10; /*the value is */ /* from 0 to 9 */ 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 68

Common Programming Errors (2/4) v. If a subscript-range error occurs inside an indexed loop,

Common Programming Errors (2/4) v. If a subscript-range error occurs inside an indexed loop, verify that the subscript is in range for both the initial and the final values of the loop control variable. v. Example: int array[5], i; for ( i = 1; i <= 5; i++) /*range is 0 to 4*/ …. . 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 69

Common Programming Errors (3/4) v. Not to apply the address-of operator to the array

Common Programming Errors (3/4) v. Not to apply the address-of operator to the array name even if the array is an output argument. v. Example: int array[5]; go(&array); /*Wrong using*/ 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 70

Common Programming Errors (4/4) v. Use the correct forms for declaring array input and

Common Programming Errors (4/4) v. Use the correct forms for declaring array input and output parameters in function prototypes. v. Example: int *z /*It could represent a single integer output*/ /*parameter or an integer array parameter. */ int z[ ] 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 71

Chapter Review(1/2) v An array is a data structure used to store a collection

Chapter Review(1/2) v An array is a data structure used to store a collection of data items of the same type. v An individual array element is referenced by replacing immediately after the array name a square-bracketed subscript for each dimension. v The initial element of a one-dimensional array x is referenced as x[0]. If x has n elements, the final element is referenced as x[n-1]. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 72

Chapter Review(2/2) v For an array declared as a local variable, space is allocated

Chapter Review(2/2) v For an array declared as a local variable, space is allocated in the function data area for all the array elements. v For an array declared as a function parameter, space is allocated in the function data area for only the address of the initial element of the actual argument passed. v The name of an array with no subscript always refers to the address of the initial array element. v A function that creates an array result should require the calling module to pass an output argument array in which to store the result. 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 73

Question? 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 74

Question? 中正大學通訊 程系 潘仁義老師 Advanced Network Technology Lab 74