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