Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
4Activity
0 of .
Results for:
No results containing your search query
P. 1
ARRAY

ARRAY

Ratings: (0)|Views: 85|Likes:
Published by api-19804685

More info:

Published by: api-19804685 on Nov 29, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

1 |Page
ARRAY
Q. To search an element from an array using non-recursive function.
Ans.
Program in language C:

/*********Binary Search using non-recursive function*********/
#include<conio.h>
#include<stdio.h>
/************************************************************/
void binary_search(int arr[], int start, int end, int n)

{int mid;
while(start<=end)
{mid=(start+end)/2;
if(arr[mid]==n)
{printf("\nElement found in position %d", mid+1);

getch();
return;
}

if(arr[mid]<n)
start=mid+1;
if(arr[mid]>n)
end=mid-1;

}
printf("\nElement not found");
getch();
}

/*************************************************************/
void main()
{int ar[10], ele, i, n;

clrscr();
printf("\nEnter number of elements in the array: ");
scanf("%d",&n);
printf("\nEnter the elements in the array:\n");
for(i=0;i<n;i++)

{scanf("%d",&ar[i]);

}
printf("\nEnter the element to be searched:");
scanf("%d",&ele);
binary_search(ar,0,n-1,ele);
}

/*************************************************************/
2 |Page
Output of the program in C:
Q. To search an element from an array using recursive function.
Program in language C:

/******** Binary Search using recursive function*********/
#include<conio.h>
#include<stdio.h>
/********************************************************/
void binary_search(int arr[], int start, int end, int n)

{int mid;
mid=(start+end)/2;
if(arr[mid]==n)
printf("\nElement found in position %d of the array.",mid+1);
if(arr[mid]<n)
binary_search(arr, mid+1, end, n);
if(arr[mid]>n)
binary_search(arr, start, mid-1, n);

}
/*********************************************************/
void main()

{int ar[50], ele, i, n;

clrscr();
printf("\nEnter number of elements in the array: ");
scanf("%d",&n);
printf("\nEnter the elements in the array:\n");
for(i=0;i<n;i++)

{

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->