2017/1/8 C Language Program Exercise D

Source: Internet
Author: User

There are 10 numbers that are stored in an array in small to large order, and enter a number that requires the binary lookup method to find out that the number is the value of the first element in the array. If the number is not in the array, print "No this number".

Input:-12-8 12 24 45 46 56 58 68 78

Enter the data you want to find: 58

Output: 58 subscript is 7

Enter the data you want to find: 21

Output: "No this number"

#include "stdio.h"

void Chazhao (int a[])

{

int i,s,mid,high,low,flag=1,n=0;

printf ("Please enter the number to look for:");

scanf ("%d", &s);

for (i=0;i<10;i++)

if (S==a[i])

N=1;

if (n==0)

{

printf ("No such number \ n");

flag=0;

}

while (flag)

{

high=10;

low=0;

while (Low<=high)

{

Mid= (High+low)/2;

if (S==a[mid])

{

printf ("%d subscript is%d\n", s,mid);

flag=0;

Break

}

if (S<a[mid])

{

High=mid;

}

if (S>a[mid])

{

Low=mid;

}

}

}

}

void Main ()

{

int a[10];

int i,j;

for (i=0;i<10;i++)

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

Chazhao (a);

}

2017/1/8 C Language Program Exercise D

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.