Problem with distance output in histogram

I've created a program which gathers the starting point and endpoint of a polymer, this works fine and it retrieves and outputs the points correctly, however, the problem I am having is the distance output, I want the program to use the start point and end point on each line and calculate its distance using the distance formula, which works but only for the first set of points then it copies and pastes the same distance onto the other set of points which is not how it is supposed to be. Any help would be greatly appreciated, Thank you.

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
#include<iostream>
#include<cstdlib>
#include<string>
#include<ctime>
#include<cmath>
#define PI 3.14159265
using namespace std;
int main()
{	
	int angle_of_monomer, how_many_monomers = 0;	
	double rad;		 
	double arrx[4000]={}; 
	double arry[4000]={};
	double angle[4000]={};
	double distance[4000]={};
	int numberofsim= 10;			
				 
	
	
	for(int sim_of_sim = 0; sim_of_sim<numberofsim; sim_of_sim++)
	{
			
		srand(time(0));
		for(int sim = 0; sim<4000; sim++)
		{
			double x,y;
			cout.setf(ios::fixed);
			cout.setf(ios::showpoint);
			cout.precision(2);
			how_many_monomers = how_many_monomers+1;						
			int angle_of_monomer = 359 * rand()/RAND_MAX;
			angle[sim]= (angle_of_monomer * PI)/180;
			angle[sim]= angle[sim]+angle[sim-1];
						
			if(angle[sim]<360)
			{
				angle[sim]=angle[sim]-360;
			}
				
			arrx[sim]=cos(angle[sim]);
			arrx[sim]= arrx[sim]+arrx[sim-1];
			arry[sim]=sin(angle[sim]);
			arry[sim]= arry[sim]+arry[sim-1];
									
			cout.setf(ios::fixed);
			cout.setf(ios::showpoint);
			cout.precision(2);
				 
		}
			
		
	
		 distance[sim_of_sim]= sqrt(((arrx[3999]-arrx[0])*(arrx[3999]-arrx[0]))+((arry[3999]-arry[0])*(arry[3999]-arry[0])));
		
					
	cout<<"start/endpoint "<<"("<<arrx[0]<<", "<<arry[0]<<")"<<'\t'
	<<"("<<arrx[3999]<<", "<<arry[3999]<<")"
	<<"  distance: "<<distance[sim_of_sim]<<endl;
		
			
	}
}
srand() should be called only once per execution. If the loop from line 20 takes less than a second to complete one iteration, on line 23 you're going to seed rand() with the same value every iteration, so the output from rand() is going to be the same on all simulations.
Just move srand() outside all the loops.

By the way, this is unrelated to your question, but I've noticed a bug. The variable sim can take all integer values in the range [0;3999], but on several lines (e.g. line 33) you try to access angle[sim-1], arrx[sim-1] and arry[sim-1]. When sim == 0, these accesses are invalid.

Also, I think on line 37 I think you meant angle[sim]=angle[sim]+360;
Another problem is that that check assumes that angle[sim] is in degrees, even though you made the conversion to radians on line 32.
Last edited on
Thank You Very Much! This completely fixed my program and now it runs the way it should! I appreciate this so much!
Topic archived. No new replies allowed.