You have a problem in your code. Change
scanf("%d",arr[i]);
To
scanf("%d",&arr[i]);
This is done because scanf
expects an argument of type int*
but you provide argument arr[i]
which is of type int
. Also add a check that ends the program if user inputs a number which is greater than 10 for the first scanf
.
There can be two reasons.
Case 1 [Much likely for _always_]
Simple. Because your if(arr[i]==n)
condition is not met, and i<m
became false. It came out of for()
loop and hence, return 20
.
case 2 [Less likely for _always_]
By chance, the value of n
is present at the 21st location [index 20] in the input array.
Apart from the coding aspect, did you understand what’s the logical purpose of this function? If not, begin with that. It searches for a specific value in an array of given length, and if no element of the array matches that value, it returns 20.
Now analyze your case, based on your input.
EDIT:
After seeing the complete code, as Mr. CoolGuy has pointed out, use
scanf("%d",&arr[i]);
Just for more reference, you can look at Chapter 7.19.6.2, paragraph 12 , %d
format specifier, which goes like
… The corresponding argument shall be a pointer to signed integer.
In your code, arr[i]
is of type int
. What you need is a int *
, i.e., &arr[i]
.