以下程序功能是在数组中查找与x值相同的元素所在的位置。
#include “stdio.h”
void main( )
{ int i,x,a[11];
printf(“please input ten number:”);
for(i=1;i<11;i++)
scanf(“%d”,a+i);
printf(“please input x: “);
scanf(“%d”,&x);
a[0]= ① ;
i=10;
while(x != *(a+i) )
② ;
if( ③ )
printf(“found position is :%d”,i);
else
printf(“not found!”);
}
#include “stdio.h”
#include “conio.h”
void main( )
{int i,x,a[11];
printf(“please input ten numbers:”);
for(i=1;i<11;i++) scanf(“%d”,a+i);
printf(“please input x: “);
scanf(“%d”,&x);
a[0]= x ;
i=10;
while(x != *(a+i) )
i– ;
if( i !=0 )
printf(“found position is: %d”,i);
else
printf(“not found!”);
getch();
}
程序的执行结果是
please input ten numbers:1 2 3 4 5 6 7 8 9 10
please input x: 5
found position is: 5
please input ten numbers:1 2 3 4 5 6 7 8 9 11
please input x: 10
not found!
暂无评论内容