0% found this document useful (0 votes)
20 views105 pages

unit 1.2 Array

Uploaded by

aThArvA SiNgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
20 views105 pages

unit 1.2 Array

Uploaded by

aThArvA SiNgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 105

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: 23CSH-241
Chapter: Array
Subject Coordinator:
Ms. Upasana Tiwari
(15791)

Array DISCOVER . LEARN .


Lecture No. 1.1 EMPOWER
Index
• Basic terminology
• Linear arrays and their representation
• Traversing Linear Array
Basic terminology
• Data: Data are simply values or sets of values.
• Data items: Data items refers to a single unit of
values.
• Data items that are divided into sub-items are called
Group items. Ex: An Employee Name may be divided
into three sub items- first name, middle name, and last
name.
• Data items that are not able to divide into sub-items are
called Elementary items. Ex: SSN
Continue..
• Entity: An entity is something that has certain
attributes or properties which may be assigned
values.
• The values may be either numeric or non-numeric. Ex:
Attributes- Names, Age, Sex, SSN Values-
Rohland Gail, 34, F, 134-34-5533
• Entities with similar attributes form an entity set. Each
attribute of an entity set has a range of values, the set of
all possible values that could be assigned to the
particular attribute. The term “information” is
sometimes used for data with given attributes, of, in
other words meaningful or processed data.
Continue..
• Field : is a single elementary unit of information
representing an attribute of an entity.
• Record :is the collection of field values of a given
entity.
• File : is the collection of records of the entities in a
given entity set.
• Each record in a file may contain many field items but
the value in a certain field may uniquely determine the
record in the file. Such a field K is called a primary key
and the values k1, k2, ….. in such a field are called keys
or key values.
Continue..
• Records may also be classified according to length.
• A file can have fixed-length records or variable-
length records.
• In fixed-length records, all the records contain the same
data items with the same amount of space assigned to
each data item.
• In variable-length records file records may contain
different lengths.
Linear arrays and their representation

• Arrays :
The simplest type of data structure is a linear (or one
dimensional) array. A list of a finite number n of similar
data referenced respectively by a set of n consecutive
numbers, usually 1, 2, 3 . . . . . . . n.
if A is chosen the name for the array, then the elements of
A are denoted by subscript notation a1, a2, a3….. an
or by the parenthesis notation
A (1), A (2), A (3) . . . . . . A (n)
or by the bracket notation
A [1], A [2], A [3] . . . . . . A [n]
Continue..
• Example 1: A linear array STUDENT consisting of the
names of six students is pictured in below figure. Here
STUDENT [1] denotes John Brown, STUDENT [2] denotes
Sandra Gold, and so on.

Linear arrays are called one-dimensional arrays because


each element in such an array is referenced by one
subscript.
Continue..
• An Array is defined as, an ordered set of similar
data items. All the data items of an array are stored
in consecutive memory locations.
• The data items of an array are of same type and
each data items can be accessed using the same
name but different index value.
• An array is a set of pairs, , such that each index has
a value associated with it. It can be called as
corresponding or a mapping.
Continue..

• Here, list is the name of array. By using, list [0] to


list [4] the data items in list can be accessed
Continue..
Array in C Declaration:
• A one dimensional array in C is declared by adding brackets
to the name of a variable.
Ex: int list[5], *plist[5];
• The array list[5], defines 5 integers and in C array start at
index 0, so list[0], list[1], list[2], list[3], list[4] are the names
of five array elements which contains an integer value.
• The array *plist[5], defines an array of 5 pointers to
integers. Where, plist[0], plist[1], plist[2], plist[3], plist[4]
are the five array elements which contains a pointer to an
integer
Continue..
Continue..
Implementation:
• When the complier encounters an array declaration,
list[5], it allocates five consecutive memory locations.
Each memory is enough large to hold a single integer.
• The address of first element of an array is called Base
Address. Ex: For list[5] the address of list[0] is called
the base address.
• If the memory address of list[i] need to compute by
the compiler, then the size of the int would get by
sizeof (int), then memory address of list[i] is as
follows:
Continue..
list[i] = α + i * sizeof (int)
Traversing Linear Array

• Traversal operation in array or simply traversing an array means, Accessing


