Skip to content

Update exercise5.c #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

WongChinWee
Copy link

//documentation section
/* Exercise 5 - Binary Search by Wong Chin Wee */

//pre-processor section
#include<stdio.h>

//global variable section

//main function section
int main(){
//write here your program
int n,i,a,low,high,mid;
printf("Enter the number of elements: ");
scanf("%d",&n);
int array[n];
printf("Enter the elements in a sorted manner:\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("Enter the value to find: ");
scanf("%d",&a);
low=0;
high=n-1;
mid=(low+high)/2;

while(low<=high)
{
    if(array[mid]<a)
    low=mid+1;
    else if(array[mid==a])
    {
        printf("Element with value %d is present at index %d",a,mid+1);
        break;
    }
    else
    high=mid-1;
    mid=(low+high)/2;
}
if(low>high)
printf("%d is not present in the array.",a);
return 0;

}

//user-defined section

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant