return the 2 numbers that will give the minimum

Hi all, i'm totally new on C++, that's why i'm posting my question in this forum.

I'm elaborating my question as an example. i need to allocate each elements in a1 to pair with element in a2 that will return the smallest difference.

For a start, i use a1[0] only.

array a1: [34,25,27,86]
array a2: [75,87,96,98]
difference between a1[0] and all elements in a2: [41,53,62,64] ->return min=41

As the min=41, which means that i need to put (34,75) into another array and remove the 2 numbers from the existing arrays, how can i do so?

Kindly advise me on the algorithm / pseudocode as i would like to try the code myself.

Thanks for the help.
1) Compare each
eg:
1
2
3
4
minimum =0;
for (int j; j <4; j++)
difference = array1[j] - array[2];
[

here I only did with one loop, but you will need 2 loops. To compare each element of array1 to each element of array2
2) If difference if less then minimum then that will be the minimum.

3) every time you get a minimum store your array1[j] and array2[j].
eg:
say array3[length].
if you get the minimum then do length = length +2 since you have used 2 spaces.
hope this helps :)
Just as a suggestion, put a check for negative differences.. For eg:

consider an element 10 to be compared with the array [30, 40, 50, 60, 70]

The differences would come out to be as [-20, -30, -40, -50, -60]

clearly, it would consider 70 to be paired with 10, as -60 is the least in the above array. This is incorrect output. So have a check as:
1
2
if (difference<0)
  difference=-difference;


Hope this helps... :)
To Caprico: Thanks for your suggestion. it really helps:)

To biplav7: For point 1 and 2 i understand. But for point 3, i'm not able to store the array1 and array2 values

array syntax(for what i known is as): int billy[] = {1,2,3};

for mine i do like this:
1
2
3
4
5
6
a=1;
b=2;
c=3;

int billy[4]; //declaration of array to  store 4 records
billy[0] = {a,b,c}; //this sentence is there any wrong about it? 
Yes, there is! billy is a 1D array. In line 6, you attempt to store three ints in a memory location that can hold only one...
I'm assuming what biplav17 meant was, after you find a pair (that gives you minimum difference), you store the ints in adjacent blocks of the result array. For example, suppose you find that the ith element of array1 matches with jth element of array two, then:
1
2
3
array3[length]=array1[i];
array3[length+1]=array2[j];
length+=2;                          //Equivalent to length=length+2; 


You really can't remove an element of an array that easily. You have basically 2 options:
1) create a new array not containing the removed element (not a really efficient one but it's straight forward)

2) place the elements you don't need a t the end of your array and don't use them (if your array has N elements afterward it will use only the first N-1 elements this way)

3) As a variation of this 2nd method you can keep a track of the "removed" elements and skip them when you encounter them without moving them to a different point.

Also I did not get some point of your desired algorithm:
How are you going to choose which element to pair if more than one match the same element. From you example you have:
array a1: [34,25,27,86]
array a2: [75,87,96,98]

Apparently a1[0] should match a2[3] but also do a1[1] and a1[2] and besides the difference for those from a2[3] is bigger then that of abs(a1[0]-a2[0]). In this case which criterion are you going to use? Also if you intend to remove the elements them obviously different pairs will evolve.

eypros, in your example, why would a1[0] match with a2[3]? By what he's told us, and what I've understood, he wants to pair up numbers which have the least difference between them. I know this would result in different answers, depending on choice of your first array, and your first element. So we're taking the first array as the comparison factor, and going through it element by element, comparing with the other array.
Topic archived. No new replies allowed.