or printing each element of an array exactly once so that the data item
(values) of the array can be checked or used as part of some other
operation or process (This accessing and processing is sometimes called
“visiting” the array).
• Algorithm for Traversing an Array:
Step 01: Start
Step 02: [Initialize counter variable. ] Set i = LB.
Step 03: Repeat for i = LB to UB.
Step 04: Apply process to arr[i].
Step 05: [End of loop. ]
Step 06: Stop
Continue..
• Variables used:
i : Loop counter or counter variable for the for loop.
arr : Array name.
LB : Lower bound. [ The index value or
simply index of the first element of an array is called
its lower bound ]
UB : Upper bound. [ The index of the last element is
called its upper bound ]
Continue..
Flowchart for Traversing an Array:
Implementation in C:

// writing a program in C to perform traverse operation on an array

void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3}; //declaring and initializing array
size=sizeof(arr)/sizeof(arr[0]); //sizeof(arr) will give 20 and sizeof(arr[0]) will give 4
printf("The array elements are: ");
for(i=0;i<size;i++)
printf("\narr[%d]= %d", i, arr[i]);
}
Output:
The array elements are:
arr[0]= 1
arr[1]= -9
arr[2]= 17
arr[3]= 4
arr[4]= -3
Index
• Insertion in arrays
• Deletion in arrays
Insertion in arrays

• Insert operation in Array or inserting elements in an


Array simply means, Adding one or more elements
at specified index/indices to the existing elements
of an array.
• How do you insert an element in an array at a
given position?
• Let’s see how you can insert an element in an array at a
given position step-by-step:
Continue..
• Variables we are using here:
1. arr : Name of the array.
2. size : Size of the array (i.e., total number of elements in the
array)
3. i : Loop counter or counter variable for the for loop.
4. x : The data element to be insert.
5. pos : The position where we wish to insert the element.

The following algorithm inserts a data element x into a given


position pos (position specified by the programmer) in a linear array arr.
Continue..
• Algorithm to Insert an element in an Array:
Step 01: Start
Step 02: [Reset size of the array. ] set size = size + 1
Step 03: [Initialize counter variable. ] Set i = size - 1
Step 04: Repeat Step 05 and 06 for i = size -1 to i >= pos -1
Step 05: [Move ith element forward. ] set arr[i+1] = arr[i]
Step 06: [Decrease counter. ] Set i = i - 1
Step 07: [End of step 04 loop. ]
Step 08: [Insert element. ] Set arr[pos-1] = x
Step 09: Stop
In the above Algorithm, Step 2 to step 6 creates an empty space in the
array by moving forward one location each element from the position on.
Continue..
Continue..
Implementation in C:
// writing a program in C to insert an element in an array

void main()
{
int i, size, x, pos;
int arr[]={2, 4, 6, 8, 12};
size=sizeof(arr)/sizeof(arr[0]);
printf("The array elements before insertion operation:\n");
for(i=0;i<size;i++)
printf("arr[%d] = %d\n",i, arr[i]);
printf("Enter the element to be insert: ");
scanf("%d", &x);
printf("Enter the position where you want to insert the element: ");
scanf("%d", &pos);
size = size + 1;
printf("The array elements after insertion operation:\n");
for(i=size-1;i>=pos-1;i--)
arr[i+1]=arr[i];
arr[pos-1]=x;
for(i=0;i<size;i++)
printf("arr[%d] = %d\n",i, arr[i]);
}
Continue..
Output:
The array elements before insertion operation:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 12
Enter the element to be insert: 10
Enter the position where you want to insert the element: 5
The array elements after insertion operation:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 10
arr[5] = 12
Deletion in arrays
• Deletion operation in Array or delete an element from
an Array simply means, Removing an existing element
from a specific position of an array.
• Let’s see how you can delete an element from a specific
position of an array step-by-step:
Continue..
Variables we are using here:
1. a : Name of the array.
2. size : Size of the array (i.e., total number of
elements in the array)
3. i : Loop counter or counter variable for
the for loop.
4. pos : The position from where we wish to delete
the element.
The following algorithm deletes a data element from
a specific position pos (position specified by
the programmer) of a linear array ‘a‘.
Continue..
Algorithm to Delete an element from an Array:
Step 01: Start
Step 02: [Initialize counter variable. ] Set i = pos - 1
Step 03: Repeat Step 04 and 05 for i = pos - 1 to i < size
Step 04: [Move ith element backward (left). ] set a[i] = a[i+1]
Step 05: [Increase counter. ] Set i = i + 1
Step 06: [End of step 03 loop. ]
Step 07: [Reset size of the array. ] set size = size - 1
Step 08: Stop
In the above algorithm, step 2 to step 5 shifts (moves) each such
element one location (position) backward (left) whose position is
greater than the position of the element which we wish to delete.
Continue..
Continue..
Implementation in C:
// writing a program in C to delete an element from an array
void main()
{
int i, size, pos;
int a[]={2, 4, 6, 8, 12};
size=sizeof(a)/sizeof(a[0]);
printf("The array elements before deletion operation:\n");
for(i=0;i<size;i++)
printf("a[%d] = %d\n", i, a[i]);
printf("Enter the position from where you wish to delete the element: ");
scanf("%d", &pos);
printf("The array elements after deletion operation:\n");
for(i=pos-1;i<size;i++)
a[i]=a[i+1];
size = size - 1;
for(i=0;i<size;i++)
printf("a[%d] = %d\n", i, a[i]);
}
Continue..
Output:
The array elements before deletion operation:
a[0] = 2
a[1] = 4
a[2] = 6
a[3] = 8
a[4] = 12
Enter the position from where you wish to delete the element: 3
The array elements after deletion operation:
a[0] = 2
a[1] = 4
a[2] = 8
a[3] = 12
Index
• Binary Search
• Complexity Analysis
Binary Search

