Presentation is loading. Please wait.

Presentation is loading. Please wait.

20061206 chap8 Chapter 8 Arrays (Hanly). 20061206 chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.

Similar presentations


Presentation on theme: "20061206 chap8 Chapter 8 Arrays (Hanly). 20061206 chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a."— Presentation transcript:

1 20061206 chap8 Chapter 8 Arrays (Hanly)

2 20061206 chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a composite of related data items stored under the same name. Array : a collection of data items of the same type.

3 20061206 chap8 3 Declaring & Referencing Arrays An array is a collection of two or more adjacent elements, that are associated with a particular symbolic name. Declare both the name of the array and the number of cells. double x[8]; Subscripted variable: a variable followed by a subscript in brackets, designating an array element. x[0] read as x sub zero

4 20061206 chap8 4

5 5 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 initialization list. e.g., char vowels[] = {‘A’,‘E’, ’I’, ’O’, ’U’}; int prime[]={2, 3, 5, 7, 11, 13, 17, 19, 23};

6 20061206 chap8 6 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 the for loop is suitable for sequential manipulation. e.g., int square[SIZE], i; for(i=0; i<SIZE; i++) square[i]=i*i;

7 20061206 chap8 7 Statistical Computations Using Arrays One common use of arrays is for storage of a collection of related data values. Once the values are stored, we can perform some simple statistical computations.

8 20061206 chap8 8 Program to Print a Table of Differences (Demo-Fig 8.3)

9 20061206 chap8 9

10 10 Using Array Elements as Function Arguments 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].

11 20061206 chap8 11 Data Area for Calling Module and Function do_it

12 20061206 chap8 12 Array Arguments We can write functions that have arrays as arguments. Such functions can manipulate some, or all, of these elements corresponding to an actual array argument. We can 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

13 20061206 chap8 13 Example: fill_array (Fig 8.5) 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.

14 20061206 chap8 14 Argument Correspondence for Array Parameters Data Areas Before Return from fill_array (x, 5, 1);

15 20061206 chap8 15 Alternative Formats of Array Arugments 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[] or int *list. If you wish to prevent the modification of the array argument, you can write const fill_array(const int list[], …)

16 20061206 chap8 16 Example: Find the Largest Element in an Array

17 20061206 chap8 17 Returning an Array Result In C, it is not legal for a function ’ s return type to be an array. ~~~ Chapter 6 ~~~ Function to add two arrays.

18 20061206 chap8 18 Function Data Areas for add_arrays(x, y, x_plus_y, 5);

19 20061206 chap8 19 Stacks stack The stack is a data structure in which only the top element can be accessed. The plates stored in the spring-loaded device in a buffet line perform like a stack. A customer always takes the top plate; when a plate is removed, the plate beneath it moves to the top.

20 20061206 chap8 20 Stacks Popping the stack: remove a value from a stack. Pushing it onto stack: store an item in a stack. Array is one of the approaches to implement stacks. abcabc bcbc dbcdbc poppush d

21 20061206 chap8 21 Push: Insert a new element at the top

22 20061206 chap8 22 Pop: remove a value

23 20061206 chap8 23 Searching and Sorting an Array Two common problems in processing arrays Searching an array to determine the location of a particular value. Sorting an array to rearrange the array elements in numerical order. Examples Search an array of student exam scores to determine which student, if any, got a particular score. Rearrange the array elements in increasing order by score. We can locate several different scores in the array.

24 20061206 chap8 24 Array Search Linear search

25 20061206 chap8 25 Array Search

26 20061206 chap8 26 Sorting 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.

27 20061206 chap8 27 Function select_sort (Fig 8.17)

28 20061206 chap8 28 Multidimensional Arrays An array with two or more dimensions The memory allocated for array tictac[3][3] is shown below. 3*3=9 The number of total allocated element is 3*3=9 elements.

29 20061206 chap8 29 Multidimensional Array Declaration

30 20061206 chap8 30 Check Whether Tic-tac-toe Board Is Filled

31 20061206 chap8 31 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];

32 20061206 chap8 32 Three-Dimensional Array enroll

33 20061206 chap8 33 Summary


Download ppt "20061206 chap8 Chapter 8 Arrays (Hanly). 20061206 chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a."

Similar presentations


Ads by Google