Below are two functions:
When running the following statements in given order:Code:void printGraph(const int* topology, int dimSize) { for(int rowIndex = 0; rowIndex < dimSize; rowIndex++) { for(int columnIndex = 0; columnIndex < dimSize; columnIndex++) { cout << *(topology + rowIndex*dimSize + columnIndex) << "\t"; } cout << endl; } } void printGadagGraphAndEdgesInGADAG(EdgeSet* edgesInGADAG, const int gadagGraph[][MAX_VERTICES], int numberOfNodes) { cout << endl; cout << endl; cout << "GADAG:" << endl; cout << endl; for(int i = 0; i < numberOfNodes; i++) { for(int j = 0; j < numberOfNodes; j++) { cout << gadagGraph[i][j] << "\t"; } cout << endl; } cout << endl; size_t setSize = edgesInGADAG->size(); cout << "EDGES In GADAG:" << endl; cout << endl; for( size_t i = 0; i < setSize; i++) { if( edgesInGADAG->at(i) ) { cout << "Source vertex: " << edgesInGADAG->at(i)->srcVertex->vIndex; cout << " Dest vertex: " << edgesInGADAG->at(i)->dstVertex->vIndex << endl; } } }
The output from printGraph() is as follows:Code:printGraph(&gadagGraph[0][0], numberOfNodes); printGadagGraphAndEdgesInGADAG(&edgesInGADAG, gadagGraph, numberOfNodes);
0 1 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
The output from printGadagGraphAndEdgesInGADAG() is as follows (This is the correct output):
0 1 0 0 0 0
0 0 1 1 0 0
1 0 0 0 0 0
0 1 0 0 1 0
0 0 0 0 0 1
0 0 0 1 0 0
gadagGraph is defined as follows and modified accordingly after its definition:
I could not succeed to create a stand alone application in which printGraph() fails to print the square matrix in a wrong way.Code:int gadagGraph[MAX_VERTICES][MAX_VERTICES] = {0};
For example, the following code works fine:
The problem is with the former code only.Code:#include <iostream> using namespace std; void printGraph(const int* topology, int dimSize); int main() { int gadagGraph[3][3] = {{0, 1, 1}, {1, 1, 0}, {0, 1, 0}}; printGraph(&gadagGraph[0][0], 3); return 0; } void printGraph(const int* topology, int dimSize) { for(int rowIndex = 0; rowIndex < dimSize; rowIndex++) { for(int columnIndex = 0; columnIndex < dimSize; columnIndex++) { cout << *(topology + rowIndex*dimSize + columnIndex) << "\t"; } cout << endl; } }
Thanks.




Reply With Quote