C - Get two closest int numbers in an array -


i have array asked user enter. (i asked number of numbers in array , asked numbers). question how compare numbers , find 2 closest ones ?

so, example if user enters 4 numbers like: 3, 5, 8, 9. program should see 8 , 9 closest ones ? after need subtract 2 closest numbers.

this code far:

    int main() {     int n;     int c;     scanf("%d", &n);      (c = 0; c < n; c++)     scanf("%d", &array[c]); 

sort array first. take 3 temp variables n1=0, n2=0, diff=0. iterate sorted array assign n1, n2 a[i], a[i+1], a[] array , iterator.

if     absolute(a[i]-a[i+1]) < diff      diff =  absolute(a[i]-a[i+1]); n1 = a[i];  n2 = a[i+1]; 

when iteration completes have closest elements in n1 , n2. n.b. please take care of array out of bound a[i+1], have given approach.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -