0% found this document useful (0 votes)
17 views20 pages

Data Structures - Arrays

The document provides a comprehensive overview of arrays, a fundamental data structure used to store collections of elements of the same type. It covers various topics including types of arrays, their representation in memory, array traversal, insertion and deletion operations, and matrix operations using multi-dimensional arrays. Additionally, it includes algorithms and C programming examples for practical implementation of these concepts.

Uploaded by

Abhishek kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views20 pages

Data Structures - Arrays

The document provides a comprehensive overview of arrays, a fundamental data structure used to store collections of elements of the same type. It covers various topics including types of arrays, their representation in memory, array traversal, insertion and deletion operations, and matrix operations using multi-dimensional arrays. Additionally, it includes algorithms and C programming examples for practical implementation of these concepts.

Uploaded by

Abhishek kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

2

ARRAYS
2.1 Introduction
2.2 Types of Arrays
C
2.3 Representation of One-Dimensional Array in Memory h
2.4 Array Traversal
a
2.5 Insertion and Deletion
2.5.1 Insertion p
2.5.2 Deletion
t
2.6 Sorting and Searching
2.6.1 Sorting e
2.6.2 Searching
r
2.7 Representation of Multi-Dimensional Array in Memory
2.8 Realizing Matrices Using Two-Dimensional Arrays
2.9 Matrix Operations
O
2.9.1 Addition u
2.9.2 Subtraction
2.9.3 Multiplication
t
2.9.4 Transpose l
Solved Problems
i
Summary
Key Terms
n
Multiple-Choice Questions e
Review Questions
Programming Exercises
Answers to Multiple-Choice Questions

Arrays 2.1
2.1 INTRODUCTION
In Chapter 2, we briefly introduced one of the most important and commonly used derived data types,
called array. In this chapter, we will observe how array is used as a data structure in different programming
situations. We will also get familiar with the logical representation of arrays in memory.
An array is regarded as one of the most fundamental entities for storing logical groups of data. It
also forms the basis for implementing some advanced data structures, like stacks and queues, as we
shall see in the later chapters.
Typically, an array is defined as a collection of same type elements. That means, it can store a group
of integers, characters, strings, structures, and so on. However, an array cannot store different type
elements like a group of integers and fractions, or a group of strings and integers. Following are some
of the characteristics associated with arrays:
1. It uses a single name for referencing all the array elements. The differentiation between any two
elements is made on the basis of index value.
2. It stores the different elements at consecutive memory locations.
3. Its name can be used as a pointer to the first array element.
4. Its size is always a constant expression and not a variable.
5. It does not perform bound checking on its own. It has to be implemented programmatically.
Before we delve further into exploring array as a data structure, let us first identify the different
types of arrays.

2.2 TYPES OF ARRAYS


As already explained, an array is a logical grouping of same type data elements. Now, it is quite possible
that each of these elements is itself an array. Further, each of the elements of the sub array could also
be an array. This forms the basis of characterizing an array into different types, as depicted in Table 2.1.
Table 2.1 Types of arrays

Array Type Description C Representation Example


One-dimensional array It is a group of same array[ ] A group of integers.
type data elements, such {2, 5, 6, 1, 9}
as integers, floats, or
characters.
Multi-dimensional array It is a group of data array[ ][ ]..[] A group of strings.
elements, where each {“Ajay”, “Vikas”,
element is itself an array. “Amit”, “Sumit”}

The various instances of multi-dimensional arrays are two-dimensional (2D) array, three-dimensional
(3D) array, four-dimensional (4D) array, and so on. The choice of a particular multi-dimensional array
depends on the programming situation at hand. For instance, if we are required to realize a 3 ¥ 3 matrix
programmatically, then we can do so by declaring a two dimensional array, say M[3][3]. Here, each
dimension of the array M signifies the row and column of the matrix respectively. Multi-dimensional
arrays are covered in greater detail later in this chapter. For now, let us focus on implementing and
manipulating one-dimensional array.

2.2 Data Structures


2.3 REPRESENTATION OF ONE-DIMENSIONAL ARRAY IN MEMORY
The elements of a one-dimensional array are stored at consecutive locations in memory. Each of the
locations is accessed with the help of array index identifier to retrieve the corresponding element.
Consider the following integer array:
arr[5] = {2, 6, 7, 3, 8}
Here, arr is a five-element integer array. Figure 2.1 shows the representation of array arr in memory:

Fig. 2.1 Array representation in memory

As shown in Fig. 2.1, each array element is stored at consecutive memory locations, i.e., 6000, 6002,
6004, and so on. The location of the first element, i.e., 6000 is also referred as the base address of the array.
If we know the base address of an array, then we can find the location of its individual elements by
using a simple formula, which is
Address of A[k] = B + W * k
Here,
A[ ] is the array.
B is the base address, i.e., the address of the first element.
W is the word size or the size of an array element.
k is the index identifier.
For instance, the address of the third element of array arr stored at index location 2 would be
Address of arr[2] = 6000 + 2 * 2
= 6000 + 4
= 6004

Note The word size of a data type is decided by the programming language being used and the
hardware specifications.

2.4 ARRAY TRAVERSAL


While working with arrays, it is often Check Point
required to access the array elements; that
is, reading values from the array. This is 1. What is a base address?
achieved with the help of array traversal. Ans. It is the memory address of the first element
It involves visiting the array elements and of an array.
storing or retrieving values from it. Some of 2. How are array elements stored in memory?
the typical situations where array traversal Ans. The elements of an array are stored at
may be required are: consecutive locations in memory.

Arrays 2.3
Printing array elements,
Searching an element in the array, Mind Jog
Sorting an array, and so on
Algorithm What is ‘array index out of bound’?
It is a runtime error that is encountered when a
Example 2.1 Write an algorithm to sequentially program tries to reference as address location
traverse an array. outside of the defined array limits.
traverse(arr[], size)
Step 1: Start
Step 2: Set i = 0
Step 3: Repeat Steps 4-5 while i < size
Step 4: Access arr[i]
Step 5: Set i = i + 1
Step 6: Stop
Program

Example 2.2 Write a C program to traverse each element of an array and print its value on the console.
Program 2.1 performs array traversal and prints the array elements as output. It uses the algorithm
depicted in Example 2.1.
Program 2.1 C program to traverse each element of an array and print its value
#include <stdio.h>
#include <conio.h>

void traverse(int*, int); /*Function prototype for array traversal*/


void main()
{
int arr[5] = {2, 6, 7, 3, 8}; Specifying array values at the time
int N=5; of writing a program is referred as
clrscr(); compile-time initialization.

printf(“Press any key to perform array traversal and display its elements:
\n\n”);
getch();

traverse(arr,N); /*Calling traverse function*/

getch();
}

void traverse(int *array, int size)


{
int i;
for(i=0;i<size;i++)
printf(“arr[%d] = %d\n”,i,array[i]); /*Accessing array element and
printing it*/
}

2.4 Data Structures


Output
Press any key to perform array traversal and display its elements:

arr[0] = 2
arr[1] = 6
arr[2] = 7
arr[3] = 3
arr[4] = 8
Program analysis

Key Statement Purpose

void traverse(int*, int); Declares the prototype for the traverse() function for traversing
an array

traverse(arr,N); Calls the traverse() function for traversing the array arr
containing N elements

for(i=0;i<size;i++) Uses the for loop to access the array elements with each
iteration

Tip While traversing an array, the index identifier should be updated carefully so that array
out of bound situation does not arise. In this situation, the program tries to access a
location outside of the reserved memory block, which is an illegal operation.

2.5 INSERTION AND DELETION


Insertion is the task of adding an element into an existing
array while deletion is the task of removing an element Check Point
from the array. The point of insertion or deletion that is
the position where an element is to be inserted or deleted 1. What is array traversal?
holds vital importance, as we shall see in the subsequent Ans. It is the task of visiting the array
sections. elements and storing or retrieving values
from it.
2. What is the need for array traversal?
2.5.1 Insertion
Ans. It is required in almost all array
If an element is to be inserted at the end of the array, then related operations, such as sorting,
it can be simply achieved by storing the new element searching, printing, etc.
one position to the right of the last element. However,
the array must have vacant positions at the end for this to be feasible. Alternatively, if an element is
required to be inserted at the middle, then this will require all the subsequent elements to be moved one
place to the right. Figure 2.2 depicts the insertion of an element into an array.

Arrays 2.5
Fig. 2.2 Array insertion

Algorithm

Example 2.3 Write an algorithm to perform array insertion.


The following algorithm inserts an element P at index location k in the array A[N], where k<=N.
insert(A[N],k, P)
Step 1: Start
Step 2: Set i = N
Step 3: Repeat Steps 4-5 while i >=k
Step 4: Set A[i+1] = A[i]
Step 5: Set i = i - 1
Step 6: Set A[k] = P
Step 7: Set N = N + 1
Step 8: Stop
Program

Example 2.4 Write a C program to perform array insertion.


Program 2.2 performs array insertion and prints the updated array elements as output. It uses the algorithm
depicted in Example 2.3.
Program 2.2 Array insertion
#include <stdio.h>
#include <conio.h>

void main()
{
int array[10] = {-1, 3, 5, 22, 77};

2.6 Data Structures


int i, k, N, P;
clrscr();

N = 5;

printf(“The contents of the array are:\n”);


for(i=0;i<N;i++)
printf(“array[%d] = %d\n”,i,array[i]); /*Printing array elements*/

printf(“\nEnter the element to be inserted:\n”);


scanf(“%d”,&P);

printf(“\nEnter the index location where %d is to be inserted:\n”, P);


scanf(“%d”,&k);

for(i=N;i>=k;i—)
array[i+1]=array[i];
The existing array elements need
array[k] = P; to be moved to make space for the
N = N + 1; new element.

printf(“\nThe contents of the array after array insertion are:\n”);


for(i=0;i<N;i++)
printf(“array[%d] = %d\n”,i,array[i]); /*Printing array elements after
array insertion*/

getch();
}
Output

The contents of the array are:


array[0] = -1
array[1] = 3
array[2] = 5
array[3] = 22
array[4] = 77
Enter the element to be inserted:
19

Enter the index location where 19 is to be inserted:


1

The contents of the array after array insertion are:


array[0] = -1
array[1] = 19
array[2] = 3
array[3] = 5
array[4] = 22
array[5] = 77

Arrays 2.7
Program analysis

Key Statement Purpose

for(i=N;i>=k;i—) Shuffles the array elements to the right to create space for inserting a
array[i+1]=array[i]; new element.

array[k] = P; Inserts a new element at the point of insertion.

N = N + 1; Increments the total number of array elements by 1.

2.5.2 Deletion
The deletion of elements follows a similar procedure as insertion. The deletion of element from the
end is quite simple and can be achieved by mere updation of index identifier. However, to remove an
element from the middle, one must move all the elements present to the right of the point of deletion,
one position to the left. Figure 2.3 depicts the deletion of an element from an array.

Fig. 2.3 Array deletion

Algorithm

Example 2.5 Write an algorithm to perform array deletion.


The following algorithm deletes the element at index location k in the array A[N], where k<=N.
delete(A[N],k)
Step 1: Start
Step 2: Set D = A[k]
Step 3: Set i = k
Step 4: Repeat Steps 5-6 while i <=N-1
Step 5: Set A[i] = A[i+1]
Step 6: Set i = i + 1

2.8 Data Structures


Step 7: Set N = N - 1
Step 8: Stop
Program

Example 2.6 Write a C program to perform array deletion.


Program 2.3 performs array deletion and prints the updated array elements as output. It uses the algorithm
depicted in Example 2.5.
Program 2.3 Array deletion
#include <stdio.h>
#include <conio.h>

void main()
{
int array[10] = {-1, 3, 5, 22, 77};
int i, k, N, D;
clrscr();

N = 5;

printf(“The contents of the array are:\n”);


for(i=0;i<N;i++)
printf(“array[%d] = %d\n”,i,array[i]); /*Printing array elements*/

printf(“\nEnter the index location from where element is to be deleted:\n”);


scanf(“%d”,&k);

D = array[k];

for(i=k;i<=N-2;i++)
array[i]=array[i+1];
The existing array elements need
to be moved to fill the vacant space
N = N - 1; created by the deleted element.

printf(“\n%d element deleted from index location %d\n”,D,k);

printf(“\nThe contents of the array after array deletion are:\n”);


for(i=0;i<N;i++)
printf(“array[%d] = %d\n”,i,array[i]); /*Printing array elements after
array deletion*/

getch();
}

