0

Can anyone help me in fix this code of array element search in c

https://code.sololearn.com/c6Mo7RnRRafV/?ref=app

15th Feb 2020, 2:32 PM
Arshia
Arshia - avatar
5 Respostas
+ 1
I don't see the previous warning about non void function not returning data, I don't see what else to fix. Well, you can opt to return the index of found element instead of printing it inside the function. In which case, you setup a variable in main function to hold the return value from `searchElement`. Later on you use that variable, and print it inside main function. int searchElement(int arr[], int n, int key) { for(int i=0;i<n;i++) { if(arr[i]==key) { return i; // return the index } } return -1; // not found } int main() { int pos = searchElement(arr,7, 5); if(pos == -1) // print not found else // print the returned index here return 0; } (Edited)
15th Feb 2020, 3:23 PM
Ipang
+ 1
Function `searchElement` signature shows that it returns an `int`, but you didn't issue a return statement even until the end of the function. Add a return statement, e.g. return the index (if found) or return -1 when not found.
15th Feb 2020, 2:38 PM
Ipang
+ 1
Ipang return the value 1 but what to do next to fix the code
15th Feb 2020, 3:17 PM
Arshia
Arshia - avatar
+ 1
Ipang oh okay this way the functions will work in easy way thanks 😊
15th Feb 2020, 4:23 PM
Arshia
Arshia - avatar
+ 1
You're welcome buddy 👌
15th Feb 2020, 4:25 PM
Ipang