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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#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, i, j; // Number of steps to take and objects present respectively
int o=0;
vector<int> randx(1000); // Random variable for x movement
vector<int> randy(1000); // Random variable for y movement
vector<int> x(1000,0); // Overall x position
vector<int> y(1000,0); // Overall x position
vector<int> u(1000,0); // Number of steps taken up
vector<int> d(1000,0); // Number of steps taken down
vector<int> l(1000,0); // Number of steps taken left
vector<int> r(1000,0); // Number of steps taken right
vector<int> xt(1000,0), yt(1000,0) ; // Difference in horizontal and vertical position respectively
vector<double> z(1e6,0);
ofstream step("rwalk.txt");
ofstream distance("distances.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 << endl;
cout<<" x" << " y" << setw(5) << "d" << setw(5) << "u" << setw(5) << "l" << setw(5) << "r" << endl;
cout<<"----------------------------------------------" << endl ;
step<< "Step # "<< i << endl;
step<<" x" << " " << " y" << endl;
step<<"----------------------------------------------" << 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; // Moves on x axis
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; // Moves on y axis
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<< setw(5) << x.at(j) << setw(5) << y.at(j) << setw(5) << d.at(j) << setw(5) << u.at(j) << setw(5) <<
l.at(j) << setw(5) << r.at(j) << endl ;
step<< 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 ;
}
if (i==n){ // Calulates distances between all pairs of points at the end of walk
for(int k=0 ; k<N ; ++k) {
for(int m=0 ; m<N ; ++m) {
if(k != m){
xt[o]=X(x[k],x[m]);
yt[o]=Y(y[k],y[m]);
z[o]=dist(xt[o], yt[o]);
distance << (k+1) << "-" << (m+1) << " " << xt[o] << " " << yt[o] << " " << z[o] << endl ;
o++;
}
}
}
}
cout<<"----------------------------------------------" << endl ;
step<<"----------------------------------------------" << endl ;
}
step.close();
distance.close();
cout<< "Data for the Steps of the walk has been written to 'rwalk.txt'" << endl;
cout<< " " << endl;
cout<< "Data for the distances between all pairs of points has been written to 'distances.txt'" << endl;
}
double dist(int a, int b){ // Calculates distance between pair of points (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; }
|