0% found this document useful (0 votes)
34 views27 pages

DSC 2nd Unit Notes

The document provides an overview of arrays in C programming, including their definition, properties, types (one-dimensional, two-dimensional, and multi-dimensional), and basic operations such as insertion, deletion, and traversal. It also discusses the advantages and disadvantages of arrays, as well as sorting algorithms like bubble sort and selection sort. Additionally, it introduces the concept of Abstract Data Types (ADT) and how arrays fit into this framework.

Uploaded by

taciwil787
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)
34 views27 pages

DSC 2nd Unit Notes

The document provides an overview of arrays in C programming, including their definition, properties, types (one-dimensional, two-dimensional, and multi-dimensional), and basic operations such as insertion, deletion, and traversal. It also discusses the advantages and disadvantages of arrays, as well as sorting algorithms like bubble sort and selection sort. Additionally, it introduces the concept of Abstract Data Types (ADT) and how arrays fit into this framework.

Uploaded by

taciwil787
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/ 27

Data Structure Using C Shree Medha Degree College, Ballari

Unit – 02

Arrays
Definition of Array:
❖ The collection of homogeneous elements which has same datatype and same name.
❖ An array is a collection of items stored at contiguous memory locations. The idea is to store multiple
items of the same type together.
Properties of an Array
1. All elements of an array must be of same datatype.
2. Each element of an array is referred to be specifying the array name followed by one or more
subscript.
3. Each subscript must be expressed as a positive integer.
4. The subscript will begin with the number 0.
5. Array elements are always stored in sequential memory locations.

Advantages and Disadvantages of Array


Advantages Disadvantages
1. In an array accessing an element is very 1. There is chance of memory wastage or
easy by using the index number. storage.
2. The search process can be applied to an 2. Pre-determining the size of array is must.
array easily. 3. Insertion and deletion become tedious.
3. 2D Array is used to represent matrices.
4. Capable of storing many elements at a time.

Types of Arrays
Arrays can be classified as follows
1. One–Dimensional Array / 1-D Array
2. Two–Dimensional Array / 2-D Array
3. Multidimensional Array
1. One-Dimensional Array / 1-D Array:
❖ It is a collection of homogeneous elements and same data type and same name, and with only
one subscript is called as one-dimensional array.
Syntax or the general form or the declaration of one-dimensional array is
To declare an array,
1. Here datatype declares the type of elements to be stored in the array such as int, float, char,….
2. Array name is the name of the array
3. Size specifies maximum number of elements that can be stored in the array.
datatype arrayname[size];

Dept. of Computer Science 15 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
where
1. Datatype can be int, float, double, char etc.
2. array_ name should be a valid variable name.
3. size is an integer value.
4. Size of an array must not be in negative value.
End of the Statement

Example: int list[5]; Declaration of an Array


list[0] list[1] list[2] list[3] list[4]
Array Array
Datatype
Name Size
Initialization of Array
int arr[5]={10,20,30,40,50};
Pictorial Explanation

2. Two-Dimensional Array
❖ The two-dimensional array can be defined as an array of arrays.
❖ The 2D array is organized as matrices which can be represented as the collection of rows and
columns.
❖ However, 2D arrays are created to implement a relational database looks like data structure.
❖ It provides ease of holding the bulk of data at once which can be passed to any number of functions
wherever required.
Declaring of Two-Dimensional Array
The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
Consider the following example
Initialization of 2-D Array
There are two ways to initialize a two-dimensional array during declaration.

int disp[2][4]={

Dept. of Computer Science 16 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
{10,11,12,13}
{14,15,16,17}
Rows,
}; a[0] a[1] a[2] a[3]
Columns
(Or) a[0] 10 11 12 13
int disp[2][4]={10,11,12,13,14,15,16,17}; a[1] 14 15 16 17
3. Multi-Dimensional Array
Collection of homogeneous elements which has same datatype and same name with more than two
subscripts is called as Multi-Dimensional Array.

Syntax: datatype arrayname[size][size][size];

