diff --git a/cs677/hw7/src/floyd.cc b/cs677/hw7/src/floyd.cc index 79ed691..f54d743 100644 --- a/cs677/hw7/src/floyd.cc +++ b/cs677/hw7/src/floyd.cc @@ -6,8 +6,77 @@ */ #include +#include +#include #include +#include +#include +#include +using namespace std; + +void usage(char * progname); +int readFile(char * fileName, vector & v); +void convertToMatrix(const vector & v, int num_verts, int * vals); + +void usage(char * progname) +{ + cout << "Usage: " << progname << " " << endl; + exit(42); +} int main(int argc, char * argv[]) { + if (argc < 1) + usage(argv[0]); + + vector v; + + int num_verts = readFile(argv[1], v); + int D[2][num_verts][num_verts]; + convertToMatrix(v, num_verts, (int *) &D[0]); + + for (int i = 0; i < num_verts; i++) + { + for (int j = 0; j < num_verts; j++) + { + cout << D[0][i][j] << " "; + } + cout << endl; + } + + return 0; +} + +int readFile(char * fileName, vector & v) +{ + ifstream in(fileName); + if (!in.is_open()) + { + cerr << "Error opening " << fileName << endl; + return -1; + } + + for (;;) + { + int weight; + in >> weight; + if (in.eof()) + break; + v.push_back(weight); + } + + return (int) sqrt(v.size()); +} + +void convertToMatrix(const vector & v, int num_verts, int * vals) +{ + int vidx = 0; + int (*V)[num_verts][num_verts] = (int (*)[num_verts][num_verts]) vals; + for (int i = 0; i < num_verts; i++) + { + for (int j = 0; j < num_verts; j++) + { + (*V)[i][j] = v[vidx++]; + } + } }