Data Structures
& Algorithms
Today Topics
□
What are Arrays?
□
Arrays Data Structures
■
One Dimensional Array
□
Operations on Arrays
■
Inserting
■
Deleting
■
Traversing
■
Searching
■
Reversing
What are Arrays?
Array can be defined as:
‘a group of data items of the same type
under one name’ or ‘array is a collection of
elements or items or components that have
the same data type’.
For example array of 10 integers, arrays of
names, etcetera. It can also be defined as
‘a list of two or more variables with the
Why we use Array?
□ For example you want to find smallest numbers
amongst four integer items and suppose that you do not know how to use array or you do not desire to use it. You may tackle the problem in the following
manner:
□ Define four variables to hold four integers data items, i.e.,
int a=10, b=20, c=30; d=5;
□ After declaring these variables, you would be required to write a quite few statements to find the smallest number in them. Since there are four variables,
therefore, you would need to use four compound if statements for the solution, such as:
Why we use Array?
□ if (a<b) && (a<c) && (a<d) cout << “a is smallest number in the data”;
□ if (b<a) && (b<c) && (b<d) cout << “b is smallest number in the data”;
□ if (c<b) && (c<b) && (c<d) cout << “c is smallest number in the data”;
□ if (d<a) && (d<b) && (d<b) cout << “d is smallest number in the data”;
□ Based on the current data as mentioned above, computer will produce that “d is the smallest number in the data”.
Why we use Array?
Assuming if you would have to find smallest number amongst 100 data items, off course you would be required to write 100 compound if statements and each compound condition made of 99 simple
conditions. Further, it would not be possible to do other application such as sorting data, finding any particular number in large amount of data with the use simple data structures. Therefore, the use of arrays in programming languages is unavoidable. Processing on the data such as finding any number, calculating mean, average and sorting data are some of the fascinating applications which can be
One Dimensional Array
□ One dimensional array is most common form of the array used by programmers to solve different kinds of problems.
□ The general format of the one-dimensional array is as follows: data_type array_name[size];
where
data_type: is any valid C++ data type (int, float, etc.)
array_name: is user defined identifier. User_defined name is followed by the square
brackets which makes difference between ordinary variable and array type variable.
Size: shows the actual length of the array, i.e., number of locations or no data items.
Examples of One Dimensional Array
□
int a[10];
‘a’ is an integer array of
10 elements
□
float f[10] ‘f’ is a float array of 10
elements
□
double d[10] ‘d’ is a double array of
10 elements.
Examples of One Dimensional Array
□ Suppose you want to store identity-numbers of 20 students, the following statement would be used to declare a one dimensional integer array to hold data;
int student_id_no[20];
where int is type of the array, and
‘student_id_no’ is name of the array (user defined).
Computer will allocate 20 locations for ‘student_id_no’.
C++ starts array locations from zero, hence, first location is indicated as zero
Diagrammatically Representation of
Array
0 1 2 3 4 5 6 7 8 9 Index Number 101 103 105 107 109 111 113 115 117 119 Memory Address LB UBOne Dimensional Array
□ Maximum number of elements in a One-Dimensional Array is computed as under:
Maximum number of elements = UB – LB + 1
UB – Upper Bound LB – Lower Bound
In pervious diagram UB = 9 and LB = 0
So, Maximum number of elements = 9 – 0 +1 = 10
□ Each element of the array also has a unique memory address
□ The starting address of array is called Base Address. □ Elements of an array occupy a continuous block of
One Dimensional Array
□ The memory address of an element with index k is computed as:
L(X(k)) = L0 + C * (k – 1)
L0 is a Base Address, C memory size of each element in byte
□ Suppose that Base address is 101 and size of element is 2 bytes, what will be the memory address for index 5
(Previous diagram)
L0 = 101, C = 2, k = 5 L(X(k)) = L0 + C * (k – 1)
L(X(5)) = 101 + 2 * (5 – 1)
Operations on 1-D Array
□ Traversal: Processing each element in the array
□ Search: Finding the location of an element with a given
value
□ Insertion: Adding a new element to an array
□ Deletion: Removing an element from an array
□ Sorting: Organizing the elements in some order
□ Merging: Combining two arrays into a single array
Algorithms of Operations on 1-D
Array
Traverse_Array
Algorithm for print the each element of
array
1.
Start
2.
Repeat step-3 For i=1 to Max by 1
3.
Print arr[i]
Algorithms of Operations on 1-D
Array
Insert_Item(pos, num)
Algorithm for inserting an element at
given position of the array
1.
Start
2.
Repeat step-3 For i=Max to Pos+1 by -1
3.
arr[i] = arr[i-1]
4.
Arr[Pos] = num
5.
End
Algorithms of Operations on 1-D
Array
Delete_Item(pos)
Algorithm for delete an element from the
given position of the array
1.
Start
2.
Repeat step-3 For i=Pos to Max-1 by 1
3.
arr[i] = arr[i+1]
4.
Arr[Max] = 0
5.
End
Algorithms of Operations on 1-D
Array
Search_Item(num)
Algorithm for search Pos of the given element in the array
1. Start
2. Repeat step 3 For i=1 to Max by 1
3. If arr[i] = num then
Print I Return End If
4. Print “Element Not Found!”
Algorithms of Operations on 1-D
Array
Reverse_Array
Algorithm for reverse the entire array
1.
Start
2.
Repeat step 3 to 5 For i=1 to Max/2 by 1
3.
temp = arr[i]
4.
arr[i] = arr[Max-i+1]
5.
arr[Max-i+1] = temp
6.
End
Two Dimensional Array
□ A two-dimensional array is a collection of elements placed in m rows and n columns.
□ The syntax used to declare a 2-D array includes two
subscripts, of which one specifies the number of rows and the other specifies the number of columns of an array.
data_type array_name[rows][columns];
where
data_type: is any valid C++ data type (int, float, etc.) array_name: is user defined identifier. User defined name is followed by the square
Rows: Specifies the number of rows of an array
Examples of One Dimensional Array
□
int a[3][4]; ‘a’ is an integer array
containing 3 rows and 4 columns
□
float f[3][4]; ‘f’ is a float array
containing 3 rows and 4 columns
□
double d[3][4]; ‘d’ is a double array
containing 3 rows and 4 columns
Diagrammatically Representation of
Array
1
2
3
4
1
12
1
-9
23
2
14
7
11
121
3
6
78
15
34
Rows
Columns
Two Dimensional Array
□ Maximum number of elements in a Two-Dimensional Array is computed as under:
Maximum number of elements = M x N
M – Number of rows N – Number of columns
In pervious diagram M = 3 and N = 4
So, Maximum number of elements =3 x 4 = 12
□ Each element of the array also has a unique memory address
□ The starting address of array is called Base Address. □ Elements of an array occupy a continuous block of
□ Two Dimensional arrays are represented in memory in two ways. ■ Row-major order
Representation of Two-Dimensional
Arrays in memory
12 1 -9 23 14 7 11 121 6 78 15 34 502 504 506 508 510 512 514 516 518 520 522 524 Memory Address Values1st Row 2nd Row 3rd Row
12 14 6 1 7 78 -9 11 15 23 121 34 502 504 506 508 510 512 514 516 518 520 522 524 Memory Address Values 1st Col ■ Column-major order
Note: Each integer occupies two bytes.
Row-Major Order
□ The memory address of an element with index i (row) and j (Col) is computed as:
L(X[i][j]) = L0 + [(i – 1) * N + (j – 1)] * d
L0 is a Base Address, d memory size of each element in byte i is a row, j is a col and N is number of columns of an array
□ Suppose that Base address is 502 and size of element is 2 bytes, what will be the memory address for position x[2][2] (Previous diagram)
L0 = 502, d = 2, i = 2, j = 2, N = 4
L(X[i][j]) = L0 + [(i – 1) * N + (j – 1)] * d
L(X[2][2]) = 502 + [(2 – 1) * 4 + (2 – 1)] * 2
Col-Major Order
□ The memory address of an element with index i (row) and j (Col) is computed as:
L(X[i][j]) = L0 + [(i – 1) + (j – 1) * M] * d
L0 is a Base Address, d memory size of each element in byte i is a row, j is a col and M is number of rows of an array
□ Suppose that Base address is 502 and size of element is 2 bytes, what will be the memory address for position x[2][2] (Previous diagram)
L0 = 502, d = 2, i = 2, j = 2, M = 3
L(X[i][j]) = L0 + [(i – 1) + (j – 1) * M] * d
L(X[2][2]) = 502 + [(2 – 1) + (2 – 1) * 3] * 2
Operations on 2-D Array
□ Traversal: Processing each element in the array
□ Search: Finding the location of an element with a given
value
□ Insertion: Adding a new element to an array
□ Deletion: Removing an element from an array
□ Sorting: Organizing the elements in some order
□ Merging: Combining two arrays into a single array
Algorithms of Operations on 2-D
Array
Traverse_Array
Algorithm for print the each element of
array
1.
Start
2.
Repeat step-3 For i=1 to rMax by 1
3.
Repeat step-4 For j=1 to cMax by 1
4.
Print arr[i][j]
Algorithms of Operations on 2-D
Array
Insert_Item_Row_Major(r, c, num)
Algorithm for inserting an element at given position of the array
1. Start
2. Repeat step-3 For i=rMax to r by -1 3. Repeat step-4 For j=cMax to 1 by -1 4. if i <> r or j > c then if j = 1 then arr[i][j] = arr[i-1][cMax] else arr[i][j] = arr[i][j-1] end if end if 5. Arr[r][c] = num 6. End
Algorithms of Operations on 2-D
Array
Delete_Item_Row_Major(r, c)
Algorithm for delete an element from the given position of the array
1. Start
2. Repeat step-3 For i=r to rMax by 1 3. Repeat step-4 For j=1 to cMax by 1 4. if i <> r or j >= c then if j = cMax then arr[i][j] = arr[i+1][1] else arr[i][j] = arr[i][j+1] end if end if 5. Arr[rMax][cMax] = 0 6. End
Algorithms of Operations on 1-D
Array
Search_Item(num)
Algorithm for search Pos of the given element in the array
1. Start
2. Repeat step 3 For i=1 to rMax by 1
3. Repeat step 4 to 5 For j=1 to cMax by 1
4. If arr[i][j] = num then
Print i & “, “ & j Return
End If
5. If i = rMax and j = cMax then
Print “Element Not Found!” End If
Common Matrix Operations
□
Traverse
□
Addition
□
Multiplication
□
Transposition
□
Evaluation of the determinant of a
square matrix
Algorithms for Matrix Addition
Matrix_Addition(m1, m2)
Algorithm for add two matrices
1.
Start
2.
Repeat step 3 For i=1 to rMax by 1
3.
Repeat step 4 For j=1 to cMax by 1
4.
Mat[i][j] = m1[i][j] + m2[i][j]
Algorithms for Matrix Multiplication
Matrix_Multiplication(m1, m2)
Algorithm for multiply two matrices
1.
Start
2.
Repeat step 3 For i=1 to rMax by 1
3.
Repeat step 4 For j=1 to cMax by 1
4.
Repeat step 5 For k=1 to rMax
5.
Mat[i][j] += m1[i][k] * m2[k][j]
6.
End
Algorithms for Matrix Transposition
Matrix_Transpose(m)
Algorithm for transpose the matrix
1.
Start
2.
Repeat step 3 For i=1 to rMax by 1
3.
Repeat step 4 For j=1 to cMax by 1
4.
Mat[i][j] = m[j][i]
Multi-Dimensional Array
□ A three-dimensional array can be thought of as an array of
arrays of arrays.
□ Following figure shows a 3-D array, which is a collection of
three 2-D arrays each containing 4 rows and 2 columns int arr[3][4][2]; 3 9 1 8 6 5 4 0 2 8 0 6 4 7 1 5 3 2 8 6 1 6 4 5
Arrays and Polynomials
□
Polynomials like
5X
4+ 2X
3+ 7X
2+ 10X – 8
□
Arithmetic operations like addition and
multiplication of polynomials are common
□
Need a way to represent the polynomial in an
array
□
Each element of the array should consist of
Arrays and Polynomials
□ To create a struct (User Defined Data Type) for
Polynomials struct Poly { int coeff; int exp; }; Poly P[Max]; P[0].coeff = c; P[0].exp = e;