DEPARTMENT OF COMPUTER ENGINEERING
Power Point Presentation on-
Array based applications in C language
UNDER THE GUIDANCE OF-
SUBMITTED BY- Prof. S.B. Nikam
NAME: Tanmay Sharma PDS – Subject Faculty
ROLL NO.- 15
PRN: 1614110157
CLASS: [Link] Sem-III Computer Engg.-I
Overview
Array
Q .Write a program to implement the given operations on an
array
What is Array?
One-D Array
One-D Array Initialization
Accessing element of array
Searching in Array
• Sequential Search
• Binary Search
Sorting
• Selection Sort
Overview
Array
What is Array?
One-D Array
One-D Array Initialization
Accessing element of array
Searching in Array
• Sequential Search
• Binary Search
Sorting
• Selection Sort
Q .Write a program to implement the given operations on an
array
Array
Basic concepts and their
implementation in C
language
What is Array?
• An array is a group of consecutive memory locations with
same name and data type.
• Simple variable is a single memory location with unique name
and a type. But an Array is collection of different adjacent
memory locations. All these memory locations have one
collective name and type.
• The memory locations in the array are known as elements of
Array. The total number of elements in the array is called
length.
• The elements of array is accessed with reference to its
position in array, that is call index or subscript.
One-D Array
A type of array in which all elements are arranged in the form of
a list is known as 1-D array or single dimensional array or
linear list.
Declaring 1-D Array:
<datatype> <identifier>[length]; e.g.: int marks[5];
Data_type: Data type of values to be stored in array
Identifier: Name of the array
Length: Number of elements 0 1 2 3 4
int marks
One-D array Initialization
The process of assigning values to array elements at the time of
array declaration is called array initialization.
Syntax: 70 21 50 42 11
data_type identifier[length]={values}; e.g.:int marks[2]={1,5};
Data_type: Data type of values to be stored
Identifier: Name of the array
Length: Number of elements
List of values: Values to initialize array.
Accessing element of array
Individual Element
Array_name[index];
Using Loop:
e.g. int a[5];
for(int i=0;i<5;i++)
{ //Input statement
We can run a for
cin>>a[i]; loop to traverse
the array and any
//Output Statement operation like
insertion or
cout<<a[i]; display.
}
Searching in array
Searching is a process of finding the required data in the
array. Searching becomes more important when the length of
the array is very large.
There are two techniques to searching elements in array as
follows:
• Sequential search
• Binary search
Sequential Search
Sequential search is also known as linear or serial search. It
follows the following step to search a value in array.
• Visit the first element of array and compare its value with
required value.
• If the value of array matches with the desired value, the
search is complete.
• If the value of array does not match, move to next element
an repeat same process.
Binary Search
Binary search is a quicker method of searching for value in
the array. Binary search is very quick but it can only search
an sorted array. It cannot be applied on an unsorted array.
• It locates the middle element of array and compare with
desired number.
• If they are equal, search is successful and the index of
middle element is returned.
• If they are not equal, it reduces the search to half of the
array.
• If the search number is less than the middle element, it
searches the first half of array.
Otherwise it searches the second half of the [Link] process
continues until the required number is found or loop completes
without successful search.
Sorting Arrays
Sorting is a process of arranging the value of array in a particular
order. An array can be sorted in two order.
Ascending order
12 25 33 36 43
Descending order
43 36 33 25 12
Types of sorting techniques:
• Bubble sort
• Insertion sort
• Selection sort
• Quick sort
• Merge sort
• Heap sort
• Shell sort
Selection sort
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part
and putting it at the beginning. The algorithm maintains two
subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element
(considering ascending order) from the unsorted subarray is picked
and moved to the sorted subarray. Following example explains the
above steps:
Q .Write a program to implement
the given operations on an array
Find mid of the array and sort the elements before mid in
ascending and after mid in descending order .
Divide the array into size by 3 parts.
Find the maximum element.
Header files and their functions used in
program
iostream.h
• cin – To accept user input
• cout- To display output
• endl- To move to next line
conio.h
• clrscr()- To clear the output screen when program is run
• getch()- To hold the output screen after last execution
process.h
• exit()- To exit the program
Details of program written-
The program proceeds like a normal switch case program. A
menu is displayed and user is asked to enter the choice.
The following will make it more clear-
Now if the user Press 1, an array will be created. The length
of the array will as per user’s desire.
This choice calls a function takearray();
This is the for loop which will
run till the size given by the
user and simultaneously
accept values at ith the index.
Now if the user press 2, the stored array will be sorted in ascending order
before its middle and descending after the middle.
A function midsort(); is called.
This loop shows the stored array
before sorting.
This part is for sorting purpose. It uses the
selection sort to arrange the elements in
ascending order before the middle of the
array.
This part is also for sorting purpose. It
uses the selection sort to arrange the
elements in descending order after the
middle of the array.
This loop shows the stored array after
sorting.
Now if the user Press 3, the array will be divided into size by
3 number of arrays.
A function divide(); is called.
Calculate size/3 and store it in
variable x.
This is a nested loop. This will divide the array
into x triplets and will display after that.
Now if the user press 4, the array is sorted in ascending order
and the very last element in the array is displayed.
A function max(); is called.
This nested loop will sort the array in
ascending order and displays the last
element which is in turn the maximum
value.