Hi,

I need to make a pairs between array elements. For eg if the sored array I have is

1 1 
1 2 
1 4 
1 6 
2 3 
2 5 
2 8 
2 9 
3 7 
3 10

then I need to pair every element of the 2nd column like this.

{(1,3),(1,5),(1,8),(1,9),(1,7),(1,10),(2,3).....}

ie pairs are formed between elements only if their elements in the 1st row are different.
I have written code for it though I am sure it could be written more neatly.

#include <iostream>
#include <new>
#include <cstdio>
#include <vector>
#include <cstring>
#include <fstream>
#include <cstdlib> 


int main(){

        vector<vector <int> > readassgn(10,vector<int>(2,0));
        cout << endl;
        readassgn[0][0]=1;
        readassgn[0][1]=1;
        
        readassgn[1][0]=2;
        readassgn[1][1]=8;
        
        readassgn[2][0]=2;
        readassgn[2][1]=9;
        
        readassgn[3][0]=3;
        readassgn[3][1]=7;
        
        readassgn[4][0]=2;
        readassgn[4][1]=5;
        
        readassgn[5][0]=1;
        readassgn[5][1]=2;
        
        readassgn[6][0]=3;
        readassgn[6][1]=10;
        
        readassgn[7][0]=1;
        readassgn[7][1]=6;
        
        readassgn[8][0]=2;
        readassgn[8][1]=3;
        
        readassgn[9][0]=1;
        readassgn[9][1]=4;
         
        cout << "initial array" <<endl;       
        for (int i=0;i<readassgn.size();i++){
        	for (int j=0;j<2;j++){
        		cout << readassgn[i][j] << " ";
        	}
        	cout <<endl;
        }
                
       sort(readassgn.begin(),readassgn.end());

       cout <<"sorted array " << endl;
      
       for (int i=0;i<readassgn.size();i++){
         	for (int j=0;j<2;j++){
         		cout << readassgn[i][j] << " ";
         	}
         	cout <<endl;
         }
                
               vector <int> count(5,0);
               vector<int> currentval;
               int current = readassgn[0][0];
               
               count[current]++;
               for (int i=1;i<readassgn.size();i++){
            	   if(readassgn[i][0]==current){
            		   count[current]++;
            	   }
            	   else{
            	        current=readassgn[i][0];
               	        count[current]++;
            	   }
               }
               cout << "currentval" <<endl;	   
               for (int i=0;i<currentval.size();i++){
            	   cout << currentval[i]<< "  ";
               }
                cout <<endl;
                cout << "count" <<endl;
                for (int i=0;i<5;i++){
                	cout << count[i]<< " ";
                }
                
                cout <<endl;

                vector<vector<int> > constraintpair(10,vector<int>(2,0));
                               
                
                // making constraint pairs.
                int iter,t,e,m,val,j,s=0;
             
                for (iter=1;iter<5;iter++){
                	
                	val=count[iter];
                	
                	while(e<val){
                		for (int h=iter;h>0;h--){
                			m+=count[h-1];
                		}
                		
                		s=readassgn[j][1];
                		for (int z=val+m;z<readassgn.size();z++){
                			constraintpair[t][0]=s;//-------terminates here
                			constraintpair[t][1]=readassgn[val+z][1];
                			t++;
                		}
                		e++;
                		j++;
                		
                	}
                }
                
                for (int i=0;i<readassgn.size();i++){
                  	for (int j=0;j<2;j++){
                  		cout << constraintpair[i][j] << " ";
                  	}
                  	cout <<endl;
                }
                
                
	return 0;
}

The results so far are

initial array
1 1 
2 8 
2 9 
3 7 
2 5 
1 2 
3 10 
1 6 
2 3 
1 4 

sorted array 
1 1 
1 2 
1 4 
1 6 
2 3 
2 5 
2 8 
2 9 
3 7 
3 10 

currentval
1  2  4  6  3  5  8  9  7  10  

count
0 4 4 2 0

At line number 106 of the code, the debugger goes to stl_vector.h and at

operator[](size_type __n)
      { return *(this->_M_impl._M_start + __n); }

issues a terminate command.

I have sat with this for a while now and since I could not figure it out, decided to ask the forum. What am I doing wrong here?
Please advice.

quite a bit of code there but from what i gathered shouldnt s be an array ?

hi sandhya

wat does currentval,count mean in ur program

i hav a similar assaingment to,could use a little help and write my own program

then I need to pair every element of the 2nd column like this.
C++ Syntax (Toggle Plain Text)

1.
{(1,3),(1,5),(1,8),(1,9),(1,7),(1,10),(2,3).....}

{(1,3),(1,5),(1,8),(1,9),(1,7),(1,10),(2,3).....}
ie pairs are formed between elements only if their elements in the 1st row are different.

Maybe you should explain this again. For example, why is (1,2) not a valid pair?

Member Avatar for iamthwee

I second the above. The OP's example doesn't add up.

Clear up your explanation first.

Hi,

Sorry for messing up the explanation. Given the array

1 1 
1 2 
1 4 
1 6 
2 3 
2 5 
2 8 
2 9 
3 7 
3 10

valid pairs are those whose elements in the 1st 'column' are different. (and not 'row' as I had mentioned). Valid pairs are therefore

{(1,3),(1,5),(1,8),(1,9),(1,7),(1,10),(2,3).....}

since if we look at the 2nd column, we have 1,2,4 and 6 all having the same element in the 1st column ie '1'. So they do not pair up. Similarly, (3,5,8,9) pair up with (1,2,4,6,7,10). 'Currentval' was just to extract the elements of the 2nd column although I did not use it later on ! 'Count' is to see how many elements of the 2nd column have the same element in the 1st column. So for example '1' in the 1st column has 4 elements in 2nd column, therefore count[1]=4 (count[2]=4 and count[3]=2). I used this so that I could loop using this count value. Hope this explanation is clearer now.

Now I get it.
I'd do it this way, seems more simple to me:

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

typedef pair<int,int> int_pair;
typedef unsigned int uint;

int main()
{
  vector<int_pair> orig;
  orig.push_back(int_pair(1,1));
  orig.push_back(int_pair(2,8));
  orig.push_back(int_pair(2,9));
  orig.push_back(int_pair(3,7));
  orig.push_back(int_pair(2,5));
  orig.push_back(int_pair(1,2));
  orig.push_back(int_pair(3,10));
  orig.push_back(int_pair(1,6));
  orig.push_back(int_pair(2,3));
  orig.push_back(int_pair(1,4));
  sort(orig.begin(),orig.end());

  vector<int_pair> pairs;
  for (uint i=0;i<orig.size();i++)
  {
    for (uint j=0;j<orig.size();j++)
    {
      if (orig[i].first==orig[j].first)continue;
      pairs.push_back(int_pair(orig[i].second,orig[j].second));
    }
  }

  for (uint i=0;i<pairs.size();i++)cout << '(' << pairs[i].first << ',' << pairs[i].second << ')' << endl;
}
Member Avatar for iamthwee

[deleted]

Yes Aranarth, your method is simpler and better :) Now I see how to use typedef as well. Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.