{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter2-Array

# Chapter2-Array - DataStructure SangYongHan...

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

Data Structure                  Sang Yong Han            http://ec.cse.cau.ac.kr/ Chung-Ang University Spring 2011 1

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

View Full Document
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.
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

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

View Full Document
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) list[4] α + 4*size(int)
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) - list2[i]

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

View Full Document
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
2D Arrays The elements of a 2-dimensional array  declared as: int a[3][4]; may be shown as a table a[0][0]     a[0][1]    a[0][2]    a[0][3] a[1][0]      a[1][1]    a[1][2]     a[1][3] a[2][0]     a[2][1]    a[2][2]     a[2][3]

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 34

Chapter2-Array - DataStructure SangYongHan...

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

View Full Document
Ask a homework question - tutors are online