Professional Documents
Culture Documents
Index
Index
No.
1.
Aim
Pg.
No.
2
2.
Apparatus
3.
Theory
4.
Procedure
5.
Observation
10
6.
Calculation
11
7.
Results
14
8.
Precautions
15
9.
Sources of Error
16
10.
Bibliography
17
SL
NO
.
TOPICS
I.
II.
III.
PROGRAMS ON INHERITANCE
IV.
V.
VI.
PROGRAMS ON ARRAYS
VI.
Programs on Arrays
Question 1:
Write a program to search for 66 and 71 in the following array:
3, 4, 7, 11, 18, 29, 45, 71, 87, 89, 93, 96, 99.
Make use of binary search technique.
Solution:
#include<iostream.h>
int Bsearch(int [],int);
int main()
{
int A[]={3,4,7,11,18,29,45,71,87,89,93,96,99};
int index;
index=Bsearch(A,71);
if(index==-1)
cout<<"Element not found..";
else
cout<<"Element found at
index:"<<index<<"/Position:"<<index+1<<endl;
return 0;
}
int Bsearch(int A[],int item)
{
int beg,last,mid;
beg=0; last=13-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==A[mid]) return mid;
else if (item>A[mid]) beg=mid+1;
else last=mid-1;
}
return -1;
}