DSA-Ch3Arrays
DSA-Ch3Arrays
Chapter 3:
Array / Linear Array
Contents
3.1 Introduction
3.2 Representation of Array in memory
3.3 Operations of arrays
3.3 Traversing Linear Array
3.4 Inserting and Deleting
3.5 Searching Linear Array
3.5.1 Linear Search
3.5.2 Binary Search
3.6 Multidimensional array.
3.7 2-D Arrays
3.7 Representation of 2-D array in memory
3.1 Introduction
• An array is a collection of variables of the same
type that are referenced by a common name.
OR
• A list of finite number n of similar(homogeneous) data
elements.
• A[1],A[2],A[3]…..A[N]
• The number K in A[K] is called a subscript and A[K] is
called subscripted variable.
• Arrays are a way to group a number of items into larger
unit.
3.1 Introduction
• The elements of the array are referenced respectively
by an index set.
• The elements of the array are stored respectively in
successive memory locations.
• The number n of elements is called the length or size
of the array.
• LENGTH = UB-LB+1
• UB: Upper bound (largest index)
• LB: Lower
10 20 bound
30 5(smallest
2 0 index)
4 100 LB=0
0 1 2 3 4 5 6 7 UB=7
Representation of Array in Memory
• Memory of computer is simply a sequence of addressed
locations.
1001 1002 1003 1004 1005
… … … … …
… … … … 1015
• The binary search algorithm can only be used with sorted array.
• Eliminates one half of the elements in the array being searched after
each comparison.
• The algorithm locates the middle element of the array and compares
it to the search key.
• If they are equal, the search key is found and array subscript of that
element is returned.
Binary Search:
• If the search key is less than the middle element of
array, the first half of the array is searched.
OUTPUT :
Enter 5 elements of an array :
39518
Largest element in array : 9
Smallest element in array : 1
Program 4 :
• WAP & Algo: to display the sum and average of
elements of an array
OUTPUT :
Enter 5 elements of an array :
2 4 6 8 10
Sum : 30.00
Average : 6.00
Program 5 :
• WAP & Algo: to insert a number in an array
OUTPUT :
Enter size of array (max. 10) : 5
Enter 5 elements of array :
2 4 8 10 12
Original array is :
2 4 8 10 12
Enter the element to be inserted : 6
Enter the position of insertion : 3
New array after insertion :
2 4 6 8 10 12
Program 6 :
• WAP & Algo: to delete a number from an array
• Output:
Enter the size of array (max. 10) : 5
Enter 5 elements of an array :
2 4 6 8 10
Original array is :
2 4 6 8 10
Enter the element to delete : 6
New Array after deletion :
2 4 8 10
Multi-Dimensional
Arrays
Multidimensional arrays:
• In Multi-D arrays, elements referenced by more
than one subscript.
A[1][2]
Implementation of Two-dimensional array in
Memory
• While storing the elements of a 2-D array in
memory, these are allocated contiguous memory
locations.
• A two-dimensional array can be implemented in a
programming language in two ways :
• 1. Row-major implementation
• 2. Column-major implementation
Row-major implementation :
• Row-major implementation is a linearization
technique in which elements of array are read
from the keyboard row-wise.
• i.e. the complete first row is stored, then the
complete second row is stored and so on.
• For example, an array A [3] [3]is stored in the
memory as shown in Fig.(1) below :
Row-major implementation :
• The storage can be clearly understood by arranging
array as matrix as shown below :
Column-major implementation :
• Column-major implementation is a linearization
technique in which elements of array are read from
the keyboard column-wise.
• i.e. the complete first column is stored, then the
complete second column is stored and so on.
• For example, an array a[3][3] is stored in the
memory as shown in Fig.(1) below :
Column-major implementation :
• The storage can be clearly understood by arranging
array as matrix as shown below :
Double Dimensional Array declartion :
• data_type array_name[rowSize][colSize];
• For e.g.
• int A[4][3];
• Where int is data type, A is array variable_name, 4
is row size and 3 is column size.
Double Dimensional Array initialization :
• data_type array_name[rowSize][colSize]={
{1st row elements},
{2nd row elements},
………
};
• For e.g.
• int A[4][3]={{1,2,3}, {4,5,6,}, {7,8,9}, {10,11,12}};
Array input from user :
Row major form Column major form
for(r=0;r<4;r++) { for(c=0;c<3;c++) {
for(c=0;c<3;c++) { for(r=0;r<4;r++) {
cin>>a[r][c]; cin>>a[r][c];
} }
} }
Assignments
• Program 1 : Matrix Initialization & Output
OUTPUT :
• Given 4 * 3 matrix is :
1 2 3
4 5 6
7 8 9
10 11 12
Program 2 : Matrix Input & Output
OUTPUT :
• Enter elements of a 4 * 3 matrix :
1 2 3
4 5 6
7 8 9
10 11 12
• Given 4 * 3 matrix is :
1 2 3
4 5 6
7 8 9
10 11 12
Program 3 : Addition of Two 3 * 3 Matrices
• Enter elements of first 3 * 3 matrix :
1 2 3
4 5 6
7 8 9
• Enter elements of second 3 * 3 matrix :
2 3 4
5 6 7
8 9 10
• Addition of first two matrices :
3 5 7
9 11 13
15 17 19
Program 4 : Transpose of a 3 *3 matrix
• Enter elements of a 3 * 3 matrix :
1 2 3
4 5 6
7 8 9
• Original matrix is :
1 2 3
4 5 6
7 8 9
• Transpose of given matrix is :
1 4 7
2 5 8
3 6 9
Program 5 : Multiply two 3 * 3 matrices
• OUTPUT :
• Enter elements of first 3 * 3 matrix :
1 2 3
4 5 6
7 8 9
• Enter elements of second 3 * 3 matrix :
1 2 3
4 5 6
7 8 9
• Product of first two 3 * 3 matrices :
30 36 42
66 81 96
102 126 150