C程序输出和题意不相符,问修改?
题目:有15个数按由小到大的顺序存放在一个数组中。输入一个数,要求用折半查找法找出该数是数组中第几个元素的值,
如果该数不在数组中,输出“不在表中”。
要求:在我的程序上进行修改
#define N 15
#include<stdio.h>
void main()
{int number ,top,bott,mid,a[N],sign,flag=1,i=0,loca;
char c;
printf("enter data:\n");
scanf("%d",&a[0]);
i=1;
while(i<N)
{scanf("%d",&a[i]);
if(a[i]>a[i-1]) i++;
else printf("enter the data again:\n");
}
printf("\n");
for(i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");
while(flag)
{ printf("the number to look for is:\n");
scanf("%d",&number);
top=0;bott=N-1;sign=0;
if(number<a[0]||number>a[N-1]) loca=-1;
while((!sign)&&(top<=bott))
{mid=(top+bott)/2;
if(number==a[mid]) {
loca=mid;
printf("Has found %d,the position is %d.\n",number,loca+1);
sign=1;
}
else if(number<a[mid]) bott=mid-1;
else top=mid+1;
}
if(!sign||loca==-1) printf("can not find %d.\n ",number);
printf("continue or not(Y/N)?\n");
scanf("%c",&c);
if(c=='N'||c=='n')
flag=0;
}
system("pause");
}
如果该数不在数组中,输出“不在表中”。
要求:在我的程序上进行修改
#define N 15
#include<stdio.h>
void main()
{int number ,top,bott,mid,a[N],sign,flag=1,i=0,loca;
char c;
printf("enter data:\n");
scanf("%d",&a[0]);
i=1;
while(i<N)
{scanf("%d",&a[i]);
if(a[i]>a[i-1]) i++;
else printf("enter the data again:\n");
}
printf("\n");
for(i=0;i<N;i++)
printf("%3d",a[i]);
printf("\n");
while(flag)
{ printf("the number to look for is:\n");
scanf("%d",&number);
top=0;bott=N-1;sign=0;
if(number<a[0]||number>a[N-1]) loca=-1;
while((!sign)&&(top<=bott))
{mid=(top+bott)/2;
if(number==a[mid]) {
loca=mid;
printf("Has found %d,the position is %d.\n",number,loca+1);
sign=1;
}
else if(number<a[mid]) bott=mid-1;
else top=mid+1;
}
if(!sign||loca==-1) printf("can not find %d.\n ",number);
printf("continue or not(Y/N)?\n");
scanf("%c",&c);
if(c=='N'||c=='n')
flag=0;
}
system("pause");
}
...全文
相关推荐
使用VC6.0对C语言程序进行调试的基本手段分享
如果输出窗口中的程序输出不符合预期,那么也说明在断点之前的代码中有问题。
【C程序】popen获取系统命令的输出内容
#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; int main (void) { int ssh_cnt = 0; char cmd[256] = {0}; int status = 0; FILE* fp = NULL; scanf ("%d", &...
c语言程序设计-习题集-含答案.pdf
总结:本资源提供了C语言程序设计的习题集,涵盖了C语言的基本概念、数据类型、运算符、表达式、函数、数组、指针、结构体、文件输入输出等方面的知识点,为学习和掌握C语言提供了系统和全面的参考资料。
应用程序无法启动,因为并行配置不正确
标签 "C++"、"Windows"、"Visual-Studio"、"VS2010" 和 "Win32" 提示我们问题可能与使用C++语言开发、在Windows环境下运行,通过Visual Studio
C和C++程序设计常见的错误代号收集
以上是C和C++程序设计中常见的一些错误类型及其解释。掌握这些基础知识有助于避免常见的编程陷阱,提升代码质量和可读性。在实际编程过程中,对这些规则的深入理解和灵活应用将大大减少调试时间和提高开发效率。


