### Function to output all combinations that give a certain value

I have two vectors _A and _B and an equation (x-y)/2, where the x is vector _A and y is _B. So for example _A={8,1,16} and B={4,1,8} my output should be all the combinations that give a postive integer greater than 0 so {(8,4),(16,8)}. How can i make it output only the numbers that work for this equation.

My Code for the function:

for(int i =0; i<_A.size(); i++){
for(int j=0; j<_B.size();j++){
int INT =( _A.at(i) - _B.at(j))/2;
if (INT % 1 == 0 && INT > 0) {
cout << "{";
for(int i =0; i<_A.size();i++){

cout << "(" << _A.at(i) << "," << _B.at(i) << "),";
}
cout << "}";
cout << endl;

}else{
cout << "The number is not an intger" << endl;
}
}
}
you want %2 instead of %1 I think.
%1 checks if a number is zero (its either 1 or undefined). %2 checks if it is even/odd (divisible by 2). ...
Last edited on
Thanks joninn I think I finally got it working
Not sure that I agree with your model output:

 ``12345678910111213141516171819`` ``````#include #include using namespace std; vector> evenPositiveDifference( const vector &A, const vector &B ) { vector> result; for ( int x : A ) { for ( int y : B ) if ( x > y && ( x - y ) % 2 == 0 ) result.push_back( { x, y } ); } return result; } int main() { vector A = { 8, 1, 16 }, B = { 4, 1, 8 }; for ( auto p : evenPositiveDifference( A, B ) ) cout << p.first << ", " << p.second << '\n'; }``````

 ```8, 4 16, 4 16, 8```
Topic archived. No new replies allowed.