Thread: Txt file and data storage

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

  3. #3
    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