Successfully reported this slideshow.
Upcoming SlideShare
×

# 02 Arrays And Memory Mapping

6,392 views

Published on

Published in: Technology, Art & Photos
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### 02 Arrays And Memory Mapping

1. 1. Linear Data Structures <ul><li>Arrays </li></ul><ul><li>Strings </li></ul><ul><li>Structures </li></ul><ul><li>Stacks </li></ul><ul><li>Queues </li></ul><ul><li>Link List (Logically Linear)‏ </li></ul>
2. 2. Representation of Single Dimensional Arrays <ul><li>Dimension ? </li></ul><ul><li>Whole array is stored in a single contiguous memory block. </li></ul><ul><li>Address of the first element is called the base address and it is also the start address of array. </li></ul><ul><li>The address of the ith element is given by the following formula: </li></ul><ul><li>Address i = base_address + i * size_of_element </li></ul><ul><li>Arrays are very efficient way of organizing data since accessing array elements requires O(1). </li></ul>elem n-1 … elem i … elem 2 elem 1 elem 0
3. 3. <ul><li>float sum (float *list, int n)‏ </li></ul><ul><li>{ </li></ul><ul><li>float temp = 0; </li></ul><ul><li>int i; </li></ul><ul><li>for (i = 0; i < n; i++)‏ </li></ul><ul><li>temp = temp + *(list + i); </li></ul><ul><li>return temp; </li></ul><ul><li>} </li></ul>float sum (float list[], int n)‏ { float temp = 0; int i; for (i = 0; i < n; i++)‏ temp = temp + list[i]); return temp; }
4. 4. Representation of Two Dimensional Array <ul><li>Can be called Table </li></ul><ul><li>Needs two dimensions </li></ul><ul><li>- One for Rows </li></ul><ul><li>- Other for columns </li></ul><ul><li>Two representations </li></ul><ul><ul><li>Row Major </li></ul></ul><ul><ul><li>Column Major </li></ul></ul>
5. 5. Representations of Table <ul><li>Row Major ( First subscript changes least frequently and last subscript changes most frequently)‏ </li></ul><ul><li>(i,j)th element = Base Address + (i * N + j) * size of array </li></ul><ul><li>Column Major ( Last subscript changes least frequently and first subscript changes more frequently)‏ </li></ul><ul><li>(i,j)th element = Base Address + (j * M + i) * size of array </li></ul>
6. 6. <ul><li>void add (int a[][], int b[][], </li></ul><ul><li> int c[][], </li></ul><ul><li> int rows, int cols)‏ </li></ul><ul><li>{ </li></ul><ul><li>int i, j; </li></ul><ul><li>for (i=0; i<rows; i++)‏ </li></ul><ul><li>for (j=0; j<cols; j++)‏ </li></ul><ul><li>c[i][j] = a[i][j] + b[i][j]; </li></ul><ul><li>} </li></ul>What’s wrong?
7. 7. <ul><li>const int N = 100; </li></ul><ul><li>void add (int a[][N], int b[][N], </li></ul><ul><li> int c[][N], </li></ul><ul><li> int rows, int cols)‏ </li></ul><ul><li>{ </li></ul><ul><li>int i, j; </li></ul><ul><li>for (i=0; i<rows; i++)‏ </li></ul><ul><li>for (j=0; j<cols; j++)‏ </li></ul><ul><li>c[i][j] = a[i][j] + b[i][j]; </li></ul><ul><li>} </li></ul>Why?
8. 8. <ul><li>Two Dimensional Array </li></ul>Offset of a[i][j]? 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1 User’s view (abstraction)‏ 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1 System’s view (implementation)‏
9. 9. <ul><li>Two Dimensional Array </li></ul>Offset of a[i][j]? Number of elements in each row? i th row total elements in the previous rows? j th element in i th row Offset (in units) N i * N i * N + j The addressing formula needs to know the number of columns but does not need the number of rows! 54 345 106 0 99 82 76 22 64 38 2 3 6 3 5 1 User’s view (abstraction)‏
10. 10. <ul><li>Row major – C, Pascal, C++, etc. </li></ul><ul><li>Column Major – FORTRAN </li></ul><ul><li>Ada - indefinite </li></ul>Two Dimensional Array
11. 11. <ul><li>Row Major </li></ul><ul><ul><li>Slice along the 1 st dimension to get an array of N-1 dimensions </li></ul></ul><ul><ul><li>Continue until you are left with one dimension only. </li></ul></ul><ul><ul><li>Offset of a[d 0 ] [d 1 ] …[d n-1 ] </li></ul></ul>Multi-Dimensional Arrays A[D 0 ] [D 1 ]…[D n-1 ] (…((d 0 *D 1 + d 1 )*D 2 + d 2 )*D 3 + …) + d n-1
12. 12. Simple View <ul><li>General formula in Row Major </li></ul><ul><li>A[s1,s2,s3,...sn] = Base Address + ((s 1 * D 2 *D 3 *..D n ) + (s 2 * D 3 *D 4 *..D n ) + (s n-1 *D n ) + S n ) * Size of array </li></ul><ul><li>General formula in Column Major </li></ul><ul><li>A[s1,s2,s3,...sn] = Base Address + ((s n * D 1 *D 2 *..D n-1 ) + (s n-1 * D 1 *D 2 *..D n-2 ) + (s 2 *D 1 ) + S 1 ) * Size of array </li></ul>
13. 13. <ul><li>Symmetric </li></ul><ul><li>Diagonal </li></ul><ul><li>Triangular </li></ul><ul><li>Sparse </li></ul><ul><li>* make programs of all these properties </li></ul>Special Matrices
14. 14. Ordered Lists <ul><li>One of the most common data object is an ordered list. </li></ul><ul><li>An ordered list has elements with definite ordering. The simplest example of an ordered list is an ordered pair which has only two elements. </li></ul><ul><li>Examples of ordered lists: </li></ul><ul><li>Months of the years (Jan, Feb, Mar, Apr, May,…., Dec)‏ </li></ul><ul><li>English Alphabets (A, B, C, …, Z)‏ </li></ul><ul><li>Words in a sentence (“This is a book on data structures.”)‏ </li></ul><ul><li>Names of the students in a class stored in the order of their roll numbers. </li></ul>
15. 15. Operations Defined on an Ordered List <ul><li>Find the length of list. </li></ul><ul><li>Check if the list is empty. </li></ul><ul><li>Traverse the list in some order. </li></ul><ul><li>Get the i th elements in the list. </li></ul><ul><li>Change the value of the i th element. </li></ul><ul><li>Insert a new element at the i th position. </li></ul><ul><li>Delete the i th element. </li></ul><ul><li>*Make programs of all these properties </li></ul>
16. 16. Representation of an Ordered List <ul><li>The easiest way to represent an ordered list is by an one-dimensional array where we store the i th element of the list in the array element with index i. </li></ul><ul><li>This is called sequential or linear mapping – mapping a one-dimensional vector onto a one-dimensional space. </li></ul>
17. 17. <ul><li>class LinearList { </li></ul><ul><li>public: </li></ul><ul><li>LinearList(int s); // constructor </li></ul><ul><li>~ LinearList () {delete [ ] ListArray; } // destructor </li></ul><ul><li>bool add (int value); </li></ul><ul><li>bool remove (int index); </li></ul><ul><li>bool change(int index, int value); </li></ul><ul><li>bool get(int index, int & value); </li></ul><ul><li>void printAll(); </li></ul><ul><li>bool isFull() {return size == maxSize ;} </li></ul><ul><li>bool isEmpty() {return size == 0; } </li></ul><ul><li>int length() {return size;} </li></ul><ul><li>private: </li></ul><ul><li>int maxSize; // max List size </li></ul><ul><li>int *ListArray; </li></ul><ul><li>int size; // no. of elements in the List </li></ul><ul><li>void readjust(int index); </li></ul><ul><li>void moveForward(int index); </li></ul><ul><li>void moveBackward(int index); </li></ul><ul><li>}; </li></ul>Linear List Using Arrays
18. 18. <ul><li>LinearList :: LinearList(int s)‏ </li></ul><ul><li>{ </li></ul><ul><li>maxSize = s; </li></ul><ul><li>ListArray = new int[maxSize]; </li></ul><ul><li>size = 0; </li></ul><ul><li>} </li></ul><ul><li>void LinearList :: printALL()‏ </li></ul><ul><li>{ </li></ul><ul><li>for (i = 0; i < size; i++)‏ </li></ul><ul><li>cout << ListArray[i] << newline; </li></ul><ul><li>} </li></ul>
19. 19. <ul><li>bool LinearList :: get(int index, int & value) { </li></ul><ul><li>if (index < size && index >= 0) { </li></ul><ul><li>value = ListArray[index]; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>return false; </li></ul><ul><li>} </li></ul>
20. 20. <ul><li>bool LinearList::add(int value)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (! isFull() ) { </li></ul><ul><li>for (int i = size; i >= 1; i--) { </li></ul><ul><li>if (ListArray[i-1] > value)‏ </li></ul><ul><li>ListArray[i] = ListArray[i -1]; </li></ul><ul><li>else </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>ListArray[i] = value; </li></ul><ul><li>size++; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else return false; </li></ul><ul><li>} </li></ul>
21. 21. <ul><li>bool LinearList::remove(int index)‏ </li></ul><ul><li>{ </li></ul><ul><li>if (index < size && index >= 0) { </li></ul><ul><li>for (int i = index; i < size - 1; i++)‏ </li></ul><ul><li>ListArray[i] = ListArray[i +1]; </li></ul><ul><li>size--; </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else return false; </li></ul><ul><li>} </li></ul>
22. 22. <ul><li>bool LinearList :: change(int index, int value) { </li></ul><ul><li>if (index < size && index >= 0) { </li></ul><ul><li>ListArray[index] = value; </li></ul><ul><li>readjust(index); </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>return false; </li></ul><ul><li>} </li></ul>void LinearList :: readjust(int index) { if ((index < (size - 1)) && (ListArray[index] > ListArray[index + 1]))‏ moveForward(index); else if ((index > 0) && (ListArray[index] < ListArray[index - 1]))‏ moveBackward(index); }
23. 23. void LinearList :: moveForward(int index) { int temp = ListArray[index]; int i = index; while ((i < (size - 1)) && (ListArray[i] > ListArray[i + 1])) { ListArray[i] = ListArray[i+1]; i = i + 1; } if (i != index) ListArray[i] = temp; } void LinearList :: moveBackward(int index) { int temp = ListArray[index]; int i = index; while ((i > 0 ) && (ListArray[i] < ListArray[i - 1])) { ListArray[i] = ListArray[i-1]; i = i - 1; } if (i != index) ListArray[i] = temp; }
24. 24. Assignment# 1 last date of submission : 02-09-2008 (Tuesday)‏ <ul><ul><li>Obtain an addressing formula for elements a[i,j] in the upper triangular matrix of order nxn. You can assume that this upper triangular is stored row by row </li></ul></ul><ul><ul><li>Arrays - Read a matrix , find upper triangular matrix, find diagonal matrix, find determinant of a matrix </li></ul></ul><ul><ul><li>Strings - Read a string , find the length of string, a particular word, replace a particular word with new word. - Read an array of names of students and sort it. </li></ul></ul><ul><ul><li>Structures - Make an array of structures (Roll-No, Name, Marks, Grade) , Display all records, find all the students with particular grade , find the record of a particular student, delete record of a particular student </li></ul></ul>