Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Similar presentations


Presentation on theme: "Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )"— Presentation transcript:

1 Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-2 Arrays Simple data types use a single memory cell to store variable. ArrayArray is a data structure which groups two or more adjacent memory cells. Syntax: element-type aname[size]; element-type aname[size]={initialization lsit}; e.g., double x[8]; char name[4];

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-3 The Elements of Array array subscriptWe use the array subscript to specify the array element being manipulated, which ranges from zero to one less than the number of size. Array subscript Array size

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-4 Array Initialization We can initialize an array in its declaration. We can omit the size of an array that is being fully initialized since the size can be deduced from the initailization list. e.g., char vowels[] = {‘A’, ‘E’, ’I’, ’O’, ’U’}; int prime[]={2, 3, 5, 7, 11, 13, 17, 19, 23};

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-5 Using for Loops for Sequential Access The elements of an array are usually processed by for loops, because –the elements are sequentially stored in the memory, and –the for loop is suitable for sequential manipulation. e.g., int square[SIZE], i; for(i=0; i<SIZE; i++) square[i]=i*i;

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-6 Using Array Elements as Function Arguments (1/2) Suppose that we have a function prototype as shown below. –void do_it(double arg_1, double *arg2_p, double *arg3_p); Let x be an array of type double elements. We can call the function do_it as follows. –do_it(x[0], &x[1], &x[2]); The modification on arg2_p and arg3_p will change the values of x[1] and x[2].

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-7 Using Array Elements as Function Arguments (2/2) arg_1 has a copy of the value from x[0] arg_3 is a pointer pointed to the memory of x[2].

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-8 Array Arguments We can also pass an entire array as the input argument for a function. However, the function does not copy the array but just manipulates the original array. –An assignment to one of the array elements by a statement in the function changes the contents of the original array.

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-9 An Example of Array Arguments We have the flexibility to pass an array of any size to the function. –Because C does not allocate any memory space for the array, and thus the compiler does not need to know the size. int list[] is the declaration for passing an int array

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-10 Data Areas After Calling fill_array (x, 5, 1); The value of each element in the original array are changed to 1.

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-11 Alternative Formats of Array Arguments fill_array(x, 5, 1) is the same as fill_array(&x[0], 5, 1). –This call may lead to misunderstanding of the reader. In the declaration for function fill_array, we can declare the array argument as either –int list[]int *list –int list[] or int *list. If you wish to prevent the modification of the array argument, you can write const –fill_array(const int list[], …)

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-12 Returning an Array Result The add_arrays function computes the sum of corresponding elements in arrays ar1 and ar2, and then stores results into ar3. Input array arguments Output array arguments

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-13 Memory for calling add_arrays(x, y, x_plus_y) Indirectly read the values of array x Indirectly read the values of array y Indirectly write the values of x+y into x_plus_y

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-14 Stacks stackThe stack is a data structure in which only the top element can be accessed. There are two operations which are associated with the stack. –Pop –Pop: remove the top element of a stack. –Push –Push: insert a new element at the top of the stack. abcabc bcbc dbcdbc poppush d

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-15 Stacks The usage of stack is commonly in daily life. –e.g., Dishes used by guests in a buffet restaurant. Stacks are the frequently used data structure in programming. –e.g., Before jumping to a sub-function, the variables in the current function are pushed into a stack. ArrayArray is one of the approaches to implement the stack.

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-16 The Implementation of a Stack by an Array (push) Suppose we have the following global variables. int size = 5; char stack[size]; int count=0; The push function: void push(char item){ if(count < size){ stack[count]=item; count++; } }

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-17 The Implementation of a Stack by an Array (pop) char pop(void){ char temp; if(count > 0){ temp=stack[count]; count--; }else{ printf(“Stack empty!\n”); return 0; } return temp; }

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-18 Searching and Sorting an Array Sometimes we may need to –search a particular value in an array, or –sort an array to rearrange the array elements in numerical order. Commonly used searching methods: –Sequential search, binary search, etc. Commonly used sorting methods: –Selection sort, bubble sort, quick sort, etc.

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-19 Sequential Search in an Array target is the value we want to locate in the array Traverse the array to look for the value identical to target.

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-20 Selection Sort in an Array Selection sort is an intuitive sorting algorithm. –Find the index of the smallest element in the array. –Swap the smallest element with the first element. –Repeat the above steps for the 2 nd, 3 rd, …, smallest elements.

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-21 An Function for Selection Sort Suppose we have a function that can find the minimal element. Swap the minimal element with the previous larger element.

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-22 Multidimensional Arrays Multidimensional arrays stand for the arrays with two or more dimensions. Syntax for normal usage: element-type aname[size 1 ][size 2 ]…[size n ]; Syntax for parameter in function prototype: element-type aname[][size 2 ]…[size n ] e.g., char tictac[3][3]; –A 2-dimensional array with three rows and three columns. e.g., tictac[][3]; –The declaration for the function prototype.

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-23 The Memory Allocated for tictac[3][3] The memory allocated for array tictac[3][3] is shown below. 3*3=9The number of total allocated element is 3*3=9 elements.

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-24 Initialization of Multidimensional Arrays The multidimensional array can also be initialized in declarations. The initialized values are grouped in rows. –e.g., char tictac[3][3]={{‘a’, ’b’, ’c’}, {‘d’, ’e’, ’f’}, {‘ ’,‘ ’,‘ ’}}; We can also declare arrays with more dimensions. –e.g., declare a 3-dimensional array. int enroll[course][campus][cls_rank];

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-25 The Memory Allocated for enroll[100][5][4] enroll[10 0][5][4] consists of 100*5*4 =2000 elements.

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-26 Homework #5 (1/2) Write a function that computes the average grades and sorts the grades of two classes. Your program should input two lists of grades for each class. Your program should output: (1) average grades for each class; (2) a list of sorted grades of two classes. You can use the sorting function in p.401 in the text book. You can either assume the number of students in each class is fixed or prompt the user to input the number.

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-27 Homework #5 (2/2) e.g., Please input grades of 1 st class: 55 45 65 85 75 Please input grades of 2 nd class: 80 70 50 60 40 Output: The average grades of 1 st class is: 65 The average grades of 2 nd class is: 60 The sorted grades of two classes are: 40 45 50 55 60 65 70 75 80 85 You may need to use the array concepts in this chapter.


Download ppt "Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )"

Similar presentations


Ads by Google