|
发表于 2013-5-3 00:44:42
|
显示全部楼层
- # include <stdio.h>
- # include <conio.h>
- int i,z,x; //其实这里不建议使用全局变量
- int a[10] = {21,53,2,33,-3,3,-44,5,2,10}; //同上
- void find();
- int main ()
- {
- printf ("The elements in array are :\n");
- for (i=0; i<11; ++i)
- {
- printf ("%d ",a[i]);
- }
- printf ("\n nINput a number to be searched: ");
- scanf ("%d",&x);
- find();
- getch();
- return 0;
- }
- void find()
- {
- for (i=0; i<11; ++i)
- {
- if (a[i] == x)
- {
- // z = i + 1;
- printf ("%d is found, it's at %d . \n",x,i);
- }
- }
- if (i == 10)
- {
- printf ("Not exist in the array!");
- }
- }
- /*
- 改进后的代码如上。
- 在VC++ 6.0中的显示结果如下、你的要求是不是这样子:
- ----------------------------------------------------------
- The elements in array are :
- 21 53 2 33 -3 3 -44 5 2 10 0
- nINput a number to be searched: 2
- 2 is found, it's at 2 .
- 2 is found, it's at 8 .
- ----------------------------------------------------------
- */
复制代码 |
|