Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. /*
  9. void freeMatrix(float** ptr){
  10.     for(auto element : ptr){
  11.         free(ptr[element]);
  12.     }
  13.     free(ptr);
  14. }
  15. */
  16.  
  17.  
  18. float** parseFile(string fileName){
  19.     // Pull adjacency matrix & variable prep
  20.     ifstream reader;
  21.     string line;
  22.     char* temp;
  23.     reader.open(fileName, ios::in);
  24.    
  25.     if(!reader.is_open()){
  26.         cerr << "Error opening adjacency file! Come back later." << endl;
  27.     }
  28.    
  29.     // Read number of points - matrix is numPoints x numPoints
  30.     getline(reader, line);
  31.     int numPoints = stoi(line);
  32.     cout << numPoints << endl; 
  33.  
  34.     // Parse rest of matrix
  35.     float** matrix;
  36.     matrix = new float*[numPoints];
  37.     float vals[numPoints];
  38.    
  39.     // Run the rows of the matrix
  40.     for(int i = 0; i < numPoints; i++){
  41.         matrix[i] = new float[numPoints];
  42.         getline(reader, line);
  43.         char buf[line.size() +1];
  44.         strcpy(buf, line.c_str());
  45.         temp = strtok(buf, " ");
  46.         int q = 0;
  47.        
  48.         // Convert all values into floats
  49.         while(temp){
  50.             vals[q] = stof(temp);
  51.             temp = strtok(NULL, " ");
  52.             q++;
  53.         }
  54.        
  55.         //Place floats into appropriate cols
  56.         for(int j = 0; j < numPoints; j++){
  57.                 matrix[i][j] = vals[j];
  58.         }
  59.     }
  60.    
  61.     //Debug prints
  62.     cout << "MATRIX IS:" << endl;
  63.     for(int i = 0; i < 5; i++){
  64.         for (int j = 0; j < 5; j++){
  65.             cout << matrix[i][j] << " ";
  66.         }
  67.         cout << "\n";
  68.     }
  69.    
  70.     reader.close();
  71.     return matrix;
  72. }
  73.  
  74. int main(int argc, char* argv[]){
  75.     // Verify filename commandline parameter present
  76.     if(argc != 2){
  77.             cout << "Incorrect arguments!" << endl;
  78.             cout << "Usage: ./tsp <filename> " << endl;
  79.             cout << "<filename> is a file containing an adjacency matrix of all points." << endl;
  80.     }
  81.    
  82.     //Get dat matrix money
  83.     string fileName = argv[1];
  84.     float** matrix = parseFile(fileName);
  85.    
  86.     // Do the algorithm
  87.    
  88.     //freeMatrix(matrix);
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement