Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstring>
- using namespace std;
- /*
- void freeMatrix(float** ptr){
- for(auto element : ptr){
- free(ptr[element]);
- }
- free(ptr);
- }
- */
- float** parseFile(string fileName){
- // Pull adjacency matrix & variable prep
- ifstream reader;
- string line;
- char* temp;
- reader.open(fileName, ios::in);
- if(!reader.is_open()){
- cerr << "Error opening adjacency file! Come back later." << endl;
- }
- // Read number of points - matrix is numPoints x numPoints
- getline(reader, line);
- int numPoints = stoi(line);
- cout << numPoints << endl;
- // Parse rest of matrix
- float** matrix;
- matrix = new float*[numPoints];
- float vals[numPoints];
- // Run the rows of the matrix
- for(int i = 0; i < numPoints; i++){
- matrix[i] = new float[numPoints];
- getline(reader, line);
- char buf[line.size() +1];
- strcpy(buf, line.c_str());
- temp = strtok(buf, " ");
- int q = 0;
- // Convert all values into floats
- while(temp){
- vals[q] = stof(temp);
- temp = strtok(NULL, " ");
- q++;
- }
- //Place floats into appropriate cols
- for(int j = 0; j < numPoints; j++){
- matrix[i][j] = vals[j];
- }
- }
- //Debug prints
- cout << "MATRIX IS:" << endl;
- for(int i = 0; i < 5; i++){
- for (int j = 0; j < 5; j++){
- cout << matrix[i][j] << " ";
- }
- cout << "\n";
- }
- reader.close();
- return matrix;
- }
- int main(int argc, char* argv[]){
- // Verify filename commandline parameter present
- if(argc != 2){
- cout << "Incorrect arguments!" << endl;
- cout << "Usage: ./tsp <filename> " << endl;
- cout << "<filename> is a file containing an adjacency matrix of all points." << endl;
- }
- //Get dat matrix money
- string fileName = argv[1];
- float** matrix = parseFile(fileName);
- // Do the algorithm
- //freeMatrix(matrix);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement