圖中的index有問題吧,輸出index不對
#include <stdio.h>
int getIndex(int arr[5], int value)
{
? ? int i;
? ??
? ? for (i = 0; i < 5; i++)
? ? {
? ? ? ? /* 請完善數組查詢功能 */
? ? ? ? if (arr[i] == value)
? ? ? ? {
? ? ? ? ? ? return i;
? ? ? ? ? ?
? ? ? ? }
? ? ??
? ? }?
? ? return -1;
}
int main()
{
? ? int arr[5] = { 3,12,9,8,6 };
? ? int value =9;
? ? int index = getIndex(arr, value);? ? ? //這里應該傳什么參數呢?
? ? if (index != -1)
? ? {
? ? ? ? printf("%d在數組中存在,下標為:%d\n", value, index);
? ? }
? ? else
? ? {
? ? ? ? printf("%d在數組中不存在。\n", value);
? ? }
? ? return 0;
}
2021-08-04