• It is a searching technique. It is based upon Divide


and conquer strategy. Binary search is applicable
only on sorted data. It takes O(log n) time for
completion.
• Working of Binary Search
• Let us understand the working of a binary search
algorithm with the help of an example.
• Consider a sorted array having 10 elements as shown:
Continue..
Suppose we wish to search 38 in the array.
• Step 1: Find the middle element of the array.
index(Middle) = index(low) + index(high – low)/2.
Here, middle = 0 + (9-0)/2 = 4 i.e. the element at
the 4th index i.e. 25.
Continue..
• Step 2: Compare 38 with the middle element. 38 >
25 So, discard the first half.
• Step 3: Select the send half of the array. For the
second half, low = middle+1 as shown:
Continue..
• Step 4: Find the middle element of this smaller array
which comes out to 32. Compare 38 with 32.
38 > 32 Thus, discard the first half of this array.
• Step 5: Select the remaining half of the array by doing
low = middle+1 as shown:

Finally, we have found 38 and thus the algorithm will


halt here.
Continue..
Let us understand the working of a binary
search algorithm with the help of another
example.
Continue..
• Algorithm:
Complexity Analysis

• Now, let's see the time complexity of Binary search


in the best case, average case, and worst case. We
will also see the space complexity of Binary search.
1. Time Complexity
Continue..
• Best Case Complexity - In Binary search, best case occurs
when the element to search is found in first comparison, i.e.,
when the first middle element itself is the element to be
searched. The best-case time complexity of Binary search
is O(1).
• Average Case Complexity - The average case time
complexity of Binary search is O(logn).
• Worst Case Complexity - In Binary search, the worst case
occurs, when we have to keep reducing the search space till
it has only one element. The worst-case time complexity of
Binary search is O(logn).
• 2. Space Complexity
The space complexity of binary search is O(1).
Index
• Sorting
• Insertion Sort
• Complexity Analysis
Sorting

• Sorting is a process of ordering or placing a list of


elements from a collection in some kind of order.
• It is nothing but storage of data in sorted order.
Sorting can be done in ascending and descending
order.
• It arranges the data in a sequence which makes
searching easier.
Continue..
• Sorting Techniques
Sorting technique depends on the situation. It
depends on two parameters.

1. Execution time of program that means time


taken for execution of program.
2. Space that means space taken by the program.

Sorting techniques are differentiated by their


efficiency and space requirements.
Insertion sort

• Insertion sort is a simple sorting algorithm that works similar to the


way you sort playing cards in your hands. The array is virtually split
into a sorted and an unsorted part. Values from the unsorted part
are picked and placed at the correct position in the sorted part.
• Step by Step Process
The insertion sort algorithm is performed using the following steps...
Step 1 - Assume that first element in the list is in sorted portion and
all the remaining elements are in unsorted portion.
Step 2: Take first element from the unsorted portion and insert that
element into the sorted portion in the order specified.
Step 3: Repeat the above process until all the elements from the
unsorted portion are moved into the sorted portion.
Continue..
• Following is the sample code for insertion sort…
Continue..
Example
Continue..
Continue..
Continue..
Complexity Analysis

