Professional Documents
Culture Documents
Ds-Unit 1
Ds-Unit 1
• θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n >
n0. }
Cases considered in analysis
• Best case : This is the analysis when the input is in favor of output. For
example, in sorting problem, the analysis done when the list is
already sorted is called best case analysis. This is not of much interest.
• Worst case : Worst case analysis is the longest running time of an
algorithm for a particular size of input. For sorting problem, the worst
case is when the number are already sorted but in reverse order. This
gives an upper bound of an algorithm.
• Average case : It is the general case (not best not worst). But in
practice, it is as bad as the worst case. It occurs fairly often (as often
as worst case). In sorting example, sorting any random list falls under
this category
Arrays
• 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:
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.
• 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.
ACCESSING THE ELEMENTS OF AN ARRAY
• Storing related data items in a single array enables the programmers
to develop concise and efficient programs.
• To access all the elements, we must use a loop. That is, we can access
all the elements of an array by varying the value of the subscript into
the array.
int i, marks[1 ];
for(i= ;i<1 ;i++)
marks[i] = –1;
• In the for loop, first the value of marks[0] is set to –1, then the value
of the index (i) is incremented and the next value, that is, marks[1] is
set to –1. The procedure continues until all the 10 elements of the
array are set to –1.
Inputting Values from the Keyboard
• An array can be initialized by inputting values from the keyboard. In
this method, a while/do–while or a for loop is executed to input the
value for each element of the array.
• For example,
• In the code, we start at the index i at 0 and input the value for the
first element of the array. Since the array has 10 elements, we must
input values for elements whose index varies from 0 to 9.
int i, marks[1 ];
for(i= ;i<1 ;i++)
scanf("%d", &marks[i]);
OPERATIONS ON ARRAYS
There are a number of operations that can be preformed on arrays.
These operations include:
• Traversing an array
• Inserting an element in an array
• Searching an element in an array
• Deleting an element from an array
• Merging two arrays
• Sorting an array in ascending or descending order
Write a program to read and display n numbers using an array.
void main()
{
int i, n, arr[20];
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
}
• Output: Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Write a program to read an array of n numbers and then find the smallest number.
void read_array(int arr[], int n);
int find_small(int arr[], int n);
void main()
{
int num[10], n, smallest;
printf("\n Enter the size of the array : ");
scanf("%d", &n);
read_array(num, n);
smallest = find_small(num, n);
printf("\n The smallest number in the array is = %d", smallest);
}
void read_array(int arr[10], int n) {
int i;
for(i=0;i<n;i++) {
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
} }
int find_small(int arr[10], int n)
{
int i = 0, small = arr[0];
for(i=1;i<n;i++)
{
if(arr[i] < small)
small = arr[i];
}
return small;
}
Output :
Enter the size of the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The smallest number in the array is = 1
Searching and Sorting
Searching
• Searching means to find whether a particular value is present in an
array or not. If the value is present in the array, then searching is said
to be successful and the searching process gives the location of that
value in the array.
• However, if the value is not present in the array, the searching
process displays an appropriate message and in this case searching is
said to be unsuccessful.
• There are two popular methods for searching the array elements:
linear search and binary search.
Linear Search
• Linear search, also called as sequential search, is a very simplemethod
used for searching an array for a particular value. It works by
comparing the value to be searched with every element of the array
one by one in a sequence until a match is found.
• Linear search is mostly used to search an unordered list of elements
(array in which data elements are not sorted).
• For example, if an array A[] is declared and initialized as,
int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5};
and the value to be searched is VAL = 7, then searching means to find
whether the value ‘7’ is present in the array or not.
• If yes, then it returns the position of its occurrence. Here, POS = 3
(index starting from 0).
• LINEAR_SEARCH(A, N, VAL)
Step 1: [INITIALIZE] SET POS = -1
Step 2: [INITIALIZE] SET I = 1
Step 3: Repeat Step 4 while I<=N
Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
[END OF LOOP]
Step 6: EXIT
SET I = I + 1
Step 5: IF POS = –1
PRINT VALUE IS NOT PRESENT
IN THE ARRAY
[END OF IF]
Complexity of Linear Search Algorithm
• Linear search executes in O(n) time where n is the number of
elements in the array.
• Obviously, the best case of linear search is when VAL is equal to the
first element of the array. In this case, only one comparison will be
made.
• Likewise, the worst case will happen when either VAL is not present in
the array or it is equal to the last element of the array. In both the
cases, n comparisons will have to be made.
• However, the performance of the linear search algorithm can be
improved by using a sorted array.
Program:
#include <stdio.h>
#include <conio.h>
#define size 20
void main() {
int arr[size], num, i, n, found = 0, pos = -1;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be searched : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
pos=i;
printf("\n %d is found in the array at position= %d", num,i+1);
break;
}
}
if (found == 0)
printf("\n %d does not exist in the array", num);
}
O/P: Enter the number of elements in the array : 5
Enter the elements: 10 20 30 40 50
Enter the number that has to be searched :30
30 is found in the array at position=3
Binary Search
• Binary search is a searching algorithm that works efficiently with a
sorted list. The mechanism of binary search can be better understood
by an analogy of a telephone directory.
• When we are searching for a particular name in a directory, we first
open the directory from the middle and then decide whether to look
for the name in the first part of the directory or in the second part of
the directory.
• Again, we open some page in the middle and the whole process is
repeated until we finally find the right name.
Consider an array A[] that is declared and initialized as
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
and the value to be searched is VAL = 9. The algorithm will be:
BEG = 0, END = 10, MID = (0 + 10)/2 = 5
Now, VAL = 9 and A[MID] = A[5] = 5
A[5] is less than VAL, therefore, we now search for the value in the second half
of the array. So,
we change the values of BEG and MID.
Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 = 8
VAL = 9 and A[MID] = A[8] = 8
A[8] is less than VAL, therefore, we now search for the value in the second half
of the segment.
So, again we change the values of BEG and MID.
Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9
Now, VAL = 9 and A[MID] = 9.
BINARY_SEARCH(A, lower_bound, upper_bound, VAL)
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS = - 1
Step 2: Repeat Steps 3 and 4 while BEG <= END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL
SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
Step 5: IF POS = -1
PRINT “VALUE IS NOT PRESENT IN THE ARRAY”
[END OF IF]
Step 6: EXIT
Complexity of Binary Search Algorithm
• The complexity of the binary search algorithm can be expressed as
f(n).
• where n is the number of elements in the array. The complexity of the
algorithm is calculated depending on the number of comparisons that
are made. In the binary search algorithm, we see that with each
comparison, the size of the segment where search has to be made is
reduced to half.
• Thus, we can say that, in order to locate a particular value in the
array, the total number of comparisons that will be made is given as
• 2f(n) > n or f(n) = log2n
Program:
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements");
scanf("%d",&n);
printf("Enter %d integers", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list", key);
return 0;
}
Output:
Enter number of elements 5
Enter 5 integers
10 20 30 40 50
Enter value to find 40
40 found at location 4
SORTING
• Sorting means arranging the elements of an array so that they are placed in
some relevant order which may be either ascending or descending.
• That is, if A is an array, then the elements of A are arranged in a sorted
order (ascending order) in such a way that A[0] < A[1] < A[2] < ...... < A[N].
So we swap them.
We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
This process goes on until all the unsorted values are covered in a sorted sub-list. Now we
shall see some programming aspects of insertion sort.
Algorithm
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
Complexity of Insertion Sort
• For insertion sort, the best case occurs when the array is already sorted. In
this case, the running time of the algorithm has a linear running time (i.e.,
O(n)). This is because, during each iteration, the first element from the
unsorted set is compared only with the last element of the sorted set of
the array.
• Similarly, the worst case of the insertion sort algorithm occurs when the
array is sorted in the reverse order. In the worst case, the first element of
the unsorted set has to be compared with almost every element in the
sorted set.
• Furthermore, every iteration of the inner loop will have to shift the
elements of the sorted set of the array before inserting the next element.
Therefore, in the worst case, insertion sort has a quadratic running time
(i.e., O(n2)).
• Even in the average case, the insertion sort algorithm will have to make at
least (K–1)/2 comparisons. Thus, the average case also has a quadratic
running time.
Program:-
#include <stdio.h>
#define size 5
void insertion_sort(int arr[], int n);
void main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
insertion_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
}
void insertion_sort(int arr[], int n)
{
int i, j, temp;
for(i=1;i<n;i++)
{
temp = arr[i];
j = i-1;
while((temp < arr[j]) && (j>=0))
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
Output:- Enter the number of elements in the array : 5
Enter the elements of the array : 500 1 50 23 76
The sorted array is :1 23 20 76 500 6 7 8 9 10
SELECTION SORT
• Selection sort is a simple sorting algorithm. This sorting algorithm is
an in-place comparison-based algorithm in which the list is divided
into two parts, the sorted part at the left end and the unsorted part
at the right end. Initially, the sorted part is empty and the unsorted
part is the entire list.
• The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element becomes a
part of the sorted array. This process continues moving unsorted
array boundary by one element to the right.
Consider the following depicted array as an example
For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in
the list, appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in a
linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second place.
We swap these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array. Following is a pictorial
depiction of the entire sorting process −
Complexity of Selection Sort
• Selection sort is a sorting algorithm that is independent of the original
order of elements in the array.
• In Pass 1, selecting the element with the smallest value calls for
scanning all n elements; thus, n–1 comparisons are required in the
first pass. Then, the smallest value is swapped with the element in the
first position.
• In Pass 2, selecting the second smallest value requires scanning the
remaining n – 1 elements and so on. Therefore,
(n – 1) + (n – 2) + ... + 2 + 1
= n(n – 1) / 2 = O(n2) comparisons
Program:-
#include <stdio.h>
#include <conio.h>
int smallest(int arr[], int k, int n);
void selection_sort(int arr[], int n);
void main() {
int arr[10], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
}
int smallest(int arr[], int k, int n)
{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{
small = arr[i];
pos = i;
} }
return pos;
}
void selection_sort(int arr[],int n)
{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}
Merge Sort
• Merge sort is a sorting algorithm that uses the divide, conquer, and
combine algorithmic paradigm.
• Divide means partitioning the n-element array to be sorted into two
sub-arrays of n/2 elements. If A is an array containing zero or one
element, then it is already sorted.
• However, if there are more elements in the array, divide A into two
sub-arrays, A1 and A2 , each containing about half of the elements of
A.Conquer means sorting the two sub-arrays recursively using merge
sort.
• Combine means merging the two sorted sub-arrays of size n/2 to
produce the sorted array of n elements.
Merge sort algorithm focuses on two main concepts to improve its
performance (running time):
• A smaller list takes fewer steps and thus less time to sort than a large
list.
• As number of steps is relatively less, thus less time is needed to
create a sorted list from two sorted lists rather than creating it using
two unsorted lists.
• The basic steps of a merge sort algorithm are as follows:
1. If the array is of length 0 or 1, then it is already sorted.
2. Otherwise, divide the unsorted array into two sub-arrays of about
half the size.
3. Use merge sort algorithm recursively to sort each sub-array.
4. Merge the two sub-arrays to form a single sorted list.
Example
• MERGE (ARR, BEG, MID, END)
• Step 1: [INITIALIZE] SET I = BEG, J = MID + 1, INDEX =0
• Step 2: Repeat while (I <= MID) AND (J<=END)
• IF ARR[I] < ARR[J]
• SET TEMP[INDEX] = ARR[I]
• SET I = I + 1
• ELSE
• SET TEMP[INDEX] = ARR[J]
• SET J = J + 1
• [END OF IF]
• SET INDEX = INDEX + 1
• [END OF LOOP]
• Step 3: [Copy the remaining elements of right sub-array, if any]
• IF I > MID
• Repeat while J <= END
• SET TEMP[INDEX] = ARR[J]
• SET INDEX = INDEX + 1, SET J = J + 1
• [END OF LOOP]
• [Copy the remaining elements of left sub-array, if any]
• ELSE
• Repeat while I <= MID
• SET TEMP[INDEX] = ARR[I]
• SET INDEX = INDEX + 1, SET I = I + 1
• [END OF LOOP]
• [END OF IF]
• Step 4: [Copy the contents of TEMP back to ARR] SET K=0
• Step 5: Repeat while K < INDEX
• SET ARR[K] = TEMP[K]
• SET K = K + 1
• [END OF LOOP]
• Step 6: END
• MERGE_SORT(ARR, BEG, END)
• Step 1: IF BEG < END
• SET MID = (BEG + END)/2
• CALL MERGE_SORT (ARR, BEG, MID)
• CALL MERGE_SORT (ARR, MID + 1, END)
• MERGE (ARR, BEG, MID, END)
• [END OF IF]
• Step 2: END
Program:-
#include <stdio.h>
#define max 10
int a[11] = { 10, 41, 19, 26, 37,9, 33, 35, 63, 44, 0 };
int b[10];
void merging(int low, int mid, int high)
{ int l1, l2, i;
for(l1 = low,l2 = mid + 1,i = low; l1 <= mid && l2 <= high; i++)
{ if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high)
{ int mid;
if(low < high)
{ mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
}
Else { return; }
}
int main()
{ int i;
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
List before sorting
10 41 19 26 37 9 33 33 63 44 0
List after sorting
0 9 10 19 26 33 35 37 41 44 63
Abstract Data Type (ADT)
• Abstract Data type (ADT) is a type (or class) for objects whose behaviour is
defined by a set of value and a set of operations.
• The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented. It does not specify how data will be
organized in memory and what algorithms will be used for implementing the
operations. It is called “abstract” because it gives an implementation-
independent view. The process of providing only the essentials and hiding the
details is known as abstraction.
Array as an ADT
• ADT indicates for Abstract Data Type. Arrays are defined as ADT’s
because they are capable of holding contiguous elements in the same
order. And they permit access for the specific element via index or
position.
• The elements stored in an array can be anything from primitives types
such as integers to more complex types like instances of classes.
int[] arrA = new int[1];
String[] arrB = new String[1];
Person[] arrC = new Person[3];
• An element is stored in a given index and they can be retrieved at a
later time by specifying the same index.
• The Array (ADT) have one property, they store and retrieve elements
using an index.
Deleting an Element from an Array
• Deleting an element from an array means removing a data element
from an already existing array. If the element has to be deleted from
the end of the existing array, then the task of deletion is quite simple.
We just have to subtract 1 from the upper_bound.
• Algorithm to delete an element from the middle of an array
The algorithm DELETE will be declared as DELETE(A, N,POS). The
arguments are:
(a) A, the array from which the element has to be deleted
(b) N, the number of elements in the array
(c) POS, the position from which the element has to be deleted
• Example
Program:-
#include <stdio.h>
void main()
{
int i, n, pos, arr[10];
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\nEnter the position from which the number has to be deleted : ");
scanf("%d", &pos);
for(i=pos; i<n–1;i++)
arr[i] = arr[i+1];
n– –;
printf("\n The array after deletion is : ");
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, arr[i]);
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the position from which the number has to be deleted : 3
The array after deletion is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 5
PASSING ARRAYS TO FUNCTIONS
• Like variables of other data types, we can also pass an array to a
function. In some situations, you may want to pass individual
elements of the array; while in other situations, you may want to pass
the entire array.
Passing Individual Elements
The individual elements of an array can be passed to a function by passing
either their data values or addresses.
• Passing Data Values
Individual elements can be passed in the same manner as we pass
variables of any other data type. The condition is just that the data type of
the array element must match with the type of the function parameter.
Calling function Called function
main() void func(int num)
{ {
int arr[5] ={1, 2, 3, 4, 5}; printf("%d , num);
func(arr[3]); }
}
• Passing Addresses
Like ordinary variables, we can pass the address of an individual
array element by preceding the indexed array element with the address
operator.
However, in the called function, the value of the array element must be
accessed using the indirection (*) operator.
Calling function Called function
main() void func(int *num)
{ {
int arr[5] ={1, 2, 3, 4, 5}; printf("%d , *num);
func(&arr[3]); }
}
Passing the Entire Array
• We have discussed that in C the array name refers to the first byte of the
array in the memory. The address of the remaining elements in the array
can be calculated using the array name and the index value of the element.
Therefore, when we need to pass an entire array to a function, we can
simply pass the name of the array.
Calling function Called function
main() void func(int arr[5])
{ {
int arr[5] ={1, 2, 3, 4, 5}; int i;
func(arr); for(i= ;i<5;i++)
} printf("%d", arr[i]);
}
Program:-
#include <stdio.h>
#include<conio.h>
void read_array(int arr[], int n);
int find_small(int arr[], int n);
void main()
{
int num[10], n, smallest;
clrscr();
printf("\n Enter the size of the array : ");
scanf("%d", &n);
read_array(num, n);
smallest = find_small(num, n);
printf("\n The smallest number in the array is = %d", smallest);
getch();
}
void read_array(int arr[10], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", &arr[i]);
} }
int find_small(int arr[10], int n)
{
int i = 0, small = arr[0];
for(i=1;i<n;i++)
{
if(arr[i] < small)
small = arr[i];
}
return small;
}
Output
Enter the size of the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The smallest number in the array is = 1
Pointers And Arrays
The concept of array is very much bound to the concept of pointer.
Example:- int arr[] = {1, 2, 3, 4, 5};
Base address is the address of the first element in the array or the address of
arr[0].
Now let us use a pointer variable as givenin the statement below.
int *ptr;
ptr = &arr[0];
Here, ptr is made to point to the first element of the array
main()
{
int arr[]={1,2,3,4,5};
printf("\n Address of array = %p %p %p", arr, &arr[0], &arr);
}
If pointer variable ptr holds the address of the first element in the
array, then the address of successive elements can be calculated by
writing ptr++.
Output:- 3
int main()
{
int arr[]={1,2,3,4,5};
int *ptr, i;
ptr=&arr[2];
*ptr = –1;
*(ptr+1) = 0;
*(ptr–1) = 1;
printf("\n Array is: ");
for(i=0;i<5;i++)
printf(" %d",arr[i]);
return 0;
}
Output
Array is: 1 1 –1 0 5
int main()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int *ptr1, *ptr2;
ptr1 = arr;
ptr2 = arr+2;
printf("%d", ptr2–ptr1);
return 0;
}
Output:- 2
Arrays Of Pointers
An array of pointers can be declared as - int *ptr[10];
The above statement declares an array of 10 pointers where each of the
pointer points to an integer variable.
Example:-
int *ptr[10];
int p = 1, q = 2, r = 3, s = 4, t = 5;
ptr[0] = &p;
ptr[1] = &q;
ptr[2] = &r;
ptr[3] = &s;
ptr[4] = &t;
Can you tell what will be the output of the following statement?
printf("\n %d", *ptr[3]);
int main()
{
int arr1[]={1,2,3,4,5};
int arr2[]={0,2,4,6,8};
int arr3[]={1,3,5,7,9};
int *parr[3] = {arr1, arr2, arr3};
int i;
for(i = 0;i<3;i++)
printf(“%d”, *parr[i]);
return 0;
}
Output :- 1 0 1
TWO-DIMENSIONAL ARRAYS
• One-dimensional arrays are organized linearly in only one direction. But at times,
we need to store data in the form of grids or tables. Here, the concept of single-
dimension arrays is extended to incorporate two-dimensional data structures.
• A two-dimensional array is specified using two subscripts where the first
subscript denotes the row and the second denotes the column.
data_type array_name[row_size][column_size];
Ex:- store the marks obtained by three students in five different subjects
int marks[3][5];
Write a program to print the elements of a 2D array.
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[2][2] = {12, 34, 56,32};
int i, j;
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t", arr[i][j]);
}
return 0;
}
Output
12 34
56 32
void main()
{
int marks[5][3], i, j, max_marks;
for(i=0; i<5; i++)
{
printf("\n Enter the marks obtained by student %d",i+1);
for(j=0; j<3; j++)
{
printf("\n marks[%d][%d] = ", i, j);
scanf("%d", &marks[i][j]);
}
}
for(j=0; j<3; j++)
{
max_marks = –999;
for(i=0; i<5; i++)
{
if(marks[i][j]>max_marks)
max_marks = marks[i][j];
}
printf("\n The highest marks obtained in the subject %d = %d", j+1, max_marks);
}
}
Output:- Enter the marks obtained by student 1
marks[0][0] = 89
marks[0][1] = 76
marks[0][2] = 100
Enter the marks obtained by student 2
marks[1][0] = 99
marks[1][1] = 90
marks[1][2] = 89
Enter the marks obtained by student 3
marks[2][0] = 67
marks[2][1] = 76
marks[2][2] = 56
Enter the marks obtained by student 4
marks[3][0] = 88
marks[3][1] = 77
marks[3][2] = 66
Enter the marks obtained by student 5
marks[4][0] = 67
marks[4][1] = 78
marks[4][2] = 89
The highest marks obtained in the subject 1 = 99
The highest marks obtained in the subject 2 = 90
The highest marks obtained in the subject 3 = 100
OPERATIONS ON TWO-DIMENSIONAL ARRAYS
Two-dimensional arrays can be used to implement the mathematical concept of matrices. In
mathematics, a matrix is a grid of numbers, arranged in rows and columns. Thus, using two
dimensional arrays, we can perform the following operations on an m×n matrix:
• Transpose:- Transpose of an m×n matrix A is given as a n × m matrix B, where Bi,j = Aj,i.
• Sum:- Two matrices that are compatible with each other can be added together, storing the result
in the third matrix. Two matrices are said to be compatible when they have the same number of
rows and columns. The elements of two matrices can be added by writing:
Ci,j = Ai,j + Bi,j
• Difference:- Two matrices that are compatible with each other can be subtracted, storing the
result in the third matrix. Two matrices are said to be compatible when they have the same
number of rows and columns. The elements of two matrices can be subtracted by writing:
Ci,j = Ai,j – Bi,j
• Product:- Two matrices can be multiplied with each other if the number of columns in the first
matrix is equal to the number of rows in the second matrix. Therefore, m × n matrix A can be
multiplied with a p × q matrix B if n=p. The dimension of the product matrix is m × q. The
elements of two matrices can be multiplied by writing:
Ci,j = ∑ Ai,kBk,j for k = 1 to n
void main()
{
int i, j, mat[3][3];
printf("\n Enter the elements of the matrix ");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ scanf("%d",&mat[i][j]);
}
}
printf("\n The elements of the matrix are ");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
printf("\t %d",mat[i][j]);
}
}
Output:- Enter the elements of the matrix 1 2 3 4 5 6 7 8 9
The elements of the matrix are 1 2 3
4 5 6
7 8 9
Program:- Write a program to transpose a 3×3 matrix.
void main()
{
int i, j, mat[3][3], transposed_mat[3][3];
printf("\n Enter the elements of the matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &mat[i][j]);
} }
printf("\n The elements of the matrix are ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t %d", mat[i][j]);
}
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
transposed_mat[i][j] = mat[j][i];
}
printf("\n The elements of the transposed matrix are ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t %d",transposed_ mat[i][j]);
}
}
Output :- Enter the elements of the matrix 1 2 3 4 5 6 7 8 9
The elements of the matrix are 1 2 3
4 56
7 8 9
The elements of the transposed matrix are 1 4 7
2 5 8
3 6 9
• Write a program to input two m × n matrices and then calculate the
sum of their corresponding elements and store it in a third m×n
matrix.
Passing two-dimensional arrays to functions
• There are three ways of passing a two-dimensional array to a
function. First, we can pass individual elements of the array. This is
exactly the same as passing an element of a one-dimensional array.
• Second, we can pass a single row of the two-dimensional array. This is
equivalent to passing the entire one-dimensional array to a function
that has already been discussed in a previous section.
• Third, we can pass the entire two-dimensional array to the function.
Passing a Row
A row of a two-dimensional array can be passed by indexing the
array name with the row number. Look at Fig. 3.32 which illustrates
how a single row of a two-dimensional array can be passed to the
called function.
Calling function Called function
main() void func(int arr[])
{ {
int arr[2][3] = ({1, 2, 3}, {4, 5, 6}); int i;
func(arr[1]); for(i= ;i<3;i++)
} printf("%d", arr[i] * 1 );
}
Passing the Entire 2D Array
To pass a two-dimensional array to a function, we use the array name as the
actual parameter. However, the parameter in the called function must indicate that
the array has two dimensions.
# include <stdio.h>
void read_matrix(int mat[5][5], int);
void display_matrix(int mat[5][5], int);
int main()
{
int row;
int mat1[5][5];
printf("\n Enter the number of rows and columns of the matrix:");
scanf("%d", &row);
read_matrix(mat1, row);
display_matrix(mat1, row);
}
void read_matrix(int mat[5][5], int r)
{
int i, j;
for(i=0; i<r; i++)
{
for(j=0; j<r; j++)
{
if(i==j)
mat[i][j] = 0;
else if(i>j)
mat[i][j] = –1;
else
mat[i][j] = 1;
}
}
}
void display_matrix(int mat[5][5], int r)
{
int i, j;
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<r; j++)
printf("\t %d", mat[i][j]);
}
}
Output
Enter the number of rows and columns of the matrix: 2
0 1
–1 0
Program:-
#include<stdio.h>
void main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
} } }
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
• Output:- Enter the number of rows in the first matrix: 2
• Enter the number of columns in the first matrix: 2
• Enter the number of rows in the second matrix: 2
• Enter the number of columns in the second matrix: 2
• Enter the elements of the first matrix
• 1234
• Enter the elements of the second matrix
• 5678
• The elements of the product matrix are
• 19 22
• 43 50
Multi-Dimensional ARRAYS
• A multi-dimensional array in simple terms is an array of arrays. In this
we have n indices in an n-dimensional array or multi-dimensional
array. Conversely, an n–dimensional array is specified using n indices.
• In a multi-dimensional array, a particular element is specified by using
n subscripts as A[I1][I2][I3]...[In],
where I1 <= M1, I2 <= M2, I3 <= M3, ... In <= Mn
Consider a three-dimensional array defined as int A[2][2][3]. Calculate
the number of elements in the array. Also, show the memory
representation of the array in the row major order and the column
major order.
Solution:- A three-dimensional array consists of pages. Each page, in turn, contains
m rows and n columns.
(0,0,0) (0,0,1) (0,0,2) (0,1,0) (0,1,1) (0,1,2) (1,0,0) (1,0,1) (1,0,2) (1,1,0) (1,1,1) (1,1,2)
(a) Row major order
(0,0,0) (0,1,0) (0,0,1) (0,1,1) (0,0,2) (0,1,2) (1,0,0) (1,1,0) (1,0,1) (1,1,1) (1,0,2) (1,1,2)
(b) Column major order
The three-dimensional array will contain 2 × 2 × 3 = 12 elements.
Program:-
#include <stdio.h>
#include <conio.h>
int main()
{
int array[2][2][2], i, j, k;
printf("\n Enter the elements of the matrix");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
scanf("%d", &array[i][j][k]);
}
}
}
printf("\n The matrix is : ");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\n");
for(k=0;k<2;k++)
printf("\t array[%d][%d][%d] = %d", i, j, k, array[i][j][k]);
}
}
}
Output :- Enter the elements of the matrix 1 2 3 4 5 6 7 8
The matrix is
arr[0][0][0] = 1 arr[0][0][1] = 2
arr[0][1][0] = 3 arr[0][1][1] = 4
arr[1][0][0] = 5 arr[1][0][1] = 6
arr[1][1][0] = 7 arr[1][1][1] = 8
Applications of Arrays
Arrays are frequently used in C, as they have a number of useful
applications. These applications are
• Arrays are widely used to implement mathematical vectors, matrices,
and other kinds of rectangular tables.
• Many databases include one-dimensional arrays whose elements are
records.
• Arrays are also used to implement other data structures such as
strings, stacks, queues, heaps, and hash tables. We will read about
these data structures in the subsequent chapters.
• Arrays can be used for sorting elements in ascending or descending
order.