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.