下面程序要实现的功能是:用二分法在一个升序排列的数组中查找某个数。若存在,输出该数及其下标位置;若不存在,则输出表示找不到此数的信息。该程序中有4处错误。
改错形式:
错误一:原语句: 修改为:
错误二:原语句: 修改为:
错误三:原语句: 修改为:
错误四:原语句: 修改为:
【带错误的源代码】
#include
int main(void)
{
int a[10] = { 2,5,6,8,11,15,18,22,60,88 };
int low, high, m, i, x;
scanf("%f", &x);
low = 0; high = 9;
while (low > high)
{
m = (low + high) / 2;
if (x == a[m]) break;
else if (x > a[m]) high = m + 1;
else low = m - 1;
}
if (low <= high)
printf("%d is found, the position is %d", x, m);
else printf("%d is not found\n", x);
return 0;
}
改错形式:
错误一:原语句: 修改为:
错误二:原语句: 修改为:
错误三:原语句: 修改为:
错误四:原语句: 修改为:
【带错误的源代码】
#include
int main(void)
{
int a[10] = { 2,5,6,8,11,15,18,22,60,88 };
int low, high, m, i, x;
scanf("%f", &x);
low = 0; high = 9;
while (low > high)
{
m = (low + high) / 2;
if (x == a[m]) break;
else if (x > a[m]) high = m + 1;
else low = m - 1;
}
if (low <= high)
printf("%d is found, the position is %d", x, m);
else printf("%d is not found\n", x);
return 0;
}