Chapter2-Array - DataStructure SangYongHan...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Data Structure                  Sang Yong Han            http://ec.cse.cau.ac.kr/ Chung-Ang University Spring 2011 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays Array: a set of pairs ( index and value ) data structure For each index, there is a value associated with that index. representation (possible) implemented by using consecutive memory.
Background image of page 2
     Objects:  A set of pairs <index, value> where for each value of index        there is a value from the set item.  Index  is a finite ordered set of one or       more dimensions, for example, {0, … , n-1} for one dimension,     {(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for two  dimensions,      etc.     Methods:    for all A   Array, i   index, x   item, j, size   integer    Array Create(j, list)   ::=  return  an array of   j dimensions  where  list  is a                                               j-tuple  whose  kth element  is the  size  of the                                                       kth  dimension. Items are undefined.    Item Retrieve(A, i)    ::=  if  (i   index)  return  the item associated with                                           index value i in array A                                           else return  error   Array Store(A, i, x)   ::=  if ( i in index)                                           return  an array that is identical to array                                             A except the new pair <i, x> has been                                              inserted   else return  error     The Array  ADT
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays in C int list[5], *plist[5]; list[5]: five integers list[0], list[1], list[2], list[3], list[4] *plist[5]: five pointers to integers plist[0], plist[1], plist[2], plist[3], plist[4] implementation of 1-D array list[0] base address = α list[1] α + sizeof(int) list[2] α + 2*sizeof(int) list[3] α + 3*sizeof(int)
Background image of page 4
Arrays in C (cont’d)  Compare int *list1 and int list2[5] in C. Same: list1 and list2 are pointers. Difference: list2 reserves five locations . Notations: list2 - a pointer to list2[0] (list2 + i) - a pointer to list2[i] *(list2 + i) - list2[i]
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Address Contents 1228 0 1230 1 1232 2 1234 3 1236 4 Example: int one[] = {0, 1, 2, 3, 4}; //Goal: print out address and value void print1(int *ptr, int rows) { printf(“Address Contents\n”); for (i=0; i < rows; i++) printf(“%8u%5d\n”, ptr+i, *(ptr+i)); printf(“\n”); } Example 
Background image of page 6
2D Arrays The elements of a 2-dimensional array  declared as: int a[3][4]; may be shown as a table
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2011 for the course DATA STRUC 101 taught by Professor Hansanyong during the Spring '11 term at 한동대학교.

Page1 / 34

Chapter2-Array - DataStructure SangYongHan...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online