# programming help

Discussion in 'C' started by arindam.kotal, Dec 3, 2012.

1. ### arindam.kotalNew Member

Joined:
Nov 4, 2012
Messages:
13
0
Trophy Points:
0
I have the set of matching pairs say p=(0,1),p=(1,2),p=(4,0),p=(0,5),p=(2,4),p=(3,3),p=(5,1),p=(1,7),p=(2,6)
now,I want to print those pairs whose sum is same but the difference between
the elements of each pair is minimum.
i.e. say p,p,p have the same sum 6.
the difference of elements in p is 2 ,in p is 0 & in p is 4.
so we will choose p.
the result should print p,p,p,p,p.

2. ### DRKNew Member

Joined:
Apr 13, 2012
Messages:
44
3
Trophy Points:
0
I don't quite get why p,p,p,p,p should be the result. I'm giving you the algorithm to calculate sum and difference for each pair, you'll have to handle the rest.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
const char *p;
int i, v1, v2, sum, difference;
p = "(0,1)";
p = "(1,2)";
p = "(4,0)";
p = "(0,5)";
p = "(2,4)";
p = "(3,3)";
p = "(5,1)";
p = "(1,7)";
p = "(2,6)";
for (i = 0; i < 9; ++i)
{
sscanf(p[i], "(%d,%d)", &v1, &v2);
sum[i] = v1 + v2;
difference[i] = abs(v1 - v2);
}
return 0;
}```

3. ### arindam.kotalNew Member

Joined:
Nov 4, 2012
Messages:
13
0
Trophy Points:
0
sir,I understand your algo. But I already calculated the sum . I just want to find out those pairs which have equal sum but the difference between the element
should be minimum.
Like,from the the above example ,
p,p,p,p dont have the same sum. So this will be printed.
But p,p,p have the same sum so we have to calculate their differences between the elements.
Its find that p has the minimum difference i.e. 0 so,we select p .
In the same way p & p have the same sum but p has the minimum sum so we have to select p.
so the final matching pairs should be,
p,p,p,p,p,p.

4. ### arindam.kotalNew Member

Joined:
Nov 4, 2012
Messages:
13
0
Trophy Points:
0
my code is below,
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<time.h>
#include<math.h>
#include <stdlib.h>
int main()
{
int b={0,1,4,1,2,2,3,3,4,4,5};
int c={1,6,3,7,4,6,3,8,0,7,1};
int y,k=0,p,q;
for(int i=0;i<11;i++)
{
for(int j=i+1;j<11;j++)
{
if((b+c)==(b[j]+c[j]))
{
y[k]=abs(b-c);
y[k+1]=abs(b[j]-c[j]);

if(y[k]>y[k+1])
{
p=b[j+1];
q=c[j+1];
}
else
{
p=b[j];
q=c[j];
}
}
else
{
p=b;
q=c;
}

}
printf("%d%d\n",p,q);
}
getch();
return 0;
}