help creating function

Hi guys, i have a piece of code which gets multiple objects to move randomly at a designated number of intervals. I want to create a function which calculates the distance between each pair of points after that number of steps. so the distance between object 1 and object 2, object 1 and object 3, ..., object 1 and object N. then object 2 and object 3, object 2 and object 4, ..., object 2 and object N. then then object 3 and object 4, object 3 and object 5, ..., object 3 and object N and so on until the distance between all the pairs of points have been calculated. Can anyone help with this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <ctime>
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
#include<vector>

using namespace std;

double dist(int a, int b);
int X(int x1, int x2);
int Y(int y1, int y1);

int main(){

int n, N;
vector<int> randx(1000); 
vector<int> randy(1000);
vector<int> x(1000,0);
vector<int> y(1000,0);
vector<int> u(1000,0); //up
vector<int> d(1000,0); // down
vector<int> l(1000,0); // left
vector<int> r(1000,0); // right
vector<int> z(1000,0); // distance

ofstream out;

out.open ("rwalk.txt");

cout<< "How many steps would you like to take? " ;
cin >> n ;
cout<< "How many objects would you like in your system? (max 1000) " ;
cin >> N ;


  
srand(time(0));
  

  for (int i=1 ; i<=n ; i++){   // # of steps
  
  cout<< "Step # "<< i << " " << "x" << " " << "y" << endl;
   out<< "Step # "<< i << " " << "x" << " " << "y" << endl;
   
  for (int j=0 ; j<=(N-1) ; j++){  // # of objects
 
  int ran=rand() % 2; // chooses whether to move on x or y axis
         
if (ran==0){

     randx.at(j)=rand() %2;
	 randy.at(j)=0;
	 
	   if(randx.at(j)==0){
	   
	       randx.at(j)=randx.at(j)-1;  
		   l.at(j)=l.at(j)+1;        // moves left
		   
	    }else if (randx.at(j)==1){
		
		   r.at(j)=r.at(j)+1;        // moves right
		}
}
else if (ran==1){
	
	randy.at(j)=rand() %2;
	randx.at(j)=0;
	 
	 if (randy.at(j)==0){
	 
	      randy.at(j)=randy.at(j)-1;
	 	  d.at(j)=d.at(j)+1;           // moves down
		  
     }else if(randy.at(j)==1){
	 
	      u.at(j)=u.at(j)+1;          // moves up
     }
	 
	 
	 
}
     x.at(j)=x.at(j)+randx.at(j);  // total x moved
     y.at(j)=y.at(j)+randy.at(j);  // total y moved

cout<< x.at(j) << " " << y.at(j) << " "  << d.at(j) << " " << u.at(j) << " " <<
	   l.at(j) << " " << r.at(j) << " "  << r.at(j)+l.at(j)+d.at(j)+u.at(j) <<  endl ;
	 
 out<< x.at(j) << " " << y.at(j) << " "  << d.at(j) << " " << u.at(j) << " " << 
       l.at(j) << " " << r.at(j) << " "  << r.at(j)+l.at(j)+d.at(j)+u.at(j) <<  endl ;
		
}
cout<<"-----------" << endl ;
 out<<"-----------" << endl ;

}
out.close();
}  	 


double dist(int a, int b){     // calculates distance (pythagoras)

double c=sqrt(a*a + b*b);

return c; }


int X(int x1, int x2){      // difference in x values between pair of points

int x= x1 - x2 ;

return x; }

int Y(int y1, int y2){     // difference in y values between pair of points

int y= y1 - y2;

return y; }
You can just use two nested for loops:

1
2
3
4
5
for(int i=0; i<your_objects.size()-1; ++i) {
    for(int j=i+1; j<your_objects.size(); ++j) {
        /* here you calculate the distance */
    }
}
i have written a basic piece of code that calculates the distance between all pairs of points on a line using your suggestion but i just get repeated answers for the difference between the arrays with the same k value, i want the difference between the arrays for all k values. any suggestions?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;


int main(){

vector<int> xa(5,0),xb(5,0); 
vector<int>	   xt(25,0);

for (int k=0 ; k<=4 ; k++){

cin>> xa[k] >> xb[k] ;
}
for(int i=0; i<xb.size()-1; ++i) {
 
    for(int j=i+1; j<=xb.size(); ++j) {
	
	xt[j]=xa[j]-xb[j];
	
	cout<< xt[j] << " "  ;
			
    }

}
}
Last edited on
xt[j] is wrong: j starts from 1... I suggest keeping a variable named "filled" (or something similar) that you use to keep track of what cell you have to write to. Something like

1
2
3
4
5
6
7
8
9
10
11
12
int filled = 0;
for(int i=0; i<xb.size()-1; ++i) {
 
    for(int j=i+1; j<=xb.size(); ++j) {
	
	xt[filled]=xa[j]-xb[j];
	
	cout<< xt[filled] << " "  ;
        filled++;			
    }

}
Last edited on
thanks for the help, the j was wrong in more ways than one, i also had to change the j in xa to an i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main(){

vector<int> xa(5,0), xb(5,0), ya(5,0), yb(5,0); 
vector<int>	xt(25,0), yt(25,0);
int k=0;
xa[0]=-3,ya[0]=10;
xa[1]=2,ya[1]=8;
xa[2]=3,ya[2]=1;
xa[3]=-2,ya[3]=-2;
xa[4]=1,ya[4]=-7;


for(int i=0 ; i<xa.size() ; ++i) {
 
    for(int j=0 ; j<xa.size() ; ++j) {
	
	xt[k]=xa[i]-xa[j];
	yt[k]=ya[i]-ya[j];
	
	cout<< xt[k] << " " << yt[k] << endl ;
	k++;		
    }

}
}
Topic archived. No new replies allowed.