This might sound like a repeat question but it is not because I haven't found the answer to my question in-spite of going through several references/related questions as listed below (and a lot more not listed).
Minimum difference in an array
Finding out the minimum difference between elements in an array
Find the minimum difference between two arrays
Find the minimum difference between two arrays
Dividing an array into two subsets of equal sizes having minimum difference in sum of values
http://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/
http://programmingpraxis.com/2013/10/15/find-the-minimum-difference/#comment-9146
Following is my program and my own attempt to try and find the "Minimum difference between two elements within the array, any two pairs", then let be there more than one pair of numbers which gives the same minimum difference numerically. I tried to change loops to count them differently and also tried to change the if condition clause for different results but finally ended up with the following code:
#include <iostream> //input output stream
#include <cstdlib>
#include "math.h" //to be able to use math functions if any
using namespace std;
int main()
{
int x=0,y=0,number1 = 0,number2 = 0,size1=0,mindiff = 0;
int index = 0, count = 0, diff = 0;
cout<<"enter the size of array you want \n\n";
cin>>size1;
int arr1[size1];
cout<<"\n\n please enter elements of array \n\n";
for (int i =0;i<size1;i++)
{
cin>>arr1[i];
}
for ( x=0;x<size1;x++ )
{
diff = 0;
for ( y=0;y<size1;y++ )
{
diff = abs(arr1[x] - arr1[y]);
if (diff<mindiff)
{
mindiff = diff;
//index = x+1;
number1 = arr1[x];
number2 = arr1[y];
}
}
}
cout<<"\nthe elements amongst which the minimum difference was given are "<<number1<<" & "<<number2<<endl;
cout<<"the difference between them is "<<abs(mindiff)<<endl;
system("pause");
return 0;
}
//end of program
All the above references and lots more but could not find anyone that follows my perspective. I know that i may be doing something really silly/wrong out here because i get a maximum difference and not minimum difference.Where am I going wrong? I anyways have to keep on assigning the new minimum every time a lower difference is found as written in the if condition statement right???
following is the output
enter the size of array you want
6
please enter elements of array
1 2 34345 7 8 9
the elements amongst which the minimum difference was given are 1 & 34345 the difference between them is 34344
Press any key to continue . . .