Operations of arrays
There are six types of operations of arrays:
1. Traversal: processing each element in the array or to visit the element stored in it. Traversing an
array means going through each element of an array exactly once is called as traversal.
2. Searching: Finding the location of the element with a given value in the array or the search
operation is used to find a particular data item or element in an array is called searching.
3. Insertion: Adding a new element into an array. Based on the requirement, a new element can be
added at the beginning, end or any given index of array is called insertion.
4. Deletion: Removing an element from an array is called deletion.
5. Sorting: Arranging the element in some type of order or the processing of arranging the data in
ascending or descending order. There are several types of sorting in data structures namely- bubble
sort, insertion sort, selection sort, merge sort, quick sort etc.
6. Merging: Combining of two arrays into single array or when two or more arrays are merged to form
a bigger array containing all the items of the merged arrays is called merging.

Dept. of Computer Science 17 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Abstract Data Type (ADT):
An abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed
on the data. And this is organized in such a way that the specification of the values and operations on those
values are separated from the representation of the values and the implementation of the operations.in simple
terms, an abstract data type is a data type with associated operations, but whose representation is hidden.
Arrays as ADT:
1. An array is the fixed size sequence of element of an same type.
2. An array is the fundamental abstract datatype.
3. The basic operating includes direct access to each element in the array by specifying its position, so
that values can be retrieved or stored in that position.
4. The most important operation of ADT is setting the value in a particular index and getting a value
from a particular index.
Inserting an element in to Array:
Adding a new element in to an array is called as Insertion of array.
Algorithm:
1. Set I=N
2. Repeat while (I>=LOC)
3. Set (A(I+1)=A(I)
4. Set I=I-1
5. Set A[LOC]=ITEM
6. Set N=N+1
7. EXIT
//Program to inserting an element into array.
#include<stdio.h>
#include<conio.h>
void main ()
{
int I,n,num,pos,arr[10];
clrscr();
printf(“\n enter the number of elements in the array”);
scanf(“%d”,&n);
printf(“enter the values:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}

Dept. of Computer Science 18 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
printf(“\n enter the number to be inserted”);
scanf(“%d”,&num);
printf(“enter the position”);
scanf(“%d”,&position);
for(i=n;i>=pos;i--)
arr[i+1]=arr[i];
arr[pos]=num;
n++;
printf(“\n the array after inserting %d is :”,num);
for(i=0;i<n;i++)
enter Printf(“\t %d”,arr[i]);
getch();
}
Output: -
Number of elements in to array:5
Enter the values:10 20 30 40 50
Enter the number to be inserted:70
The array after the insertion of 70 is
10 20 30 70 40 50
Delete an element from array:
The process of removing or deleting an element from an array is called as Deletion of Array.
Algorithm:
1. Set ITEM=A[LOC]
2. Repeat for I=LOC to N
3. Set A[1]=A[I+1]
4. Set N=N-1
5. EXIT
//Program to delete an element from array.
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,n,pos,arr[10];

Dept. of Computer Science 19 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
printf(“\n enter the size of an array”);
scanf(“%d”,&n);
printf(“\n enter the elements of array”);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
printf(“\n enter the position which the number has to be deleted “);
scanf(“%d”,&pos);
for(i=pos;i<n;i++)
arr[i]=arr[i+1];
n--;
printf(“\n the array after deletion”);
for(i=0;i<n;i++)
printf(“\t %d”,arr[i]);
getch();
}
Output: -
Enter the size of the array:5
Enter the elements in the array:
10 20 30 40 50
Enter the position which the number has to be deleted 40
10 20 30 50
Traversing in Linear arrays:
1. Processing each element in the arrays atleast one.
2. Revisiting each element in a linear array only once is called Traversing.
3. Accessing each element of the array only once so that it can be processed is called as Traversing
Algorithm:
1. Start
2. Initialize counter
Set k=LB
3. Repeat steps 4th and 5th
4. Visit element Apply process to LA[K]
5. Increment counter
K=k+1
6. Stop

