Lecture 1.7 - Array Traversing Insert Delete Presentation
Lecture 1.7 - Array Traversing Insert Delete Presentation
DEPARTMENT: CSE
• Introduces:
• Linear array and it’s representation in main memory.
• Operations of a linear array
• Multidimensional array
• 2-dimensional array representation in a memory
• Pointer
• Pointer Arrays
• Records
• Parallel Arrays
09/06/2021
Contents
• Introduction
• Linear Array
• Linear Array Representations in Memory
• Traversing, Insert, and Delete Algorithms
• Multidimensional Array
• 2-D Array
• Representations in Memory
• Inconsistency Matrix Algorithms
09/06/2021
Arrays
• Arrays: - Linear DS Homogeneous (same type) data elements.
Index Data
1 100
2 200
3 300 Index 1 2 3 4 5 6 7
4 400
5 500 Data 100 200 300 400 500 600 700
6 600
7 700
09/06/2021
Arrays
• Easy to traverse
• Easy to search & sort
• Used to store relatively permanent collections of data.
• Types of arrays:-
• Linear arrays
• Two dimensional arrays
• Multidimensional arrays
09/06/2021
Representation of Linear array
• LOC(LA[K]) = address of the element LA[K] of the array LA
• LOC(LA[K])=Base(LA)+w(K-lower bound)
• where w is the number of words per memory cell for the array
LA.
09/06/2021
Representation of Array in a Memory
The process to determine the address in a memory:
a) First address – base address.
b) Relative address to base address through index function.
Example: char X[100];
Let char uses 1 location storage.If the base address is 1200 then the next
element is in 1201.Index Function is written as:
Loc (X[i]) = Loc(X[0]) + i , i is subscript and LB = 0
single name.
09/06/2021
Disadvantages
1. We must know in advance that how many elements are to be stored
in array.
2. Array is static structure. It means that array is of fixed size. The
memory which is allocated to array can not be increased or reduced.
3. Since array is of fixed size, if we allocate more memory than
requirement then the memory space will be wasted. And if we
allocate less memory than requirement, then it will create problem.
09/06/2021
Two-Dimensional Arrays
09/06/2021
Example
• Suppose each student in a class of 25 students is given 4 tests. Assuming
the students are numbered from 1 to 25, the test scores can be assigned to
a 25 ᵡ 4 matrix array SCORE as in table. Thus SCORE[K,L] contains the
Kth student’s score on the Lth test. In particular, the second row of the
array, Contains the four test scores of the second student.
SCORE[2,1], SCORE[2,2], SCORE[2,3], SCORE[2,4]
Student Test 1 Test 2 Test 3 Test 4
1 84 73 78 88
2 56 58 78 67
3 56 77 45 67
: : : : :
25 76 78 80 45
09/06/2021
Formula to calculate the Two-D Arrays
• Length = UB – LB +1
If integer NUM (2:5, -3:1), then find the length of 1st & 2nd dimensions.
Length of 1st dimension = 5 – 2 + 1 = 4
Length of 1st dimension = 1 – (-3) + 1 = 5
Number contains 4 * 5 = 20 elements.
09/06/2021
Representation of 2-D Arrays
09/06/2021
Computing of 2-D Arrays
• Column-major order:-
• LOC(A[J,K]) = Base(A) + w[M(K – 1) + (J – 1)]
• Row-major order: -
• LOC(A[J,K]) = Base(A) + w[N(J – 1) + (K – 1)]
eg:- Consider the 25 * 4 matrix array SCORE in previous example.
Suppose Base(SCORE) = 200 & there are w = 4 words per memory cell.
Furthermore, suppose the PL stores 2-D arrays using row-major order. Then the
address of SCORE[12,3], the third test of the 12th student, follows:
LOC(SCORE[12,3]) = 200 + 4 [4(12 – 1) + (3 – 1)]
= 200 + 4[46] = 384
09/06/2021
Multidimensional Arrays
• Li = UB – LB + 1
09/06/2021
How to Calculate Multidimensional Arrays
• Suppose a 3-D array MAZE is declared using MAZE(2:8, -4:1, 6:10). Then the lengths of the 3-D of
the MAZE are, respectively,
L1 = 8 – 2 + 1 = 7, L2 = 1 – (-4) + 1 = 6, L3 = 10 – 6 + 1 = 5
Accordingly, MAZE contains L1 . L2 . L3 = 7 * 6 * 5 = 210 elements
suppose the PL stores MAZE in memory in row-major order & suppose Base(MAZE) = 200 & there are
w = 4 words per memory cell.
eg: - The address of MAZE[5, -1, 8]:-
E1 = 5 – 2 = 3 E2 = -1 – (-4) = 3 E3 = 8 – 6 = 2
E1 L2 = 3 * 6 = 18
E1 L2 + E2 = 18 + 6 = 21
(E1 L2 + E2)L3 = 21 * 5 = 105
(E1 L2 + E2)L3 + E3 = 105 + 2 = 107
Therefore, LOC(MAZE[5, -1, 8] = 200 + 4(107) = 628
09/06/2021
Sparse matrix
Matrix with relatively a high proportion of zero entries are called sparse
matrix. Two general types of n-square sparse matrices are there which
occur in various applications are mention in figure below(It is
sometimes customary to omit blocks of zeros in a matrix as shown in
figure below)
09/06/2021
Triangular and Tridiagonal matrix
• Triangular matrix:
• This is the matrix where all the entries above the main diagonal
are zero or equivalently where non-zero entries can only occur
on or below the main diagonal is called a (lower)
• Tridiagonal matrix:
• This is the matrix where non-zero entries can only occur on the
diagonal or on elements immediately above or below the diagonal
is called a Tridiagonal matrix.
• The natural method of representing matrices in memory as two-
dimensional arrays may not be suitable for sparse matrices i.e. one
may save space by storing only those entries which may be non-
zero.
09/06/2021
Records: Record Structures
• Collections of data organized into: -
• Field, records, & files
• Record: - collection of related data items, each of which is
called a field/attribute
• File: - collections of similar records.
• Each data items itself may be a group item composed of sub-
items; those items which are indecomposable are called
elementary items/atoms/scalars.
• The names given to the various data items identifiers.
Record
• malloc: Allocates requested number of bytes and returns a pointer to the first
byte of the allocated space.
• calloc: Allocates space for an array of elements, initializes them to zero and
then returns a pointer to the memory.
• free : Frees previously allocated space.
• realloc: Modifies the size of previously allocated space.
Dynamic allocation of two-dimensional arrays
int **array;
int i,j,10;
array = malloc(10*sizeof(int*));
for(i=0;i<10;i++)
for(j=0;j<10;j++)
array[i][j] = i*j;
• Syntax: -
delete ptrVar;
Pointers
int *intPtr; Create a pointer
*intPtr 6837
intPtr 0x0050
*intPtr 5 otherVal
intPtr 0x0054 &otherVal
REFERENCES
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley Student
Edition.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of India
• https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
• https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecturehtml
09/06/2021