c语言查找元素返回下标,用指针编写在数组中查找指定值的函数:找到返回元素下标,否则返回-1。然后编写主函数测试该函数...

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

nsdku

2013.12.04

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率:48%    等级:9

已帮助:667人

#include

#include

#include

#define random(x) (rand()%x)

int findEle(int *pa, int n)

{

for(int i=0;i<10;i++)

{

if(pa[i] == n)//找到

return i;

}

return -1;//没找到

}

void output(int data,int key)

{

if(key == -1)

printf("%d元素没找到\n", data);

else

printf("%d元素位于%d\n", data, key);

}

void main()//测试程序{

srand((int)time(0));

int pa[10];//数组大小为10

int can_find_it,can_not_find;

for(int x=0;x<10;x++)

{

pa[i] = random(100);//随机生成 

}

can_find_it = pa[0]; //让其中一个能找到

can_not_find = 200;//其中一个找不到

output(can_find_it, findEle(pa,can_find_it));

output(can_not_find, findEle(pa,can_not_find));

}

02分享举报

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值