Thread: Txt file and data storage

  1. #46
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If i'm right the repeating of numbers occurs because i never clear my pairs vector so those old coordinates remain in the vector. How can i solve this?
    The easiest way that I can think of? Declare pairs at the beginning of the while loop, so that a new variable is constructed for each iteration of the loop.

    I just noticed that you posted your input file a while back. I'm testing your program now . . . .

    [edit] By doing that, and by changing the reference to pairs after the while loop, your program works correctly, at least I think it does.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <sstream>
    using namespace std;
    
    class Coord {           //values of coordinates (x,y)
    
            public:
                    double xt;
                    double yt;
                    
                    Coord(){};
                    Coord(double x, double y) : xt(x), yt(y) {}; 
                    
            };
            
    typedef vector<Coord>Dots;              //collection of coordinates for one path (one line in txt file is one path)
    typedef vector<Dots>Paths;              //collection of all paths
    
    int main(int argc, char* argv[])
    {
            
            Paths trajectories;
            
            int i,j;
            
            if (argc == 2) {        
            
            ifstream dat (argv[1]);
            if (!dat) {
                    cerr << "Given file cannot be opened "<< argv[1]<<endl;
            }
            
            while (!dat.eof()) {
                    Dots pairs;
                    string tmp;
                    double x,y;
                    
                    getline(dat, tmp);
                    stringstream stmp(tmp);
    
                    int p=-1;
                    
                    while (stmp >> x >> y) {
                                    
                                    p++;
                                    cout <<"counter pp="<<p<<"\n"<<"number x       "<<x<<" number y        "<<y<<endl;
                            
                                    pairs.push_back(Coord(x,y));
    
                                    cout <<"number x inside vector  "<<pairs[p].xt<<"      number y inside vector  "<<pairs[p].yt<<endl;
                                    // the lines for printing are just for testing because i had some problems here
                    }
                    
                    trajectories.push_back(pairs);  
            }       
    }       
    else if (argc < 2) {
                    cout << "Error while running program" <<endl;
                    cout << "Usage: eg_decomposition <file_name>" <<endl;
            }
    else {
                    cerr << "Too much parameters" << endl;
                    exit (1);
            }       
                    
    // printing of all paths                
    for(i=0; i < trajectories.size(); i++) {
            for (j=0; j < trajectories[i].size(); j++)
                    cout << '\t' << trajectories[i][j].xt <<' '<<trajectories[i][j].yt << endl; 
                                    
            }
            
    return 0;
    }
    You'll notice I removed pp. It's the same thing as p, after all. [/edit]
    Last edited by dwks; 06-23-2008 at 09:25 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #47
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's the code from my previous post modified slightly. It does exactly the same thing. Consider my changes suggestions.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <sstream>
    using namespace std;
    
    class Coord {           //values of coordinates (x,y)
    public:
            double xt;
            double yt;
            
            Coord(){};
            Coord(double x, double y) : xt(x), yt(y) {}; 
            
    };
    
    typedef vector<Coord>Dots;              //collection of coordinates for one path (one line in txt file is one path)
    typedef vector<Dots>Paths;              //collection of all paths
    
    int main(int argc, char* argv[])
    {
            Paths trajectories;
            
            if (argc == 2) {
                    ifstream dat (argv[1]);
                    if (!dat) {
                            cerr << "Given file cannot be opened "<< argv[1]<<endl;
                    }
            
                    string line;
                    while(getline(dat, line)) {
                            stringstream stmp(line);
                            Dots pairs;
                            double x,y;
                            
                            while (stmp >> x >> y) {
                                    pairs.push_back(Coord(x,y));
                            }
                            
                            trajectories.push_back(pairs);
                    }
            }
            else if (argc < 2) {
                    cout << "Error while running program" <<endl;
                    cout << "Usage: eg_decomposition <file_name>" <<endl;
            }
            else {
                    cerr << "Too much parameters" << endl;
                    exit (1);
            }
            
            // printing of all paths
            for(Paths::size_type i=0; i < trajectories.size(); i++) {
                    for (Dots::size_type j=0; j < trajectories[i].size(); j++)
                            cout << '\t' << trajectories[i][j].xt <<' '<<trajectories[i][j].yt << endl;
                    
            }
            
            return 0;
    }
    A brief overview of my changes:
    • I indented your code a little bit more consistently, trying to stick with your indentation style.
    • I removed the p variable, because you don't need it when you're just printing stuff. (Besides, it's the same thing as pairs.size().)
    • I used size_types for the loop control variables because otherwise you have a signed/unsigned type mismatch. Basically, vector::size() returns an unsigned integer, and int is a signed integer; they have different ranges, and this could cause problems if you had [i]extremely[i] large vectors. You could also just cast the .size() calls to (int), or ignore this altogether.
    • I removed the while(!dat.eof()) call. That can cause problems; see this: http://faq.cprogramming.com/cgi-bin/...&id=1043284351


    [edit] Oh, and I syntax highlighted the code with codeform. [/edit]
    Last edited by dwks; 06-23-2008 at 10:23 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #48
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Dwks thank you very much, this is great. I have only one more thing to do. Now i must calculate Euclidean distance for all paths in trajectories vector. Sort them and find the shortest path so i can send the robot down that path. I've solved the robot motion part of course but how do you suggest that i do the calculation and finding of the shortest path? Once again thank you very much for all your help.
    By the way, you've done very good job on codeform
    Last edited by radnik; 06-23-2008 at 11:12 PM.

  4. #49
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For the distances, use Pythagoras's theorem.
    Code:
    #include <cmath>
    
    double hypotenuse(double x, double y) {
        return std::sqrt(x*x + y*y);
    }
    (C99 has a hypot() function that does this. C++ may, you might want to try it.)

    I'm not sure which pairs of points you want to examine, but finding the shortest path is pretty easy. It should just be a matter of finding the lowest number, no?

    Let me know if you have trouble with that . . . .

    [edit] Note that you'll have to pass a hypot()-like function with the differences between coordinates, like this:
    Code:
    Coord one(4, 5), two(6, 8);
    double distance = hypotenuse(one.xt - two.xt, one.yt - two.yt);
    [/edit]
    Last edited by dwks; 06-23-2008 at 11:15 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #50
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    You can see the formula i need to use on this link:
    http://en.wikipedia.org/wiki/Euclidean_metric.
    I thought to calculate distance for all paths and store these values in vector of doubles. Then to sort them in some way. But how could i know to which path does the smallest number (shortest distance) belong to. Should i calculate those values for all paths again and compare them with the one that is the shortest?
    I should pass values from the trajectories vector while calculating distances.

  6. #51
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If all you're trying to do is figure out the shortest distance, then you don't need to record every distance and then sort them. You can just calculate one distance; if it's the shortest distance found so far, then record the set of points you used to calculate this distance and the actual distance.

    Something like this.
    Code:
            double value, min_value = distance(trajectories[0]);
            Paths::size_type min_index = 0;
            
            for(Paths::size_type i=1; i < trajectories.size(); i++) {
                    value = distance(trajectories[x]);
                    if(value < min_value) {
                            min_value = value;
                            min_index = i;
                    }
            }
            
            // now trajectories[min_index] has the minimum Euclidean distace,
            // the exact value of which is recorded in min_value.
    You'll have to write the distance() function, of course. That should be pretty easy, though.

    [edit] Just use a for loop to loop through all the items in a Paths. Calculate (x1-x2)**2 for each pair of coordinates, as your Wiki page indicates, sum those values, and return the square root of the final sum . . . . [/edit]

    Best of luck, I'm off.
    Last edited by dwks; 06-23-2008 at 11:33 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #52
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Ok, thanks. I'll try that later today and post you on how it went.

  8. #53
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Ok i have problem again right at the start. I don't know how to pass vector with index (vector[x]) as function argument. I did this:
    Code:
    double distance (Paths path[i]) {
    	double dist, tmp;
    	
    	for (Dots::size_type j=0; j<path[i].size[];j++) { 
    	
    		tmp += pow((path[i][j].xt - path[i][++j].xt),2) + pow((path[i][j].yt - path[i][++j].yt),2);
    	}
    	dist = sqrt(tmp);
    	return dist;
    }
    and during compiling i got error that i and path aren't declared in this scope of course. How can i pass it as argument? And i'm not sure about the ++j in the calculation line. In fact i'm quite sure that it is wrong. How can i access the next coordinate if not in this manner?
    Last edited by radnik; 06-25-2008 at 02:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM