I made a little program in which i read double values from file and store them in vector of vectors. So i have one vector pairs which is collection of coordinates (numbers from one line in txt file) and vector trajectories which is vector of vectors pairs (collection of all paths). Now i need to calculate Euclidean distance (http://en.wikipedia.org/wiki/Euclidean_distance) for all paths and find the shortest path. I'm not too familiar with c++ so i don't know how to pass vector with index (vector[x]) as function argument. I meant to do something like this:

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;

but while compiling i get that i and path isn't declared in this scope. So my first question is how can i pass this to function as argument. The second question is how can i solve the situation with j counter in the calculation line (i'm sure that i did something wrong with it) because i'm increasing it's value once in the for condition and twice in the calculation line. Thank you all in advance.

Member Avatar for jencas

You can't pass an array to a function this way. Replace all "path" by "path" in your function.
Call your function with path as parameter:


double d = distance(path[i]);

By the way: size[] should be size() if Paths is a STL container. A normal C++ array has no size() operator.

No that is the problem, all paths don't have the same length. Here is the whole code, maybe it will help someone understand the situation:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <cmath>
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

//double distance(Paths);

/*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;
}*/

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;
}

if you want to pass a 'Paths' type to 'distance' you simply have to call like this

distance(trajectories)

since trajectories is of type 'Paths', in the definition you will say

double distance(Paths trajectory){}
Member Avatar for jencas

From Wikipedia: "The Euclidean distance between points P=(p_1,p_2,\dots,p_n)\, and Q=(q_1,q_2,\dots,q_n)\,"

so why don't you rewrite your function to take two Paths (P and Q) as parameters:

double distance (Paths p, Paths q) {
  ....
}

Then do a double for loop to calculate the distances between all pathes and do whatver you want with these results.

Will in that case i be able to call function like this?:

for(Paths::size_type i=1; i < trajectories.size(); i++) {
                value = distance(trajectories[x]);
                if(value < min_value) {
                        min_value = value;
                        min_index = i;
                }
        }

If i pass two values i'll still have to do it with some kind of counter because all my paths are stored in vector trajectories. How do you suggest me to do this?

Member Avatar for jencas

Something like

for(Paths::size_type i=1; i < trajectories.size(); i++) {
 for(Paths::size_type j=i+1; j < trajectories.size(); j++) {
  value = distance(trajectories[i], trajectories[j]);
  // do anything you want with value
 }
}

value = distance(trajectories[x]);

This is incorrect. trajectories[x] is not of type 'Paths' its of type 'Dots'. Remember when you say [x] you are passing the element at that location and the types are different. If you want to pass trajectories[x] then in the defintion you need to take them in the same type something like 'distance(Dots d)'

Thank you very much jenkas and Agni. I'll try to do this later today and post on how it went.

Agni how will i write the function body now? Because in the body i need to have defined current index of trajectory (to know for which path i am calculating the distance value). Like in these lines (the red highlighted parts):

for (Dots::size_type i=0; i<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);
	}

Jencas that what you wrote isn't what i need. I have path with several (unknown number) coordinates (x,y). I need to calculate the Euclidean distance for every path. So i need to calculate euclidean distance between every two coordinates inside one path and sum the all together to get the total distance for current path.

dude for that i'll have to understand the equation and i dont want to tell you something that gives a wrong result. what i was trying to explain to you is how to pass a vector to a function or a vector of vector to a function. You might have to go through that and see how you can modify your code.

When i wrote this:

double distance (Dots d) {
	double dist, tmp;
	
	for (Dots::size_type i=0; i < d.size[];i++) {  //this is line 49 for which i get error 
	
		tmp += pow((d[i].xt - d[++i].xt),2) + pow((d[i].yt - d[++i].yt),2);
	}
	dist = sqrt(tmp);
	return dist;
}

i got this compiler error:
error: expected primary-expression before ']' token

d.size[] should be d.size()

Thanks. I didn't even notice that.

Member Avatar for jencas

d.size[] should be d.size()

Wrote that in my first post!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.