chapter 2 1. arrays array: a set of index and value data structure for each index, there is a value...

42
Arrays Chapter 2 1

Upload: clement-farmer

Post on 31-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

Arrays

ArraysChapter 21ArraysArray: a set of index and valueData structureFor each index, there is a value associated with that index.Eg. int list[5]: list[0], , list[4] each contains an integer

201234list8723829123Abstract data type GeneralArrayclass GeneralArray {// A set of pairs where for each value of index in IndexSet there is a value of type float. // IndexSet 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), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)} for two dimensions,// etc.public:GeneralArray(int j, RangeList list, float initValue = defaultValue);// This constructor creates j dimension array of floats; the range of the kth dimension is given by the // kth element of list. For each index i in the index set, insert into the array.

float Retrieve(index i);// If I is in the index set of the array, return the float associated with i in the array; otherwise throw an // exception.void Store(index i, float x);// If i is in the index set of the array, replace the old value associated with i by x; otherwise throw an // exception.}; //3Ordered (linear) listEg.Days of the week: (SUN, MON, TUS, WED, THU, FRI, SAT)Values in a deck of cards: (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)Floors of a building: (basement, lobby, mezzanine, first, second)Years the United States fought in World War II: (1941, 1942, 1943, 1944, 1945)4Operations on lists(1) Find the length, n, of the list.(2) Read the items from left to right (or right to left).(3) Retrieve the ith element, 0i (5, 0, -15)(1, 1, 11) ====> (1, 1, 11)Move elements down very often.For all elements in column j,place element in element 21Iteration 0: scan the array and process the entries with col=022

Reference: J.L. Huang@NCTUIteration 1: scan the array and process the entries with col=123

Reference: J.L. Huang@NCTUTransposing a matrixSparseMatrix SparseMatrix::Transpose( ){// Return the transpose of *this SparseMatrix b(cols , rows , terms); // capacity of b.smArray is terms if (terms > 0) {// nonzero matrix int currentB = 0; for (int c = 0 ; c < cols ; c++) // transpose by columns for (int i = 0 ; i < terms ; i++) // find and move terms in column c if (smArray[i].col = = c) { b.smArray[currentB].row = c; b.smArray[currentB].col = smArray[i].row; b.smArray[currentB++].value = smArray[i].value; } } // end of if (terms > 0) return b;}24Transposing a matrix fasterStore some information to avoid scanning all terms backFastTranspose requires more space than TransposeCalculate RowSize by scanning array bCalculate RowStart by scanning RowSize25Example26

27

128

729

Reference: J.L. Huang@NCTUTransposing a matrix fasterSparseMatrix SparseMatrix::FastTranspose( ){// Return the transpose of *this in O(terms + cols) time. SparseMatrix b(cols , rows , terms); if (terms > 0) {// nonzero matrix int *rowSize = new int[cols]; int *rowStart = new int[cols]; // compute rowSize[i] = number of terms in row i of b fill(rowSize, rowSize + cols, 0); // initialize for (int i = 0 ; i < terms ; i ++) rowSize[smArray[i].col]++; // rowStart[i] = starting position of row i of b rowStart[0] = 0; for (int i = 1 ; i < cols ; i++) rowStart[i] = rowStart[i-1] + rowSize[i-1];30 for (int i = 0 ; i < terms ; i++) {// copy from *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row= smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } delete [] rowSize; delete [] rowStart; } return b;}31Multidimensional arraysRow major orderEg. A[2][3][2][2]24 elements (2 x 3 x 2 x 2 = 24)a[0][0][0][0], a[0][0][0][1], a[0][0][1][0], a[0][0][1][1]a[0][1][0][0], a[0][1][0][1], a[0][1][1][0], a[0][1][1][1]a[1][2][0][0], a[1][2][0][1], a[1][2][1][0], a[1][2][1][1]32Two dimensional array row major order33

Generalizing array representationDeclare a[u1][u2][un]The address for a[i1][i2][in] is

34

where is the address of a[0][0], an=1 and aj=

1j