Getting all pairs of numbers in a 2D array c++

1.1k Views Asked by At

I am needing to print out all pairs of numbers from each line read in from a text document. A sample text document would be:

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

Where the first line is the number of nets (6) and the number of cells (8) for a hypergraph. The rest of the lines are the cells that are in the net. So net 1 consists of cells 1, 3, and 5, net 2 consists of cells 2, 3, and 4 and so on. In order to turn this netlist into an actual graph I need to go through each line and basically take all combinations of the numbers on each line. So after reading in the first net I would like to be able to make a graph with (1,3), (1,5), and (3,5) and then go down the netlist and add to the graph. So far I am able to read everything in from the text file and print out the individual cells that I put into a 2D array. Here is my code for that:

    int main() {

ifstream infileHGR; // set stream for hypergraph text file
string inputFileName = "structP.hgr"; // input hypergraph filename here
infileHGR.open(inputFileName, ios::in);

clock_t start = clock(); // start clock

string line;
string data[2]; // initialize data array to take in # of nets and # of cells
int nets = 0;
int cells = 0;

// Reads in the first line of the text file to get # for nets and cells
    getline(infileHGR, line);
    stringstream ssin(line);
    int i = 0;

    while (ssin.good() && i < 2) { // error checking to make sure first line is correct format
    ssin >> data[i];
    i++;
    }
    nets = atoi(data[0].c_str()); // set first number to number of nets 
    cells = atoi(data[1].c_str()); // set second number to number of cells

    freopen("output.txt", "w", stdout); // writes outptut to text file

// TESTING PURPOSES
cout << "Number of nets = " << nets << endl;
cout << "Number of cells = " << cells << endl;

// while loop to go through rest of the hgr file to make hypergraph (starts at line 2)
string str;
int count = 1; // counter for nets

while (infileHGR.good()) {
    getline(infileHGR, str);
    stringstream in(str);
    int i = 0;
    // have the line in str

    int n = 1; // start at 1, spaces + 1 = number of nodes per net
    for (int i = 0; i < str.length(); ++i) {
        if (str.at(i) == ' ') {
            n++; // n is number of cells in the net
        }
    }
    // testing
    //cout << "str = " << str << endl;
    //cout << "n = " << n << endl;

    int number;

    vector<vector<int> > netList;
    vector<int> temp;
    while (in >> number){
        temp.push_back(number);
    }
    netList.push_back(temp);
    //printNetList(temp); // test to see if info is being put into the vectors

    // loop through the 2d vector
    for (const auto& inner : netList) {

        cout << "net " << count << " = "; //TESTING PURPOSES
        for (const auto& item : inner) {
            cout << item << " ";
        }
        count = count + 1;
    }
    cout << endl;
} 
clock_t stop = clock(); // end clock
infileHGR.close();
double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
printf("Time elapsed in ms: %f", elapsed);
system("pause"); //for original testing
return 0;

}

I used vectors because every input file will be of different size, and some contain a lot of nets, and some nets have up to 20 cells in them. I need help with getting all pairs (coordinates) from the netlist and printing them out to show all of them. I have messed around with the for loops a lot but can't seem to get something that works. Any help would be greatly appreciated, and just ask if I need to include anything else. Thank you!

3

There are 3 best solutions below

1
On

Looking at your example,

for each line 
    for i = 1 to N-1th element
        for j = i+1 to Nth element
            print (i,j)
0
On

I'll post my answer here because I was able to figure it out thanks to some suggestions. Also thank you for all the feedback on the rest of the code, I have definitely made it better since the original post. My for loop though for looping through the 2D vector and printing out all of the pairs is this (you can just ignore the weight variable being output):

    for (vector<vector<int>> ::iterator i = netList.begin(); i != netList.end(); ++i) {
        for (vector<int> ::iterator j = temp.begin(); j != temp.end() - 1; ++j) {
            for (auto k = temp.begin() + 1; k != temp.end(); ++k) {
                if (*j != *k) {
                    cout << *j << " " << *k << " " << weight << endl;
                }
            }
        }
    }
4
On

If you're looking to print out all of the possible indexes from an array. You are looking in the right direction. You can do it with for loops, and I actually had this issue a bit back for an assignment. Take a look at this, it should return all possible indexes:

int b, c = 0;
int userIndex_A;
int userIndex_B;

cin >> userIndex_A;
cin >> userIndex_B;

 //This is so you can have variable array lengths

int Arr[userIndex_A][userIndex_B];

for(int a = 0; c < 10; a++){
        //The reason I put c in the for loop, to tell it when to stop, is because when c is equal to 10, that's the last index being paired
            cout << "Array Index: "<< b << ", "<< c << endl;
        if(b == (userIndex_A - 1)){
        //userIndex-A is the array length, but 10 doesn't exist, so subtract 1
            b = 0;
            c++;
        //if b is equal to max index value entered, set it to zero and add one to c.  You only add one to c when you want to start with the next set of index.
        }
        b++;    
        //after each loop, increment the variables
    }

This also works for 3D and 4D arrays too, just add more variables to increment each loop and set up for loops to reset the variable once it reachs the respective max array index length.