Documente Academic
Documente Profesional
Documente Cultură
C++ PROGRAMMING
Sign and grade:_____________
Lab report# 08
Objectives:
❖ To discuss array in detail.
❖ To know about searching in array i-e sequential search and binary search.
❖ To understand that how an array can be sorted in descending or ascending order by
either selection sorting or bubbling sorting.
❖ To know about declaration and initialization of two-dimensional array as well as of
multi-dimensional array.
Theory:
Searching in Arrays is a process of finding the required data in the array. Searching becomes
more important when the length of the array is very large. Searching in array can be find by two
method.
1) Sequential Search is also called linear search or serial search. It is a simple way to search
an array for the desired value. First of all it visit the first element of the array and
compare its value with the 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 and repeat same process.
2) Binary Search is a quicker method of searching for value in the array. Binary search is
very quick but it can only search in sorted array. First of all it locates the middle element
of array and compares with the search number. If they are equal, search is successful
and the index of middle element is returned. 3) If they are not equal, it reduces the
search to half of the array. 4) 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 array. The
process continues until the required number is found or loop completes without
successful search.
Sorting Arrays is a process of arranging the values of array in a particular order. An array can be
sorted in two orders: 1) Ascending order 2) Descending order. Sorting can be possible either by
selection sorting or bubble sorting.
Two-Dimensional Array can be considered as a table that consists of rows and column.Each
element in 2-D array is referred with the help of two indexes. One index is used to indicate the
row and the second index indicates the column of the element.
Multi-Dimensional Array is also called as array of arrays. Multidimensional arrays are not
limited to two indices. Multidimensional arrays can have three, four or more dimensions. The
amount of memory needed for an array rapidly increases with each dimension.
Title:
Write a program that initializes an array. It inputs a value from the user and searches the
number in the array.
Algorithm:
1) Start
2) Define an array and variable
3) Enter value to search in array
4) Repeat step 5 while I not equal to last index
5) If value find at index, noted the index and leave the loop
6) Print that value found at index else not founded at any index
7) End
Start
Flow chart:
Input value in array
Define variables
Loc=-1
i=0
If(n==arr[i])
If(loc
==-1) Loc=arr[i]
Display
result
End
Coding:
#include<iostream.h>
#include<conio.h>
void main ()
{ int n, i, loc=-1;
arr[10]={10,34,65,33,78,45,23,65,54,99};
cout<<”enter value for searching:”;
cin>>n;
for(i=0;i<10;i++)
{ if(n==arr[i])
{ break;
loc=i;}
}
if(loc==-1)
cout<<”value not found.”;
else
cout<<”value found at index “<<loc;
getch (); }
Output:
Title:
Write a program that initializes an array of ten integers. It inputs an integer from the user and
searches the value in the array using binary search.
Algorithm:
1. Start
2. Define an array and variables and enter number for searching
3. Repeat the step 4,5, and 6 while first index less than last index
4. Find middle term, if middle term is equal to required number leave the loop
5. Else if required number less than arr[mid], end=mid-1 else start=mid+1
6. Print index at which number found else number not founded
7. End
Coding:
#include<iostream.h>
#include<conio.h>
void main ()
int arr[10]={10,15,17,26,45,56,65,76,78,89};
cin>>n;
while(start<=end)
{ mid=(start+end)/2;
if(arr[mid]==n)
{loc=mid; else
end=mid-1; }
else
start=mid+1 }
if(loc==-1)
Flow chart:
Input value in array[10]
Define variables
Loc=-1
start=0
end=9
Loc=arr[mid]
If(n==arr[mid]
Leave the loop )
Is start<end
End
Output:
Title:
Write a program that stores five values in an array. It sorts the array using bubble sort. It also
displays the values of unsorted and sorted array.
Algorithm:
1. Start
2. Define a array and variables
3. Repeat the step 4 while i less than 5, initially i=0
4. Enter value to store it in array[i]
5. Print the given value in their actual order (unsorted order)
6. Repeat the step 7 while i less than 5
7. Repeat the step 8 while j less than 4
8. If first index less than second index, replace their indexes
9. Increase j by 1 and also i by 1
10.Display the sorted order of given number
11.End
Start
Define variables
i=0
j=0
Is i<5 i++
temporary=array[j]
Print array[i] with tab
array[j]=array[j+1]
space
array[j+1]=temporary
Is i<5 i++
Is j<4
i=0
Is i<5 i++
Display output
End
Coding:
#include<iostream.h>
#include<conio.h>
void main ()
{ int i, j, array[5];
for(i=0;i<5;i++)
{ cout<<”enter value:”;
cin>>array[i]; }
cout<<”the value in unsorted form is:”<<endl;
for(i=0;i<5;i++)
cout<<array[i]<<”\t”;
for(i=0;i<5;i++)
for(j=0;j<4;j++)
if(array[j]>array[j+1])
{ temp=array[j];
array[j]=array[j+1];
array[j+1]=temp; }
cout<<”\n the sorted array is:”<<endl;
for(i=0;i<5;i++)
cout<<array[i]<<”\t”;
getch (); }
Output: