KEMBAR78
02 Arrays And Memory Mapping | PPT
Linear Data Structures Arrays Strings Structures Stacks Queues Link List (Logically Linear)‏
Representation of Single Dimensional Arrays Dimension  ? Whole array is stored in a single contiguous memory block. Address of the first element is called the  base address  and it is also the start address of array. The address of the ith element is given by the following formula: Address i  = base_address + i * size_of_element Arrays are very efficient way of organizing data since accessing array elements requires O(1). elem n-1 … elem i … elem 2 elem 1 elem 0
float sum (float *list, int n)‏ { float temp = 0; int i; for (i = 0; i < n; i++)‏ temp = temp + *(list + i); return temp; } float sum (float list[], int n)‏ { float temp = 0; int i; for (i = 0; i < n; i++)‏ temp = temp + list[i]); return temp; }
Representation of Two Dimensional Array Can be called Table Needs two dimensions   -  One for Rows - Other for columns Two representations Row Major Column Major
Representations of Table Row Major ( First subscript changes least frequently and last subscript changes most frequently)‏ (i,j)th element = Base Address + (i * N + j) * size of array  Column Major ( Last subscript changes least frequently and first subscript changes more frequently)‏ (i,j)th element = Base Address + (j * M + i) * size of array
void add (int a[][], int b[][],   int c[][],   int rows, int cols)‏ { int i, j; for (i=0; i<rows; i++)‏ for (j=0; j<cols; j++)‏ c[i][j] = a[i][j] + b[i][j]; } What’s wrong?
const int N = 100; void add (int a[][N], int b[][N],   int c[][N],   int rows, int cols)‏ { int i, j; for (i=0; i<rows; i++)‏ for (j=0; j<cols; j++)‏ c[i][j] = a[i][j] + b[i][j]; } Why?
Two Dimensional Array 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)‏
Two Dimensional Array 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)‏
Row major – C, Pascal, C++, etc. Column Major – FORTRAN Ada - indefinite Two Dimensional Array
Row Major  Slice along the 1 st  dimension to get an array of N-1 dimensions Continue until you are left with one dimension only. Offset of a[d 0 ] [d 1 ] …[d n-1 ] 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
Simple View General formula in Row Major  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 General formula in Column Major  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
Symmetric  Diagonal Triangular Sparse * make programs of all these properties Special Matrices
Ordered Lists One of the most common data object is an ordered list. An ordered list has elements with definite ordering. The simplest example of an ordered list is an ordered pair which has only two elements. Examples of ordered lists: Months of the years (Jan, Feb, Mar, Apr, May,…., Dec)‏ English Alphabets (A, B, C, …, Z)‏ Words in a sentence (“This is a book on data structures.”)‏ Names of the students in a class stored in the order of their roll numbers.
Operations Defined on an Ordered List Find the length of list. Check if the list is empty. Traverse the list in some order. Get the i th  elements in the list. Change the value of the i th  element. Insert a new element at the i th  position. Delete the i th  element. *Make programs of all these properties
Representation of an Ordered List 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. This is called  sequential  or  linear mapping  – mapping a one-dimensional vector onto a one-dimensional space.
class LinearList { public:  LinearList(int s); // constructor ~ LinearList () {delete [ ] ListArray; }  // destructor bool add (int value); bool remove (int index); bool change(int index, int value); bool get(int index, int & value); void printAll(); bool isFull() {return size == maxSize ;} bool isEmpty()  {return size == 0; } int  length() {return size;} private: int maxSize; // max List size int *ListArray; int size; // no. of elements in the List void readjust(int index); void moveForward(int index); void moveBackward(int index); }; Linear List Using Arrays
LinearList :: LinearList(int s)‏ { maxSize = s; ListArray = new int[maxSize]; size = 0; } void LinearList :: printALL()‏ { for (i = 0; i < size; i++)‏ cout << ListArray[i] << newline; }
bool LinearList :: get(int index, int & value) { if (index < size  && index >= 0) { value = ListArray[index]; return true; } else return false; }
bool LinearList::add(int value)‏ { if (! isFull() ) {  for (int i = size; i >= 1; i--) { if (ListArray[i-1] > value)‏ ListArray[i] = ListArray[i -1]; else break; } ListArray[i] = value; size++; return true;  } else return false; }
bool LinearList::remove(int index)‏ { if (index < size  && index >= 0)  {  for (int i = index; i < size - 1; i++)‏ ListArray[i] = ListArray[i +1]; size--; return true;  } else return false; }
bool LinearList :: change(int index, int value) { if (index < size  && index >= 0) { ListArray[index] = value; readjust(index); return true; } else return false; } 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); }
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; }
Assignment# 1  last date of submission :  02-09-2008 (Tuesday)‏ 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  Arrays   - Read a matrix , find upper triangular matrix, find  diagonal matrix, find determinant of a matrix  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. 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