Arrays 2.9
Output
The contents of the array are:
array[0] = -1
array[1] = 3
array[2] = 5
array[3] = 22
array[4] = 77
Enter the index location from where element is to be deleted:
3
22 element deleted from index location 3
The contents of the array after array deletion are:
array[0] = -1
array[1] = 3
array[2] = 5
array[3] = 77
Program analysis

Key Statement Purpose


D = array[k]; Retrieves the element value that is to be deleted.
for(i=k;i<=N-2;i++) Shuffles the array elements to the left to fill the vacant space
array[i]=array[i+1]; created after deleting the array element.
N = N – 1; Decrements the total number of array elements by 1.

Note For large-sized arrays, inserting an element at the middle could be a considerable
programming overhead as it would require the other elements to the moved from their
current positions.

2.6 SORTING AND SEARCHING


Sorting and searching are two of the most
common operations performed on arrays. Check Point
The sorting operation arranges the elements
of an array in a specific order or sequence. 1. What is array insertion and deletion?
Searching, on the other hand, locates a Ans. The task of adding an element into an
specific element in the array. existing array is called array insertion while the
task of deleting an existing element from the array
2.6.1 Sorting is called array deletion.
2. What is a point of insertion?
Sorting involves comparing the array Ans. It is the location in the array where a new
elements with each other and shuffling them element is to be inserted.
until all the elements are sorted. There are a

2.10 Data Structures


number of sorting techniques that are applied to sort an array in an efficient manner. We shall explore
these sorting techniques in Chapter 10. Here, let us focus on one of the most basic sorting techniques
called bubble sort.
Bubble sort operates on an array in such a manner that the largest element is brought to the end of
the array with each successive iteration. It repeatedly compares two consecutive elements and moves
the largest amongst them to the right. This process is repeated for each pair of elements until the current
iteration moves the largest element to the end.
Consider the following integer array:
arr[5] = {5, 4, 3, 2, 1}
Here, arr is a five-element integer array. It will take four iterations or passes to sort this five-element
array. Each pass will bring the largest element to the end of the array. Here’s a snapshot of the array
contents after each of the four passes:
Initial Array - arr[5] = {5, 4, 3, 2, 1}
Pass 1 - arr[5] = {4, 3, 2, 1, 5}
Pass 2 - arr[5] = {3, 2, 1, 4, 5}
Pass 3 - arr[5] = {2, 1, 3, 4, 5}
Pass 4 - arr[5] = {1, 2, 3, 4, 5}
As shown above, the fourth pass produces the sorted array.
Algorithm

Refer to Section 10.2.3 for the algorithm on applying bubble sorting technique to sort an array.
Program

Example 2.7 Write a C program to sort an array of five elements.


Program 2.4 implements bubble sorting technique to sort an array of five elements.
Program 2.4 Bubble sorting technique
#include <stdio.h>
#include <conio.h>

void main()
{
int array[5]= {5, 4, 3, 2, 1};
int i, k, j, temp;
clrscr();

printf(“\nThe initial array elements are:\n”);


for(i=0;i<5;i++)
printf(“array[%d] = %d\n”,i,array[i]); /*Printing initial array*/

for(i=5;i>1;i—) /*Outer loop for controlling the number of passes*/


for(j=0;j<i-1;j++) /*Inner loop for controlling the number of comparisons
made in a pass*/
if (array[j]>array[j+1])

Arrays 2.11
{
/*Swapping adjacent elements*/
temp = array[j+1];
array[j+1] = array[j]; If the swap operation moves the
array[j] = temp; larger element towards the right then
} the array is sorted in ascending order,
otherwise it is sorted in descending
order.
printf(“\nThe sorted elements are:\n”);
for(i=0;i<5;i++)
printf(“array[%d] = %d\n”,i,array[i]); /*Printing sorted array*/

getch();
}
Output
The initial array elements are:
array[0] = 5
array[1] = 4
array[2] = 3
array[3] = 2
array[4] = 1

The sorted elements are:


array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
Program analysis

Key Statement Purpose

for(i=5;i>1;i—) Uses for loop to control the number of passes of bubble


sort algorithm

for(j=0;j<i-1;j++) Uses for loop to compare the array elements in each pass
of bubble sort algorithm

