问题:实现在指定数组中查找指定元素的功能,如果找到该元素返回该元素的下标,否则返回-1:

2024-12-18 13:56:23
推荐回答(1个)
回答1:

#include
#include

int main()
{
int len, data, ret=-1,input;
int *arr;
scanf("%d %d", &len, &data);

arr = (int*)malloc(len*sizeof(int));

for (int i=0; i {
scanf("%d",&input);
arr[i] = input;
}

for (int j=0; j if (arr[j] == data)
{
ret = j;
break;
}
}

printf("\n%d", ret);
}