02 Arrays And Memory Mapping

  • 1.
    Linear Data StructuresArrays Strings Structures Stacks Queues Link List (Logically Linear)‏
  • 2.
    Representation of SingleDimensional Arrays Dimension ? Whole array is stored in a single contiguous memory block. Address of the first element is called the base address and it is also the start address of array. The address of the ith element is given by the following formula: Address i = base_address + i * size_of_element Arrays are very efficient way of organizing data since accessing array elements requires O(1). elem n-1 … elem i … elem 2 elem 1 elem 0
  • 3.
    float sum (float*list, int n)‏ { float temp = 0; int i; for (i = 0; i < n; i++)‏ temp = temp + *(list + i); return temp; } float sum (float list[], int n)‏ { float temp = 0; int i; for (i = 0; i < n; i++)‏ temp = temp + list[i]); return temp; }
  • 4.
    Representation of TwoDimensional Array Can be called Table Needs two dimensions - One for Rows - Other for columns Two representations Row Major Column Major
  • 5.
    Representations of TableRow Major ( First subscript changes least frequently and last subscript changes most frequently)‏ (i,j)th element = Base Address + (i * N + j) * size of array Column Major ( Last subscript changes least frequently and first subscript changes more frequently)‏ (i,j)th element = Base Address + (j * M + i) * size of array
  • 6.
    void add (inta[][], int b[][], int c[][], int rows, int cols)‏ { int i, j; for (i=0; i<rows; i++)‏ for (j=0; j<cols; j++)‏ c[i][j] = a[i][j] + b[i][j]; } What’s wrong?
  • 7.
    const int N= 100; void add (int a[][N], int b[][N], int c[][N], int rows, int cols)‏ { int i, j; for (i=0; i<rows; i++)‏ for (j=0; j<cols; j++)‏ c[i][j] = a[i][j] + b[i][j]; } Why?
  • 8.
    Two Dimensional ArrayOffset 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.
    Two Dimensional ArrayOffset 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.
    Row major –C, Pascal, C++, etc. Column Major – FORTRAN Ada - indefinite Two Dimensional Array
  • 11.
    Row Major Slice along the 1 st dimension to get an array of N-1 dimensions Continue until you are left with one dimension only. Offset of a[d 0 ] [d 1 ] …[d n-1 ] 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.
    Simple View Generalformula in Row Major 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 General formula in Column Major 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
  • 13.
    Symmetric DiagonalTriangular Sparse * make programs of all these properties Special Matrices
  • 14.
    Ordered Lists Oneof the most common data object is an ordered list. An ordered list has elements with definite ordering. The simplest example of an ordered list is an ordered pair which has only two elements. Examples of ordered lists: Months of the years (Jan, Feb, Mar, Apr, May,…., Dec)‏ English Alphabets (A, B, C, …, Z)‏ Words in a sentence (“This is a book on data structures.”)‏ Names of the students in a class stored in the order of their roll numbers.
  • 15.
    Operations Defined onan Ordered List Find the length of list. Check if the list is empty. Traverse the list in some order. Get the i th elements in the list. Change the value of the i th element. Insert a new element at the i th position. Delete the i th element. *Make programs of all these properties
  • 16.
    Representation of anOrdered List 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. This is called sequential or linear mapping – mapping a one-dimensional vector onto a one-dimensional space.
  • 17.
    class LinearList {public: LinearList(int s); // constructor ~ LinearList () {delete [ ] ListArray; } // destructor bool add (int value); bool remove (int index); bool change(int index, int value); bool get(int index, int & value); void printAll(); bool isFull() {return size == maxSize ;} bool isEmpty() {return size == 0; } int length() {return size;} private: int maxSize; // max List size int *ListArray; int size; // no. of elements in the List void readjust(int index); void moveForward(int index); void moveBackward(int index); }; Linear List Using Arrays
  • 18.
    LinearList :: LinearList(ints)‏ { maxSize = s; ListArray = new int[maxSize]; size = 0; } void LinearList :: printALL()‏ { for (i = 0; i < size; i++)‏ cout << ListArray[i] << newline; }
  • 19.
    bool LinearList ::get(int index, int & value) { if (index < size && index >= 0) { value = ListArray[index]; return true; } else return false; }
  • 20.
    bool LinearList::add(int value)‏{ if (! isFull() ) { for (int i = size; i >= 1; i--) { if (ListArray[i-1] > value)‏ ListArray[i] = ListArray[i -1]; else break; } ListArray[i] = value; size++; return true; } else return false; }
  • 21.
    bool LinearList::remove(int index)‏{ if (index < size && index >= 0) { for (int i = index; i < size - 1; i++)‏ ListArray[i] = ListArray[i +1]; size--; return true; } else return false; }
  • 22.
    bool LinearList ::change(int index, int value) { if (index < size && index >= 0) { ListArray[index] = value; readjust(index); return true; } else return false; } 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.
    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.
    Assignment# 1 last date of submission : 02-09-2008 (Tuesday)‏ 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 Arrays - Read a matrix , find upper triangular matrix, find diagonal matrix, find determinant of a matrix 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. 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

Editor's Notes

  • #15 One of the most common data object is an ordered list. An ordered list has elements with definite ordering. The simplest example of an ordered list is an ordered pair which has only two elements. Following are some examples of ordered lists: Months of the years (Jan, Feb, Mar, Apr, May,…., Dec)‏ English Alphabets (A, B, C, …, Z)‏ Words in a sentence (“This is a book on data structures.”)‏ Names of the students in a class stored in the order of their roll numbers. It may be noted that the elements in an ordered-list must maintain a specified order. For example, changing the orders of words in a sentence will change the sentence. That is, it will no longer be the same sentence. As an abstract data type, an ordered-list will typically support the following operations: Create a new empty list. Delete a list. Find the number of elements in the list or length of the list. Check if the list is empty. Check if more elements can be added to the list or whether the list is full. Traverse the list in some order. Get the i th elements in the list. Change the value of the i th element. Insert a new element at the i th position. Insert a new element according to the specified order. Delete the i th element. 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. This is called sequential or linear mapping. That is mapping a one-dimensional vector onto a one-dimensional space.
  • #18 It is always a very good idea to explicitly keep track of the number elements in a list. This will make your life more comfortable. Your algorithms simpler and will be more independent of the data type stored in your list.