• Now, let's see the time complexity of insertion sort


in best case, average case, and in worst case. We
will also see the space complexity of insertion sort.
1. Time Complexity
Case Complexity
Best Case O(n)
Average Case O(n2)
Worst Case O(n2)
Continue..
• Best Case Complexity - It occurs when there is no sorting required, i.e.
the array is already sorted. The best-case time complexity of insertion
sort is O(n).
• Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of insertion sort is O(n2).
• Worst Case Complexity - It occurs when the array elements are required
to be sorted in reverse order. That means suppose you have to sort the
array elements in ascending order, but its elements are in descending
order. The worst-case time complexity of insertion sort is O(n2).
2. Space Complexity
The space complexity of insertion sort is O(1). It is because, in insertion
sort, an extra variable is required for swapping.
Index
• Selection Sort
• Complexity Analysis
Selection Sort

• Selection Sort algorithm is used to arrange a list of


elements in a particular order (Ascending or Descending).
• In selection sort, the first element in the list is selected
and it is compared repeatedly with all the remaining
elements in the list. If any element is smaller than the
selected element (for Ascending order), then both are
swapped so that first position is filled with the smallest
element in the sorted order.
• Next, we select the element at a second position in the
list and it is compared with all the remaining elements in
the list. If any element is smaller than the selected
element, then both are swapped. This procedure is
repeated until the entire list is sorted.
Continue..
Step by Step Process
The selection sort algorithm is performed using the following
steps...
Step 1 - Select the first element of the list (i.e., Element at
first position in the list).
Step 2: Compare the selected element with all the other
elements in the list.
Step 3: In every comparision, if any element is found smaller
than the selected element (for Ascending order), then both
are swapped.
Step 4: Repeat the same procedure with element in the next
position in the list till the entire list is sorted.
Continue..
• Selection Sort Logic
Continue..
Example
Continue..
Continue..
Continue..
Complexity Analysis

• Now, let's see the time complexity of selection sort


in best case, average case, and in worst case. We
will also see the space complexity of the selection
sort.
1. Time Complexity
Case Complexity
Best Case O(n2)
Average Case O(n2)
Worst Case O(n2)
Continue..
• Best Case Complexity - It occurs when there is no sorting required, i.e.
the array is already sorted. The best-case time complexity of selection
sort is O(n2).
• Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of selection sort is O(n2).
• Worst Case Complexity - It occurs when the array elements are required
to be sorted in reverse order. That means suppose you have to sort the
array elements in ascending order, but its elements are in descending
order. The worst-case time complexity of selection sort is O(n2).
2. Space Complexity
• The space complexity of selection sort is O(1). It is because, in selection
sort, an extra variable is required for swapping.
Index
• Bubble Sort
• Complexity Analysis of Bubble Sort
• Quick Sort
• Complexity Analysis of Quick Sort
Bubble Sort

• Bubble sort works on the repeatedly swapping of adjacent


elements until they are not in the intended order.
• It is called bubble sort because the movement of array
elements is just like the movement of air bubbles in the
water.
• Bubbles in water rise up to the surface; similarly, the array
elements in bubble sort move to the end in each iteration.
• Bubble short is majorly used where –
• complexity does not matter
• simple and shortcode is preferred
Continue..
• Algorithm
Example:
Complexity Analysis