temp = array[j+1];

array[j+1] = array[j];

array[j] = temp; Swaps two array elements

Note Just like an integer array, sorting can also be applied to an array of floats, characters,
structures, and so on.

2.12 Data Structures


2.6.2 Searching
Searching is the process of traversing an array
Check Point
to find out if a specific element is present in 1. What is sorting?
the array or not. If the search is successful, the Ans. It is the task of arranging the elements
index location of the element is returned. There of an array in a sequence.
are various searching mechanisms that can be
2. How many passes does bubble sorting
employed to search an element in an array.
We shall explore these searching techniques in technique require to sort an array of n
Chapter 10. Here, let us focus on one of the most elements?
basic searching techniques called linear search. Ans. n–1.
The linear search technique traverses an array
sequentially to search the desired element. It starts the search with the first element and moves towards
the end in a step-by-step fashion. At each step, it matches the element to be searched with the array
element, and if there is a match, the location of the array element is returned.
Consider the following integer array:
arr[5] = {22, 19, 4, 8, 10}
Here, arr is a five element integer array. If we apply linear search to the array arr to search element
4, then the search will be successful as element 4 is present in the array. The search module will return
index location 2 as the search result because element 4 is the third element in the array.
Algorithm

Refer to Section 10.3.1 for the algorithm on applying linear search on an array.
Program

Example 2.8 Write a C program to perform linear search on an array.


Program 2.5 applies linear searching technique on an array of five elements.
Program 2.5 Performing linear search on an array
#include <stdio.h>
#include <conio.h>

void main()
{
int array[5] = {22, 19, 4, 8, 10};
int i, flag, k, index;
clrscr();

flag = 0;

printf(“The contents of the array are:\n”);


for(i=0;i<5;i++)
printf(“array[%d] = %d\n”,i,array[i]); /*Printing array elements*/

Arrays 2.13
printf(“\nEnter the element to be searched:\n”);
scanf(“%d”,&k);

for(i=0;i<5;i++) /*Scanning array elements one by one*/


if(k==array[i])
{
flag = 1; /*Successful Search*/
index = i;
break;
} The flag variable is updated to signal
else successful search.
;

if(flag==1)
printf(“Search is successful! Element %d is present at index location
%d in the array”,k,index);
else /*Successful Search*/
printf(“Unsuccessful Search!”);

getch();
}
Output
The contents of the array are:
array[0] = 22
array[1] = 19
array[2] = 4
array[3] = 8
array[4] = 10

Enter the element to be searched:


4
Search is successful! Element 4 is present at index location 2 in the array
Program analysis

Key Statement Purpose


scanf (“%d”,&k); Reads the key value that needs to be searched
in the array.
if (k==array [i] ) Compares the key value with each array
element.
break; Takes the program control out of the for loop
as soon as the search is successful.

Note Just like an integer array, searching can also be performed on an array of floats, characters,
structures, and so on.

2.14 Data Structures


2.7 REPRESENTATION OF
MULTI-DIMENSIONAL ARRAY IN MEMORY
Let us recall that a multi-dimensional array is an array of
arrays. Unlike one-dimensional arrays which have only
one subscript, a multidimensional array has multiple
subscripts. For example, a two-dimensional array, one
Check Point
of the most widely used instances of multi-dimensional
1. What is searching?
arrays, has two subscripts. It is used to programmatically
Ans. Searching is the process of
realize a matrix with its first subscript representing the
determining if a specific element is present
row and the second subscript representing the column
in the array or not.
of a matrix.
The representation of a two-dimensional array in 2. At a maximum, how many elements
memory is not like the gird-like structure of a matrix. would the searching technique require to
Instead, it is same as the one-dimensional array traverse in an n-element array?
representation in memory. It either stores the array Ans. n
elements row by row (row major order) or column by
column (column major order). Figure 2.4 illustrates these
representations:

Fig. 2.4 Representation of two-dimensional array in memory

As shown in Fig. 2.4, the elements of a two-dimensional array are stored at consecutive memory
locations. The only difference is in the order in which these elements are stored in memory. In column-
major order, the elements are stored column-by-column while in row-major order the elements are
stored row-by-row. Both these memory representations are intrinsic to a programming language and
the programmer does not have a choice of selecting a particular representation format for storing array
elements.

