函数查找

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 
 5 
 6 int search();
 7 
 8 
 9 int main()
10 {
11 
12 
13    int index = search();
14    printf("找到的元素的下标是:%d\n",index);
15 
16    return 0;
17 
18 
19 }
20 
21 int search()
22 {
23     int nums[] = {10,20,30,40,50};
24     int searchNum;
25     int searchIndex = -1;             //给一个不可能的数字
26     int i;
27 
28     printf("请输入要找的数字:\n");
29     scanf("%d",&searchNum);
30 
31     for (i = 0; i < 5; i++)
32     {
33         if( searchNum == *(nums +i))
34         {
35 
36             searchIndex = i;   //记录下标
37             break;
38         }
39 
40     }
41 
42     return  searchIndex;
43 }

//没找到

 

 //找到了

 

posted on 2021-07-28 10:45  Bytezero!  阅读(37)  评论(0编辑  收藏  举报