2 instead of 1

Hi guys, can anyone tell me how get my y[j-1] values to give me 1 or -1 instead of 2 or -2. thanks
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
#include <ctime>
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
#include<vector>
using namespace std;


int main() {

int n;
vector<int> randx(9);
vector<int> randy(9);
vector<int> x(9,0);
vector<int> y(9,0);
vector<int> u(9,0);
vector<int> d(9,0);
vector<int> l(9,0);
vector<int> r(9,0);
vector<int> z(9,0);

ofstream out;
ofstream dis;

out.open ("rwalk.txt");



cout<< "How many steps would you like to take? " ;
cin >> n ;


  
  srand(time(0));
  

  for (int i=1 ; i<=n ; i++){
  
  for (int j=1 ; j<=10 ; j++){
 
  int ran=rand() % 2; // chooses whether to move on x or y axis
         
if (ran==0){

     randx[j-1]=rand() %2;
	 randy[j-1]=0;
	 
	   if(randx[j-1]==0){
	   
	       randx[j-1]=randx[j-1]-1;
		   l[j-1]=l[j-1]+1;
		   
	    }else if (randx[j-1]==1){
		
		   r[j-1]=r[j-1]+1;
		}
}
else if (ran==1){
	
	randy[j-1]=rand() %2;
	randx[j-1]=0;
	 
	 if (randy[j-1]==0){
	 
	      randy[j-1]=randy[j-1]-1;
	 	  d[j-1]=d[j-1]+1;
		  
     }else if(randy[j-1]==1){
	 
	      u[j-1]=u[j-1]+1;
     }
	 
	 
	 x[j-1]=x[j-1]+randx[j-1];
     y[j-1]=y[j-1]+randy[j-1];
     
}
     x[j-1]=x[j-1]+randx[j-1];
     y[j-1]=y[j-1]+randy[j-1]; 
cout<< x[j-1] << " " << y[j-1] << " " << d[j-1] << " " << u[j-1] << " " << l[j-1] << " " << r[j-1] << endl ;		  
}
cout<<"-----------" << endl ;
}
}  	 
First if I may a recommendation for a little bit better readability - change for loops to for(int j=0; j<n; ++j) and then you can change all [j-1] to [j]. This is better when you work with arrays - they always starts with index 0.

Also this part of the code at the end looks like forgoten copy-paste...thing. Remove first two lines.
1
2
3
4
5
6
	 x[j-1]=x[j-1]+randx[j-1];
     y[j-1]=y[j-1]+randy[j-1];
     
}
     x[j-1]=x[j-1]+randx[j-1];
     y[j-1]=y[j-1]+randy[j-1]; 


And if you use out.open ("rwalk.txt"); then you should most probably use out.close (); when you no longer need to write in the file.

Last thing, be careful with second for loop where you have j<=10. This meens that you are working with numbers 1 to 10 (and then [j-1] converts it to 0-9) but you declared your vectors with 9 elements (0-8). So your program is working with memory outside the vector and it may be corrupting some other part of the program (or other program). Or at leas I think it is that way; I am not very familiar with vectors.
Thank you very much, some very useful suggestions. I have the program running properly now.
Also, you should probably use vector's functions as they will generate compile-time run-time errors in case of failure.

So instead of writing x[i] = x[i] + randx[i] you should write x.at(i) = x.at(i) + randx.at(i); at() would throw an exception if you were to try to index an element which is not in the vector's range, while [] would generate undefined behaviour.
Last edited on
> generate compile-time errors in case of failure.
> So instead of writing x[i] = x[i] + randx[i] you should write x.at(i) = x.at(i) + randx.at(i);
The error is not at compile-time, but run-time
.at() would throw an exception, but operator[] would generate undefined behaviour
Last edited on
Ah, thank you for clarification ne555. I'll edit my post.
ah thank you, i didnt even know .at existed... im a newbie lol
Topic archived. No new replies allowed.