Sorry about the val. A mistake paste that had slipped in. Fixed now.
My code is deliberately incomplete. It needs something on line 16.
The const is not mandatory.
I thought that in other thread you wrote that the number is already sorted? If it is, then there is no reason to sort it again.
You do sort ascending. Therefore,
number[i] <= number[i + 1]
must be true for all pairs.
For that follows that
number[i] - number[i + 1] <= 0
and
0 <= number[i + 1] - number[i]
Your
sqrt(temp*temp)
does remove the sign, but so would
abs(temp)
, moreover,
number[i + 1] - number[i]
would not even need that.
You still have the main issue that you are not looking for the most isolated value.
Take
0 1 4 5 9 11
.
The largest single distance is between 5 and 9, the 4. Your code would return 5 as the answer. The 5 is not the most isolated. It is only 1 from 4.
Lets make a table: value (v), distance from previous (p), distance to next (n):
1 2 3 4 5 6 7
|
v p n
0 1
1 1 3
4 3 1
5 1 4
9 4 2
11 2
|
For each value you have to look at both distances to find out which one of them is smaller. Lets add one more column, the minimal distance (m):
1 2 3 4 5 6 7
|
v p n m
0 1 1
1 1 3 1
4 3 1 1
5 1 4 1
9 4 2 2
11 2 2
|
Now we see that 9 and 11 are both at least 2 away from their neighbours. The 9 is the first of them and therefore the correct answer.
Can you take my code and wrote on its line 16 a statement that assigns to the 'distance' the "minimal distance"?