• The worst-case condition for bubble sort occurs when elements of the array
are arranged in decreasing order.
In the worst case, the total number of iterations or passes required to sort a
given array is (n-1). where ‘n’ is a number of elements present in the array.
At pass 1 : Number of comparisons = (n-1)
Number of swaps = (n-1)
At pass 2 : Number of comparisons = (n-2)
Number of swaps = (n-2)
At pass 3 : Number of comparisons = (n-3)
Number of swaps = (n-3)
.
.
.
At pass n-1 : Number of comparisons = 1
Number of swaps = 1
Continue..
• Now , calculating total number of comparison
required to sort the array
= (n-1) + (n-2) + (n-3) + . . . 2 + 1
= (n-1)*(n-1+1)/2 { by using sum of N natural
Number formula }
= n (n-1)/2
• For Worst case:
• Total number of swaps = Total number of
comparison
Total number of comparison (Worst case) = n(n-1)/2
Total number of swaps (Worst case) = n(n-1)/2
Continue..
• Worst and Average Case Time Complexity: O(N2).
The worst case occurs when an array is reverse
sorted.
• Best Case Time Complexity: O(N). The best case
occurs when an array is already sorted.
• Auxiliary Space: O(1)
Quick Sort
• Quicksort is the widely used sorting algorithm that
makes n log n comparisons in average case for
sorting an array of n elements.
• It is a faster and highly efficient sorting algorithm.
This algorithm follows the divide and conquer
approach.
• Divide and conquer is a technique of breaking
down the algorithms into subproblems, then
solving the subproblems, and combining the results
back together to solve the original problem.
Continue..
• Quick sort picks an element as pivot, and then it
partitions the given array around the picked pivot
element. In quick sort, a large array is divided into
two arrays in which one holds values that are
smaller than the specified value (Pivot), and
another array holds the values that are greater than
the pivot.
• After that, left and right sub-arrays are also
partitioned using the same approach. It will
continue until the single element remains in the
sub-array.
Continue..
Choosing the pivot
• Picking a good pivot is necessary for the fast
implementation of quicksort. However, it is typical
to determine a good pivot. Some of the ways of
choosing a pivot are as follows –
• Pivot can be random, i.e. select the random pivot from
the given array.
• Pivot can either be the rightmost element of the
leftmost element of the given array.
• Select median as the pivot element.
Continue..
Algorithm:
//start –> Starting index, end --> Ending index
Quicksort(array, start, end)
{
if (start < end)
{
pIndex = Partition(A, start, end)
Quicksort(A,start,pIndex-1)
Quicksort(A,pIndex+1, end)
}
}
Continue..
Partition Algorithm:
partition (array, start, end)
{
// Setting rightmost Index as pivot
pivot = arr[end];

i = (start - 1) // Index of smaller element and indicates the


// right position of pivot found so farfor (j = start; j <= end- 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[end])
return (i + 1)
}
Continue..
Example for Partition:
Continue..
Example of Quick Sort:
Continue..
Continue..
Complexity Analysis

• Now, let's see the time complexity of quicksort in


best case, average case, and in worst case. We will
also see the space complexity of quicksort.
1. Time Complexity

Case Complexity
Best Case O(n*logn)
Average Case O(n*logn)
Worst Case O(n2)
Continue..
• Best Case Complexity - In Quick sort, the best-case occurs
when the pivot element is the middle element or near to
the middle element. The best-case time complexity of quick
sort is O(n*logn).
• Average Case Complexity - It occurs when the array
elements are in jumbled order that is not properly
ascending and not properly descending. The average case
time complexity of quick sort is O(n*logn).
• Worst Case Complexity - In quick sort, worst case occurs
when the pivot element is either greatest or smallest
element. Suppose, if the pivot element is always the last
element of the array, the worst case would occur when the
given array is sorted already in ascending or descending
order. The worst-case time complexity of quick sort is O(n2).
Continue..
• Though the worst-case complexity of quicksort is
more than other sorting algorithms such as Merge
sort and Heap sort, still it is faster in practice.
Worst case in quick sort rarely occurs because by
changing the choice of pivot, it can be implemented
in different ways. Worst case in quicksort can be
avoided by choosing the right pivot element.
2. Space Complexity
The space complexity of quicksort is O(n*logn).
Index
• Merging arrays
• Merge Sort
• Complexity Analysis
Merging Arrays

Merging two arrays means combining two separate


arrays into one single array. For instance, if the first
array consists of 3 elements and the second array
consists of 5 elements then the resulting array
consists of 8 elements. This resulting array is known
as a merged array.
Below is the pictorial representation of the merged
array.
Continue..
Algorithm
1. Start
2. Declare two arrays.
3. Initialize these two arrays.
4. Declare another array that will store the merged arrays.
5. The size of the merged array should be equal to the sum of the other two arrays.
6. For loop will help to iterate every element present in the first array.
7. Condition inside the for loop (i < Size) will ensure the compiler, not exceed the
array limit.
8. Inside the second for loop assign each and every array element to the Merged
array.
9. Now, print the merged array.
10. Stop
Merge Sort

• Merge sort is similar to the quick sort algorithm as it


