SlideShare a Scribd company logo
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

More Related Content

PPSX
Modules and packages in python
PPT
PPTX
Lecture 4 data structures and algorithms
PPTX
Priority queue in DSA
PPT
Skip list vinay khimsuriya_200430723005
PPT
Data structures & algorithms lecture 3
PPTX
Selection sorting
Modules and packages in python
Lecture 4 data structures and algorithms
Priority queue in DSA
Skip list vinay khimsuriya_200430723005
Data structures & algorithms lecture 3
Selection sorting

What's hot (20)

PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Hashing
PPTX
Java presentation on insertion sort
PPT
Enumerated data types in C
PPTX
COMPILATION PROCESS IN C.pptx
PPT
Abstract data types (adt) intro to data structure part 2
PPT
Queue data structure
PPT
Notes DATA STRUCTURE - queue
PPT
PPTX
A Presentation About Array Manipulation(Insertion & Deletion in an array)
PPTX
PPTX
Data structures
PPT
Two Dimensional Array
PPT
DATA STRUCTURES
PPTX
Threaded Binary Tree
PPT
header, circular and two way linked lists
PDF
PPTX
Programming in c Arrays
PPTX
Stack & Queue using Linked List in Data Structure
PPTX
Stack and its usage in assembly language
Array Introduction One-dimensional array Multidimensional array
Hashing
Java presentation on insertion sort
Enumerated data types in C
COMPILATION PROCESS IN C.pptx
Abstract data types (adt) intro to data structure part 2
Queue data structure
Notes DATA STRUCTURE - queue
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Data structures
Two Dimensional Array
DATA STRUCTURES
Threaded Binary Tree
header, circular and two way linked lists
Programming in c Arrays
Stack & Queue using Linked List in Data Structure
Stack and its usage in assembly language
Ad

Viewers also liked (11)

PDF
2D Array
PPTX
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
PPT
DOC
Spoj Hints
PPTX
Lecture 3 data structures and algorithms
PPTX
Lecture 8 data structures and algorithms
PPTX
Representation of binary tree in memory
POT
Arrays and addressing modes
PPT
Arrays Basics
PPTX
Array in C
PDF
Lecture17 arrays.ppt
2D Array
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
Spoj Hints
Lecture 3 data structures and algorithms
Lecture 8 data structures and algorithms
Representation of binary tree in memory
Arrays and addressing modes
Arrays Basics
Array in C
Lecture17 arrays.ppt
Ad

Similar to 02 Arrays And Memory Mapping (20)

PPTX
DSA Unit II array.pptx
PPTX
U2.pptx Advanced Data Structures and Algorithms
PPTX
arrays.pptx
PPTX
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
PPT
1st lecture.ppt
PPTX
1.Array and linklst definition
PPTX
arrays in data structure.pptx
PPT
1st lecture of DSA computer science 2024.ppt
PPTX
Data structure array
PPTX
PPTX
unit-2-dsa.pptx
PDF
cluod.pdf
PPTX
arrays.pptx
PDF
PDF
M v bramhananda reddy dsa complete notes
PPTX
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
PPTX
data structure unit -1_170434dd7400.pptx
PDF
PPTX
2. Array in Data Structure
PPTX
Data Structure Introduction- Arrays, Matrix, Linked List
DSA Unit II array.pptx
U2.pptx Advanced Data Structures and Algorithms
arrays.pptx
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
1st lecture.ppt
1.Array and linklst definition
arrays in data structure.pptx
1st lecture of DSA computer science 2024.ppt
Data structure array
unit-2-dsa.pptx
cluod.pdf
arrays.pptx
M v bramhananda reddy dsa complete notes
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
data structure unit -1_170434dd7400.pptx
2. Array in Data Structure
Data Structure Introduction- Arrays, Matrix, Linked List

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Approach and Philosophy of On baking technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Spectroscopy.pptx food analysis technology
A Presentation on Artificial Intelligence
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Programs and apps: productivity, graphics, security and other tools
NewMind AI Weekly Chronicles - August'25-Week II
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectral efficient network and resource selection model in 5G networks
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Machine learning based COVID-19 study performance prediction
Spectroscopy.pptx food analysis technology

02 Arrays And Memory Mapping

  • 1. Linear Data Structures Arrays Strings Structures Stacks Queues Link List (Logically Linear)‏
  • 2. 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
  • 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 Two Dimensional Array Can be called Table Needs two dimensions - One for Rows - Other for columns Two representations Row Major Column Major
  • 5. 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
  • 6. 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?
  • 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 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)‏
  • 9. 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)‏
  • 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 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
  • 13. Symmetric Diagonal Triangular Sparse * make programs of all these properties Special Matrices
  • 14. 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.
  • 15. 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
  • 16. 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.
  • 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(int s)‏ { 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.