Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Data Structure

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,

 in the entire course (say 100 students)

in the entire college (say 500 students)

in the entire university (say 10,000 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.

Size—the maximum number of values that the array can hold.

Arrays are declared using the following syntax:

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.

Figure 3.3 shows how different types of arrays are declared.

There are majorly three types of arrays:


1. One-dimensional array (1-D arrays)
2. Two-dimensional (2D) array
3. Three-dimensional array

1. One-dimensional array (1-D arrays) :


You can imagine a 1d array as a row, where elements are stored one after another.

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 3

1D array

Syntax for Declaration of Single Dimensional Array:


Below is the syntax to declare the single-dimensional array
data_type array_name[array_size];
where,
 data_type: is a type of data of each array block.
 array_name: is the name of the array using which we can refer to it.
 array_size: is the number of blocks of memory array going to have.

For Example
int nums[5];

2. Two-dimensional (2D) array:


Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows
and columns.

2D array

Syntax for Declaration of Two-Dimensional Array


Below is the syntax to declare the Two-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension];
where,
 data_type: is a type of data of each array block.
 array_name: is the name of the array using which we can refer to it.

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

Syntax for Declaration of Three-Dimensional Array


Below is the syntax to declare the Three-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension][sizeof_3rd_dimension];
where,
 data_type: is a type of data of each array block.
 array_name: is the name of the array using which we can refer to it.
 sizeof_dimension: is the number of blocks of memory array going to have in the
corresponding dimension.

For Example
int nums[5][10][2];

REPRESENTATION OF LINEAR ARRAYS IN MEMORY

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.

b. The element of the array are respectively in successive memory locations.

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

Representation of linear arrays in memory:


Let LA be a linear array in the memory of the computer. The memory of the computer is simply a
sequence of address location as shown below,

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)

and called the base address of 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.

1. [Initialize counter] set J:= N


2. Repeat step 3 and 4 while J ≥ K
th
3. [Move J element downward] Set LA [J+1] := LA[J]
4. [Decrease counter] set J:= J —1
[End of step 2 loop]
5. [Insert element] set LA[K]:= ITEM
6. [Reset N] set N:= N+1
7. Exit

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

elements be moved one location upward to fill up the array.

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

Algorithm: (Linear Search) LINEAR (DATA, N, ITEM, LOC)


Here DATA is a linear array with N elements, and ITEM is a given item of information. This algorithm
finds the location LOC of ITEM in DATA, or sets LOC: = 0 if the search is unsuccessful.

1.[Insert ITEM at the end of DATA.] Set DATA [N + 1]: = ITEM.


2.[Initialize counter.] Set LOC: = 1.
3.[Search for ITEM.]
Repeat while DATA [LOC] # ITEM:
Set LOC:= LOC + 1.
[End of loop.]

4.[Successful?] If LOC = N + 1, then: Set LOC:= 0


5. Exit.

Codes related to Array from Reference 1.

References:

1. Data Structures Using C, 2nd Edition by Reema Thareja.

2. DATA STRUCTURES WITH C by Seymour Lipschutz, 2010.

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.

You might also like