Professional Documents
Culture Documents
Data Structure L3
Data Structure L3
Lecture 3
Array:
Introduction:
We will explain the concept of arrays using an analogy. Consider a situation in which we have 20
students in a class and we have been asked to write a program that reads and prints the marks of all
the 20 students. In this program, we will need 20 integer variables with different names, as shown in
Fig. 3.1.
Now to read the values of these 20 variables, we must have 20 read statements. Similarly, to print
the value of these variables, we need 20 write statements. If it is just a matter of 20 variables, then it
might be acceptable for the user to follow this approach. But would it be possible to follow this
approach if we have to read and print the marks of students,
The answer is no, definitely not! To process a large amount of data, we need a data structure known
as array.
An array is a collection of similar data elements. These data elements have the same data type. The
elements of the array are stored in consecutive memory locations and are referenced by an index
(also known as the subscript). The subscript is an ordinal number which is used to identify an
element of the array.
DECLARATION OF ARRAYS
We have already seen that every variable must be declared before it is used. The same concept
holds true for array variables. An array must be declared before being used. Declaring an array
means specifying the following:
Data type—the kind of values it can store, for example, int, char, float, double.
Name—to identify the array.
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
type name[size];
The type can be either int, float, double, char, or any other valid data type. The number within
brackets indicates the size of the array, i.e., the maximum number of elements that can be stored in
the array.For example, if we write,
int marks[10];
then the statement declares marks to be an array containing 10 elements. In C, the array index starts
from zero. The first element will be stored in marks[0], second element in marks[1], and so on.
Therefore, the last element, that is the 10th element, will be stored in marks[9]. Note that 0, 1, 2, 3
written within square brackets are the subscripts. In the memory, the array will be stored as shown
in Fig. 3.2.
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
1D array
For Example
int nums[5];
2D array
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
sizeof_dimension: is the number of blocks of memory array going to have in the
corresponding dimension.
For Example
int nums[5][10];
3. Three-dimensional array:
A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-
dimensional arrays.
3D array
For Example
int nums[5][10][2];
A linear array is a list of a finite number ‘n’ of homogeneous data element such that
a. The elements of the array are reference respectively by an index set consisting of n
consecutive numbers.
The number n of elements is called the length or size of the array. The length or the numbers
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
of elements of the array can be obtained from the index set by the formula
The elements of LA are stored in successive memory cells. The computer does not keep track of the
address of every element of LA, but needs to keep track only the address of the first element of LA
denoted by,
Base (LA)
Using the base address of LA, the computer calculates the address of any element of LA by the
formula,
Where, w is the number of words per memory cell for the array LA.
ARRAY OPERATIONS
1. Traversing
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
Let A be a collection of data elements stored in the memory of the computer. Suppose if the
contents of the each elements of array A needs to be printed or to count the numbers of elements
of A with a given property can be accomplished by Traversing.
Traversing is a accessing and processing each element in the array exactly once.
Algorithm 1: (Traversing a Linear Array) Here LA is a linear array with the lower bound LB and upper
bound UB. This algorithm traverses LA applying an operation PROCESS to each element of LA using
while loop.
Algorithm 2: (Traversing a Linear Array) Hear LA is a linear array with the lower bound LB and upper
bound UB. This algorithm traverses LA applying an operation PROCESS to each element of LA using
repeat – for loop.
Example: Consider the array AUTO which records the number of automobiles sold each year from
1932 through 1984.
To find the number NUM of years during which more than 300 automobiles were sold, involves
traversing AUTO.
2. Inserting
Let A be a collection of data elements stored in the memory of the computer.Inserting refers to the
operation of adding another element to the collection A.
Inserting an element at the “end” of the linear array can be easily done provided the memory space
allocated for the array is large enough to accommodate the additional
element.
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
Inserting an element in the middle of the array, then on average, half of the elements must be
moved downwards to new locations to accommodate the new element and keep the order of the
other elements
Algorithm:
INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K ≤ N. This
algorithm inserts an element ITEM into the Kth position in LA.
3. Deleting
Deleting refers to the operation of removing one element to the collection A.
Deleting an element at the “end” of the linear array can be easily done with difficulties.
If element at the middle of the array needs to be deleted, then each subsequent
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3
References:
3. https://www.geeksforgeeks.org/types-of-arrays/
4. https://deepakdvallur.weebly.com/data-structures-and-applications.html
Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.