Dept. of Computer Science 20 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
//Program to perform traversing in linear arrays.
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,n,arr[20],sum=0
clrscr();
printf(“enter the number of elements of array”);
scanf(“%d”,&n);
for (i=0;i<n;i++)
{
printf(“\n arr[%d]”=,i);
scanf(“%d”,&arr[i]);
}
for(i=0;i<n;i++)
sum+=arr[i];
printf(“\n the sum of the array sum elements=%d”,sum);
getch();
}
Output: Enter the number of arrays: 3
arr[0]=10
arr[1]=20
arr[2]=30
The sum of array elements =60

Sorting
❖ Arranging the elements into ascending or descending order is called sorting.
❖ Arranging the unordered elements in an array in ordered way is called sorting.
Types of Sorting
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Quick Sort
5. Merge Sort

Dept. of Computer Science 21 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Bubble Sort: Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the element are swapped if they are not
in order.
Algorithm of Bubble Sort:
Step 1: for pass=1 to n-1
Step 2: for j=0 to n-pass-1
Step 3: if a[j]>a[j+1] then
Temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
Step 4: end for
Step 5: end for
//Program to perform Bubble Sort technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int , i,n,temp,j,arr[10];
clrscr();
printf(“enter the number of elements;”);
scanf(“%d”,&n);
printf(“\n enter the elements in array:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];

Dept. of Computer Science 22 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
arr[j+1]=temp;
}
}
}
printf(“\n the array sorted in ascending order is:\n”);//traversing part
for(i=0;i<n;i++)
printf(‘\t%d”,arr[i]);
getch();
}
}
Output: Enter the number of elements: 6
Enter the elements in array:
99 33 6 77 3 0
The array sorted in ascending order:
0 3 6 33 77 99
Selection Sort
❖ It is a selection of an element and placing it in an proper position.
❖ In selection sort first will search the smallest element in array and interchange with the first element.
Then search the second smallest element and interchange with the second element and continuous
the process until all the elements are completed.
❖ The method of selection sort relays heavily on a comparison mechanism to achieve its goal.
Algorithm:
1. set location=0
2. Repeat step 3 and 4 for k=0 to n-1
3. Loc=call MIN (a,k,n)
4. (Interchange A[k] and A[loc])
i. Temp=A[k]
ii. A[k]=A[loc]
iii. A[loc]=temp
5. EXIT
Algorithm min(a[0------a-1];m,n)
1. Set min=A[k],loc=k
2. Repeat step3 for j=k+1 to n-1
3. if min>A[j]
a. min =A[j]
b. loc =j
4. Return Loc;

Dept. of Computer Science 23 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
//Program of Selection Sort:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],i,j,n,pos,temp;
clrscr();
printf(“enter the size of the array;”);
scanf(“%d”,&n);
printf(“\n enter the elements in array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i=1;j<n;j++)
{
if(arr[pos].arr[j])
pos=j;
}
if(pos!=j)
{
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}
}
printf(“\n the sorted array after swapping is:\n”);//traversing part
for(i=0;i<n;i++)

Dept. of Computer Science 24 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
printf(‘\t%d”,arr[i]);
getch();
}
}
Output: Enter the size of the array: 8
Enter the elements in the array:
66 4 88 1 7 0 5 8
The sorted array after swapping is:
0 1 4 5 7 8 66 88
Insertion Sort
1. The insertion sort inserts each element in appropriate position.
2. This is same as playing card in which we insert a card in proper position.
3. The insertion sort is effectively only when the number to be sorted all very less.
Algorithm:
1. Repeat step2 to 3 for pass=1 to n-1
2. set k=A[pass]
3. Repeat step 4 for j=pass-1 to 0
4. if (k<A[j])
a. A [j+1] = A[j]
b. A [j+1] = k
5. EXIT
//Program of Insertion Sort:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr [10],i,j,n,temp;
clrscr();
printf(“enter the number of elements;”);
scanf(“%d”,&n);
printf(“\n enter the elements in array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);

Dept. of Computer Science 25 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
}
for(i=1;i<=n-1;i++)
{
j=i;
while (j<0; && arr[j-1]>arr[j])
{
temp=arr[j];
arr[j] = arr [j-1];
arr [j-1] = temp;
j--;
}
}
for(i=0;i<n;i++)
{
printf(‘sorted list is = %d\n”, arr[i]);
}
getch();
}
Output: Enter the size of the array: 6
Enter the elements in array:
381604
Sorted list is= 0
Sorted list is= 1
Sorted list is= 3
Sorted list is= 4
Sorted list is= 6
Sorted list is= 8
Quick Sort
1. It is one of the most popular sorting mechanism used to arrange the element in an ascending order.
2. In this mechanism it uses divide and conquer technique.
3. Here the 1st element of the array is selected as a key element or pivot (IMP) element.
4. Next remaining elements are grouped into two partition . Such as left and right partition.
5. Left partition contains elements smaller than the pivot element.

Dept. of Computer Science 26 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
6. Right partition contains element larger than the pivot element.
(Value>key) (Value<key)
Repeat the same process in left and right partition, until the end of array.
Another important point: When left and right element crosses each other then right element will swap or
interchange with the pivot element.
Algorithm For Sorting:
Step 1: if (low<high) then
Step 2: j=partition(A,low,high)
Step 3: Quick_ sort(A,low,j-1)
Step 4: Quick_ sort(A,j+1,high)
Step 5: End if
Step 6: EXIT
Algorithm of Quick Sort for Partition:
Step 1: set pivot=low
I=low
J=high
Step 2: Repeat step 3 to step 6 while(i<j)
Step 3: Repeat step 4 while (( i< high ) && (A[i]<=[pivot]))
Step 4: i=i+1
Step 5: Repeat step 6 while (A[j]>A[pivot])
Step 6: j=j-1
Step 7: if(i<j)
Swap a[i] & a[j]
End of step 2 while
Step 8: swap a[j] & pivot
Step 9: Return(j)
//Program Of Quick Sort:
#include<stdio.h>
#include<conio.h>
void quicksort(int a[25],int f int l)
{
int i,j,pivot,jump;

Dept. of Computer Science 27 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
if(f<l)
{
pivot=f;
i=f;
j=l;
while(i<j)
}
while(a[i]<=a[pivot] && i<l))
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,f,j-1);
quicksort(a,j+1,f);
}
}
void main()
{
int i,n,a[25],l;
clrscr():
printf(”enter the size of an array\n”);
scanf(“%d”,&n);

Dept. of Computer Science 28 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
printf(‘enter %d array elements\n”,n);
for(i=0;i<n;i++)
{
scanf(“%d’,&a[i]);
}
quicksort(a,0,n-1);
printf(“sorted elements are\n”);
for(i=0;i<n;i++)
{
printf(“%3d”,a[i]);
}
getch();
}
Output: Enter the size of an array: 7
Enter 7 array elements;
44 8 99 2 0 66 21
Sorted elements are:
0 2 8 21 44 66 99
Merge Sort
❖ It is one of the sorting techniques used to arrange the data elements in an ascending order.
❖ Merge sort is used to combine the two sorted list into one sorted list.
❖ In this sorting technique first divide the list into two parts as left and right part. Apply the same
process to left and right part up to individual elements. Then sort the elements in left and right part.
Finally combine left and right parts which we get a sorted list.
Algorithm:
Algorithm [merge (A, low, mid, high )] given in array A of (high-low+1) elements .The variables low ,mid
and high are used to identify the position of elements in each position .
Step 1: set i=low,j=mid+1,k=low

Step 2: while ((i<=mid )and (j<=high)) do step 3


Step 3: if (a[i]<a[j])
c[k]=a[i]
k=k+1

