01-Introduction Arrays
01-Introduction Arrays
Introduction
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 1
The Course
● Text Books
■ Introduction to Algorithms (Third edition)
Cormen, Leiserson, Rivest, and Stein
○ An excellent reference you should own
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 2
What is a Data Structure?
● Data is a collection of facts, such as values, numbers, words,
measurements, or observations.
● Structure means a set of rules that holds the data together.
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 3
Types of Data Structures
● Data structures are classified as either
■ Linear (e.g, arrays, linked lists), or
■ Nonlinear (e.g, trees, graphs, etc.)
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 4
Arrays
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 5
Properties of Arrays
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 6
Parameters of Arrays
● Base Address (b): The memory address of the first byte of the
first array component.
● Component Length (L): The memory required to store one
component of an array.
● Upper and Lower Bounds (li, ui): Each index type has a
smallest value and a largest value.
● Dimension
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 7
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts index value to component address
● Linear (1 D) Arrays:
a : array [l1 .. u1] of element_type
Then addr(a[i]) = b + (i l1) L
= c0 + c1 i
Therefore, the time for calculating the address of an element is same
for any value of i.
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 8
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts index value to component address
● 2 D Arrays:
a : array [l1 .. u1, l2 .. u2] of element_type
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 9
Representation of Arrays in Memory
● 2 D Arrays:
The elements of a 2-dimensional array a are declared as:
int a[3][4];
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 10
Representation of Arrays in Memory
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 11
Representation of Arrays in Memory
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 12
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts Index value to component address
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 13
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts Index value to component address
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 14
Representation of Arrays in Memory
● Array Mapping Function (AMF)
■ AMF converts Index value to component address
● 3 D Arrays :
a : array [l1 .. u1, l2 .. u2 , l3 .. u3] of element_type
Friday, September 8, 2023 Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET 15