Arrays 2.15
The formula for computing the address location of a multi-dimensional array element in row major
implementation is given below:
Address of A[i,j] = B + W (n (i – LBR) + (j – LBC))
Here,
1. A[ ][ ] is the multidimensional array.
2. B is the base address.
3. W is the word size or the size of an array element.
4. n is the number of columns.
5. i, j are the index identifiers.
6. LBR is the lower bound of row index.
7. LBC is the lower bound of column index.
Similarly, the formula for computing the address location of a multi-dimensional array element in
column major implementation is given below:
Address of A[i,j] = B + W (m (j – LBC) + (i – LBR))
Here, m represents the number of rows.
Example 2.9 A 10 ¥ 12 matrix is
implemented using array A[10][12]. If the
Check Point
base address of the array is 200 and the word
1. What is row-major order?
size is 2 then compute the address of the element
Ans. It is the memory representation of a
A[4,7] in:
two-dimensional array in row-by-row fashion.
(a) Row major order
(b) Column major order 2. What is column-major order?
Assume that the lower bound of both row and Ans. It is the memory representation of a two-
column indices is 1. dimensional array in column-by-column fashion.

Solution (a) Row major order


Address of A[i,j] = B + W (n (i – LBR) + (j – LBC))
Address of A[4,7] = 200 + 2 (12 (4 – 1) + (7 – 1))
= 200 + 2 (42)
= 284
(b) Column major order
Address of A[i,j] = B + W (m (j – LBC) + (i – LBR))
Address of A[4,7] = 200 + 2 (10 (7 – 1) + (4 – 1))
= 200 + 2 (63)
= 326

2.8 REALIZING MATRICES USING TWO-DIMENSIONAL ARRAYS


Two-dimensional arrays are most commonly used for
realizing matrices. The first subscript signifies the rows
of a matrix while the second subscript signifies the
Mind Jog
columns. Operation on these array-represented matrices What is a square matrix?
can be performed through simple programming. It is the matrix that has equal number of
Figure 2.5 depicts the realization of a matrix through rows and columns.
a two-dimensional array:

2.16 Data Structures


Note Adequate checks must be included in a program to ensure that non-compatible matrices
are not operated.

Check Point
1. What is matrix addition?
Ans. It is the task of adding the relative elements of two matrices.
2. What is matrix transpose?
Ans. It is the task of changing the rows into columns and columns into rows.

Solved Problems
Problem 2.1 Consider the following array of integers:
35 18 7 12 5 23 16 3 1
Create a snapshot of the above array for the following operations:
Inserting element 99 at index location 2.
Deleting the first element of the array.
Solution
Array contents after insertion: 35 18 99 7 12 5 23 16 3 1
Array contents after deletion: 18 7 12 5 23 16 3 1

Problem 2.2 Consider the following array of integers:


74 39 35 32 97 84
Create a snapshot of the above array after the sorting operation is performed on it.
Solution
Initial array 74 39 35 32 97 84
Sorted array 32 35 39 74 84 97

Problem 2.3 Consider the following array of integers:


74 39 35 32 97 84
How many elements would need to be traversed before search operation is completed on the
following items:
32
83
Solution
4
6

Problem 2.4 Consider the following array of integers::


35 54 12 18 23 15 45 38

2.26 Data Structures


Deduce the address of the 4th element (index location 3), if the base address is 3000. Assume that
the word size is 2.
Solution Address of arr[3] = 3000 + 2 * 3
= 3000 + 6
= 3006

Problem 2.5 A two-dimensional array A[5][10] is implemented in row order manner in the memory.
Deduce the address of the A[3][5] element, if the base address of the array is 3000 and the word size is
2. Assume the lower bound of row and column indices to be 1.
Solution Address of A[i,j] = B + W (n (i – LBR) + (j – LBC))
Address of A[3,5] = 3000 + 2 (10 (3 – 1) + (5 – 1))
= 3000 + 2 (24)
= 3048

Problem 2.6 Solve Problem 5 in case of column order implementation.


Solution Address of A[i,j] = B + W (m (j – LBC) + (i – LBR))
Address of A[3,5] = 3000 + 2 (5 (3 – 1) + (5 – 1))
= 3000 + 2 (14)
= 3028

