### pairing numbers( loops and dynamic list)

Ok so i have a group of numbers in a vector container and i want to pair them this way...eg; if these are my numbers ( 23, 25, 35, 34,75) i want to pair them this way...
(23, 25) (23,35) (23,34) (23,75) (25,35) (25,34) (25,75) (35,34) (35,75) (34,75)
such that every number is paired with any other number in the group...once.
since the quantity of numbers in the vector container may change i used a dynamic list...
I have written my code for it but for some reason is not working properly. Am gonna paste my code here..so that the experts on the forum can help me out. thanks in advance.
So am just going to post the part of the code related to my problem...i dont think is a good idea copying the whole thing and pasting it here..is a long one...to make things easier for you guys who are gonna help me out..
vector cross_check was created and is filled up somewhere in the original code... thanks

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct dropping_pairs {
int x;
int y;
dropping_pairs * next;
};

void dropping_pairing (dropping_pairs * & testa, int & i, int & j);
void display_pairs ( dropping_pairs * & testa);

int main (){

int k = 0, j;
if (k < cross_check.size ()- 1)
{for( j = k+1; j< cross_check.size(); j++){
dropping_pairing(start_of_drops, k, j);}
k +=1;
}
display_pairs(start_of_drops);
return 0;
}

void dropping_pairing (dropping_pairs * & testa, int & k, int & j){
dropping_pairs * temp;
temp = new dropping_pairs;
temp->x = cross_check [k];
temp->y = cross_check [j];
temp->next = 0 ;
if ( testa == 0) testa = temp;
else
{ dropping_pairs * curr = testa;
if (curr->next != 0) curr = curr->next;
curr->next = temp; }
}

void display_pairs ( dropping_pairs * & testa){
dropping_pairs * print = testa;
for (print = testa; print->next != 0; print=print->next)
cout<<"("<<print->x<<","<<print->y<<")";
}

so why is it not working? Thank you!
Assuming the sequence contains unique numbers:
 ``123456789101112131415161718192021`` ``````#include #include #include std::vector< std::pair > pair_them( const std::vector& seq ) { std::vector< std::pair > pairs ; for( std::size_t i = 0 ; i < seq.size() ; ++i ) for( std::size_t j = i+1 ; j < seq.size() ; ++j ) pairs.emplace_back( seq[i], seq[j] ) ; return pairs ; } int main() { for( const auto& p : pair_them( { 23, 25, 35, 34, 75 } ) ) std::cout << '(' << p.first << ',' << p.second << ") " ; std::cout << '\n' ; }``````

http://liveworkspace.org/code/1eY4Av\$0
wow, JLBorges,
what do you mean by "unique numbers" please?
thank you in advance
> what do you mean by "unique numbers"

No duplicates.

You hadn't specified what is to be done for a sequence with non-unique numbers. Like:
`{ 23, 25, 23, 35, 34, 35, 75 }`
yeah there are no duplicates in the cross_check container...any number which appears there appears one..thank you
JLBorges but anyway can you please tell me why my code didn't work? yours is working fine..thank you but i will like to know why mine didn't work can you shed some light??? thank you
 ``123456`` ``````//if (k < cross_check.size ()- 1) // **** while (k < cross_check.size ()- 1) {for( j = k+1; j< cross_check.size(); j++){ dropping_pairing(start_of_drops, k, j);} k +=1; }``````

and

 ``123456`` ``````else { dropping_pairs * curr = testa; //if (curr->next != 0) curr = curr->next; // **** while (curr->next != 0) curr = curr->next; curr->next = temp; } }``````
Topic archived. No new replies allowed.