uses the divide and conquer approach to sort the
elements. It is one of the most popular and efficient
sorting algorithm. It divides the given list into two equal
halves, calls itself for the two halves and then merges
the two sorted halves.
• The sub-lists are divided again and again into halves
until the list cannot be divided further. Then we
combine the pair of one element lists into two-element
lists, sorting them in the process. The sorted two-
element pairs is merged into the four-element lists, and
so on until we get the sorted list.
Continue..

Note: The merge() function is used for merging two halves


Continue..
Algorithm for mergsort function:
mergesort(array, left, right)
if left < right
set mid = (left + right)/2
mergesort(array, left, right)
mergesort(arr, mid + 1, right)
MERGE (array, left, mid, right)
end of if
END mergesort
• Example
Continue..
Complexity Analysis

• Now, let's see the time complexity of merge sort in


best case, average case, and in worst case. We will
also see the space complexity of the merge sort.
1. Time Complexity

Case Complexity
Best Case O(n*logn)
Average Case O(n*logn)
Worst Case O(n*logn)
Continue..
• Best Case Complexity - It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of merge sort
is O(n*logn).
• Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of merge sort is O(n*logn).
• Worst Case Complexity - It occurs when the array elements are required
to be sorted in reverse order. That means suppose you have to sort the
array elements in ascending order, but its elements are in descending
order. The worst-case time complexity of merge sort is O(n*logn).
2. Space Complexity
The space complexity of merge sort is O(n). It is because, in merge sort,
an extra variable is required for swapping.
Index
• Multi-dimensional arrays
• Representation of Multi-dimensional arrays
Course Outcome
• CO1: Identify how arrays, linked lists, stacks,
queues, trees, and graphs are represented in
memory and used by algorithms
Course Objective
• To develop understanding among the students
about the concept of the data structures
• To demonstrate methods to perform operations on
different data structures
• To analyze the various operations for complexity
Multi-dimensional arrays
• A multi-dimensional array can be termed as an array
of arrays that stores homogeneous data in tabular
form. Data in multidimensional arrays are stored in
row-major order.
• The general form of declaring N-dimensional
arrays is:
data_type array_name[size1][size2]....[sizeN];
data_type: Type of data to be stored in the array.
array_name: Name of the array
size1, size2,… ,sizeN: Sizes of the dimension
Continue..
Examples:
Two dimensional array: int two_d[10][20];
Three dimensional array: int three_d[10][20][30];
• The total number of elements that can be stored in a
multidimensional array can be calculated by
multiplying the size of all the dimensions.
For example:
• The array int x[10][20] can store total (10*20) = 200
elements.
• Similarly array int x[5][10][20] can store total (5*10*20) =
1000 elements.
Continue..
Two-Dimensional Array
Two – dimensional array is the simplest form of a
multidimensional array. We can see a two –
dimensional array as an array of one-dimensional
array for easier understanding.
• The basic form of declaring a two-dimensional array
of size x, y:
Syntax:
data_type array_name[x][y];
Here, data_type is the type of data to be stored.
Continue..
• We can declare a two-dimensional integer array say
‘x’ of size 10,20 as: int x[10][20];
• Elements in two-dimensional arrays are commonly
referred to by x[i][j] where i is the row number and
‘j’ is the column number.
• A two – dimensional array can be seen as a table
with ‘x’ rows and ‘y’ columns where the row
number ranges from 0 to (x-1) and the column
number ranges from 0 to (y-1).
Representation
• A two – dimensional array ‘x’ with 3 rows and 3
columns is shown below:
Continue..
• Initializing Two – Dimensional Arrays: There are
various ways in which a Two-Dimensional array can
be initialized.
• First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array has 3 rows and 4 columns. The elements
in the braces from left to right are stored in the table also
from left to right. The elements will be filled in the array in
order, the first 4 elements from the left in the first row, the
next 4 elements in the second row, and so on.
• Second Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
Continue..
• Third Method:

• Fourth Method(Dynamic Allocation):


Representation
• Three-Dimensional Array
Continue..
• Initializing Three-Dimensional Array: Initialization in a
Three-Dimensional array is the same as that of Two-
dimensional arrays. The difference is as the number of
dimensions increases so the number of nested braces
will also increase.
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
Method 2(Better):
int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} } };
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU

For queries
Email: vishal.e12820@cumail.in

105

You might also like