Dept. of Computer Science 29 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
i=i+1
else
c[k]=a[j]
k=k+1
j=j+1
end if
end while (step 2)
Step 4: while (i<=mid)
c[k]=a[i]
k=k+1
i=i+1
end while (Step 4)
Step 5: while (j<=high)
c[k]=a[j]
k=k+1
j=j+1
end while (step 5)
Step 6: for i=low to k-1
a[i]=c[i]
end for (Step 6)
Step 7: return
//Program Of Merge Sort:
#include<stdio.h>
#include<conio.h>
void simple-merge(int a[],int low ,int mid ,int high)
{
int i=low,j=mid+1,k=low ,c[10];
while(i<=mid && j<=high)
{
if(a[i]<a[j])
{

Dept. of Computer Science 30 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
c[k++]=a[i++];
}
else
{
c[k++]=a[j++];
}
}
while (i<=mid)
{
c[k++]=a[i++];
}
while(j<=high)
{
c[k++]=a[j++];
}
for(i=low;i<=high;i++0
{
a[i]=c[i]
}
}
void mergesort (int a[] ,int low ,int high)
{
int mid ;
if (low<high)
{
mid =(low+high)/2;
merge_sort(a,low,mid0;
merge_sort(a,mid+1,high);
simple_merge (a,low,mid ,high);
}
}

Dept. of Computer Science 31 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
void main()
{
int a[10],n,i;
clrscr();
printf(”enter the size of an array\n”);
scanf(“%d”,&n);
printf(‘enter %d array elements\n”,n);
for(i=0;i<n;i++)
{
scanf(“%d’,&a[i]);
}
merge-sort(a,0,n-1);
printf(“\n the sorted elements are.. :\n”);//traversing part
for(i=0;i<n;i++)
printf(‘\t%d”,arr[i]);
getch();
}
Output: Enter the size of the array: 5
Enter the array elements:
99 70 3 88 1
The sorted elements are
1 3 70 88 99
Searching
❖ Searching refers to finding for an element in any list.
❖ Searching in data structure refers to the process of finding location LOC of an element in a list. This
is one of the important parts of many data Structure algorithms, as one operation can be performed
on a element if and only if we find it.
There are 2 types of searching they are:
1) Linear Search
2) Binary search

Dept. of Computer Science 32 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Linear Search
Linear search is a very simple algorithm. In this type of search, a sequential search is made over all items
one by one. Every item is checked and if a match is found the that particular item is returned, otherwise the
search continues till the end of the data collection.
Algorithm
Linear search (A [] , n , key)
Let A [] be a linear array with n elements and key is an element to be searched in A [].
Step 1: Set i=0
Step 2: while (i<n) do
If A[i] = = key then
Return i; // key found at I th location
End while
Step 3: Return-1; //key not found
//Program of Linear Search
#include<stdio.h>
#include<conio.h>
void main()
{
int arr [50],i,n,key;
clrscr();
printf(“enter the required array size;”);
scanf(“%d”,&n);
printf(“\n enter the elements in array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“the entered array elements are\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}

Dept. of Computer Science 33 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
printf(“enter the key element you want to search\n”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
{
if(a[i]= = key)
{
printf(‘search is successful\n”);
printf(“key element %d found at %d position”, key ,i);
break;
}
}
if(i= = n)
{
printf(“search is unsuccessfull\n”);
printf(“key element %d not found key”);
}
getch();
}
Output: Enter the required array size:8
Enter the elements in array:
10 20 30 40 50 60 70 80
The entered elements are
10 20 30 40 50 60 70 80
Enter the key element you want to search:
60
Search is successful
Key element 60 found at 5 position

Dept. of Computer Science 34 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Binary Search
❖ Binary search is the search technique that works efficiently on sorted lists. Hence, to search an
element into some list using the binary search technique, we must ensure that the list is sorted.
❖ Binary search follows the divide and conquer approach in which the list is divided into two halves,
and the element is compared with the middle element of the list.
❖ Binary search is one of the best suits to solve the above complexity problem.

0 1 2 3 4 5 6 7 8
Low left Mid Right high
If (key>mid) --- R Mid = Low + High/2
If (key<mid) --- L

Algorithm-Binary Search
(A[0---n-1],key)
Input: Given an array A of n elements is sorted under and key is an element to be sorted.
Output: Return the position of item element if successful and returns-1 otherwise.
Step 1: set first=0, last =n-1;
Step 2: repeat step 3 until (first,=last)
Step 3: find the middle location 9mid)=(f+l)/2
if key is equal to a[mid]
else if (key<a[mid]) then search element from first to mid-1
last=mid-1
else search element from mid+1 to last first= mid+1
end while
Step 4: return-1
Program Of Binary Search:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr [50],i,n,low,mid,high,key;
clrscr();
printf(“enter the number of elements;”);

