課程
/后端開發
/C
/C語言入門
6.6
2016-11-17
源自:C語言入門 6-5
正在回答
#include <stdio.h>
int getIndex(int arr[5],int value)
{
? ? int i;
? ? int index;
? ? for(i=0;i<5;i++)
? ? {
? ? ? ?/* 請完善數組查詢功能 */
? ? ? ?if(arr[i]==value)
? ? ? ?{
? ? ? ? ? ?
? ? ? ? ? ?index=i;
? ? ? ? ? ?break;
? ? ? ?}
? ? ? ? ? else
? ? ? ? ? ?index=-1;
? ? ? ?
? ? }
? ? return index;
}
int main()
? ? int arr[5]={3,12,9,8,6};
? ? int value = 8;
? ? int index = getIndex( arr, value); ? ? ?//這里應該傳什么參數呢?
? ? if(index!=-1)
? ? ? ? printf("%d在數組中存在,下標為:%d\n",value,index); ? ? ? ? ? ??
? ? else
? ? ? ? printf("%d在數組中不存在。\n",value); ? ?
? ? return 0; ? ?
舉報
C語言入門視頻教程,帶你進入編程世界的必修課-C語言
1 回答C語言入門6-8
1 回答C語言入門2.4章節
2 回答C語言6-3節練習題
1 回答c語言%C
1 回答第一次學習C語言
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-11-18
#include <stdio.h>
int getIndex(int arr[5],int value)
{
? ? int i;
? ? int index;
? ? for(i=0;i<5;i++)
? ? {
? ? ? ?/* 請完善數組查詢功能 */
? ? ? ?if(arr[i]==value)
? ? ? ?{
? ? ? ? ? ?
? ? ? ? ? ?index=i;
? ? ? ? ? ?break;
? ? ? ?}
? ? ? ? ? else
? ? ? ?{
? ? ? ? ? ?index=-1;
? ? ? ?}
? ? ? ?
? ? ? ?
? ? }
? ? return index;
}
int main()
{
? ? int arr[5]={3,12,9,8,6};
? ? int value = 8;
? ? int index = getIndex( arr, value); ? ? ?//這里應該傳什么參數呢?
? ? if(index!=-1)
? ? {
? ? ? ? printf("%d在數組中存在,下標為:%d\n",value,index); ? ? ? ? ? ??
? ? }
? ? else
? ? {
? ? ? ? printf("%d在數組中不存在。\n",value); ? ?
? ? }
? ? return 0; ? ?
}