Summary
 Arrays are characterized as one-dimensional and multi-dimensional arrays.
 One-dimensional arrays are stored at consecutive locations in memory.
 Array traversal involves visiting the array elements and storing or retrieving values from it.
 Insertion is the task of adding an element into an existing array while deletion is the task of
removing an element from the array.
 Sorting involves arranging the elements of an array in a specific order or sequence.
 Searching involves locating a specific element in an array.
 Multi-dimensional array either stores the array elements in row major order or column major
order.
 Two-dimensional arrays are most commonly used for realizing matrices.
 Common operations performed on matrices are: addition, subtraction, multiplication, transpose.

Key Terms
 Array An array is defined as a collection of same type elements, such as integers, characters,
strings, structures, and so on.
 One-dimensional array It is a group of same type data elements, such as integers, floats, or
characters.
 Multi-dimensional array It is a group of data elements, where each element is itself an array.
 Array subscript It is the index identifier used to identify individual array elements.

Arrays 2.27
 Base address It is the memory address of the first element of an array.
 Sorting It involves arranging the elements of an array in a specific order or sequence.
 Searching It involves locating a specific element in an array.
 Row major order It is the memory representation of a two-dimensional array in row-by-row
fashion.
 Column major order It is the memory representation of a two-dimensional array in column-
by-column fashion.

Multiple-Choice Questions
2.1 Which of the following is not true about arrays?
(a) It uses a single name for referencing all the array elements.
(b) Its name can be used as a pointer to the first array element.
(c) It performs automatic bound checking on its own.
(d) It stores the different elements at consecutive memory locations.
2.2 Which of the following is an incorrect array representation?
(a) {2, 5, 6, 1, 9}
(b) {2.5, 5.5, 6.8, 1.0, 9.7}
(c) {‘S’, ‘J’, 6, ‘4’, ‘P’}
(d) All of the above are correct
2.3 While performing array insertion, the elements to the right of the point of insertion are required
to be moved in which direction?
(a) Right
(b) Left
(c) They are not required to be moved
(d) None of the above
2.4 While performing array deletion, the elements to the right of the point of deletion are required
to be moved in which direction?
(a) Right
(b) Left
(c) They are not required to be moved
(d) None of the above
2.5 Which of the following is a representation of multi-dimensional array in memory?
(a) Row major order
(b) Column major order
(c) Sequential order
(d) Both (a) and (b)
2.6 Address of A[i,j] = B + W (m (j – LBC) + (i – LBR)) is the formula for computation of memory
addresses of which of the following array representations?
(a) Column major order
(b) Row major order
(c) Sequential order
(d) None of the above

2.28 Data Structures


2.7 A multi-dimensional array A[3][7] possesses how many number of elements?
(a) 10 (b) 21
(c) 17 (d) None of the above
2.8 Transposing a matrix refers to
(a) converting rows into columns
(b) converting columns into rows
(c) Both (a) and (b)
(d) None of the above

Review Questions
2.1 What is an array? What are its various types?
2.2 Explain the representation of a one-dimensional array in memory with the help of an illustration.
2.3 What is array traversal? Why is it used?
2.4 What are the typical operations associated with arrays? Explain.
2.5 Write an algorithm for deleting an element at index location k in the array A[N].
2.6 What is the difference between sorting and searching?
2.7 Explain the representation of a two-dimensional array in memory with the help of an illustration.
2.8 Explain with the help of an illustration how a 2 ¥ 2 matrix is stored in memory using column
major order representation.
2.9 What is matrix multiplication? Explain with the help of an example.
2.10 What is the significance of transposing a matrix?

Programming Exercises
2.1 Write a C program to find the smallest element in an integer array.
2.2 Write a C program to read a value and insert it at the middle of an integer array.
2.3 Write a C program to sort an array of 10 integers.
2.4 Write a C program to demonstrate searching on an array of ten integers.
2.5 Write a C program to show how matrices are realized using two-dimensional arrays.
2.6 Write a C program to perform matrix subtraction.
2.7 Write a C program to perform transpose of a matrix.

Answers to Multiple-Choice Questions

2.1 (c) 2.2 (c) 2.3 (a) 2.4 (b) 2.5 (d)
2.6 (a) 2.7 (b) 2.8 (c)

Arrays 2.29

You might also like