Dept. of Computer Science 35 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
scanf(“%d”,&n);
printf(“\n enter the elements in array:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“the entered array elements are\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
printf(“enter the key element you want to search\n”);
scanf(“%d”,&key);
low=0;
high=n-1;
while (low<=high)
{
mid=(low+high)/2;
if(key= =a[mid])
{
printf(‘search is successful\n”);
printf(“key element %d found at %d position”, key ,i);
break;
}
else if (key> a[mid ])
{
low=mid+1;
}
else
{
high=mid-1;

Dept. of Computer Science 36 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
}
}
if (low>high)
{
printf(“search is unsuccessfull\n”);
printf(“key element %d not found key”);
}
getch();
}
Output: Enter the required array size:8
Enter the elements in array:
5 10 15 20 25 30 35 40
The entered elements are
5 10 15 20 25 30 35 40
Enter the key element you want to search:
25
Search is successful
Key element 25 found at 4 position
MULTI DIMENSIONAL ARRAY
A multidimensional array associates each element in the array with multiple indexes. The most commonly
used multidimensional array is the two-dimensional array, also known as a table or matrix. A two-
dimensional array associates each of its elements with two indexes. We use the (row, column) notation to
identify an element. For example
2-D array declaration:
To declare a 2D array, you must specify the following:
Row-size: Defines the number of rows
Column-size: Defines the number of columns
Type of array: Defines the type of elements to be stored in the array i.e. either a number, character, or other
such data type . A sample form of declaration is as follows:
type arr [row_size][column_size]
int arr[3][5];

Dept. of Computer Science 37 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
2D array initialization:
An array can either be initialized during or after declaration. The format of initializing an array during
declaration is as follows:
type arr[row_size][column_size] = {{elements}, {elements} ... }
int arr[3][5] = {{5, 12, 17, 9, 3}, {13, 4, 8, 14, 1}, {9, 6, 3, 7, 21}};
Program of Multi-Dimensional Array
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2d array
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}
Output: arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4

Dept. of Computer Science 38 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
arr[3][1] = 5
arr[3][2] = 6
Sparse Matrix
❖ Matrices which contain high number of zero entries are called as sparse matrices.
❖ In simple terms the matrix which contains more zero elements than non zero elements are referred as
sparse matrix.
❖ In this above example there are 20 elements in which 13 elements are zeros and remaining 7
elements are non-zeros.
Program to check whether the given matrix is sparse matrix or not
#include<stdio.h>
#include<stdlib.h>
int main(){
int row,col,i,j,a[10][10],count = 0;
printf("Enter row\n");
scanf("%d",&row);
printf("Enter Column\n");
scanf("%d",&col);
printf("Enter Element of Matrix1\n");
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
scanf("%d",&a[i][j]);
}
}
printf("Elements are:\n");
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
/*checking sparse of matrix*/
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){

Dept. of Computer Science 39 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
if(a[i][j] == 0)
count++;
}
}
if(count > ((row * col)/2))
printf("Matrix is a sparse matrix \n");
else
printf("Matrix is not sparse matrix\n");
}
Output: When the above program is executed, it produces the following result −
Run 1:
Enter row
3
Enter Column
2
Enter Element of Matrix1
102020
Elements are:
10
20
20
Matrix is not sparse matrix
Run 2:
Enter row
3
Enter Column
2
Enter Element of Matrix1
100000
Elements are:
10

Dept. of Computer Science 40 of 41 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
00
00
Matrix is a sparse matrix

Dept. of Computer Science 41 of 41 From the desk of Mr. Chaitanya Reddy Mtech

You might also like