### Possibly misunderstanding exit(0) statement.

I have written my code to run a simulation of evenly distributing n points on a sphere. I have put a failsafe in place to end the code if there are any issues. My code is as follows:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259`` ``````#include #include #include #include #include #include #include #include using namespace std; int main() { int a,f,i,j,k,m,n,s; double p,Energy,energy,Distance,Length,DotProdForce,Forcemagnitude,ForceMagnitude[101], Force[101][4],E[10001],x[101][4],y[101][4]; clock_t t1,t2; t1=clock(); /* Set the number of points */ n=25; /* check that there are no more than 100 points */ if(n>100){ cout << n << " is too many points for me :-( \n"; exit(0); } /* Reset the random number generator */ srand((unsigned)time(0)); for (i=1;i<=n;i++){ x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0; x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0; x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0; Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2)); for (k=1;k<=3;k++){ x[i][k]=x[i][k]/Length; } } /* Calculate the energy */ Energy=0.0; for(i=1;i<=n;i++){ for(j=i+1;j<=n;j++){ Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2) +pow(x[i][3]-x[j][3],2)); Energy=Energy+1.0/Distance; } } /* Save Original Points */ for(i=1;i<=n;i++){ y[i][1]=x[i][1]; y[i][2]=x[i][2]; y[i][3]=x[i][3]; } /* Loop for random points m times*/ m=10; if (m>100){ cout << "The m="<< m << " loop is inefficient...lessen m \n"; exit(0); } a=1; while(a0.00001){ /* Reset initial force and energy change */ for(i=1;i<=n;i++){ Force[i][1]=0.0; Force[i][2]=0.0; Force[i][3]=0.0; } /* Calculate force on each particle */ for(i=1;i<=n;i++){ for(j=1;j

My failsafe is at line 213 and I want it to basically exit the code if energy I am calculating stops decreasing. It does this without fail, but my issue is that my outputs are confusing me.

A standard output gives me something like:

Energy=269.7821451992
energy=266.7549548756
E=266.7549548756
Successes=1 Failures=1
Terminated

As you can see, the Energy is higher than the energy and so this is a success, hence 1 success. However where has the failure come from?

The code states (I think) that on lines 204/205 I should get an output of my Energy and energy and then increment f if there is a failure. Since I only have one set of Energy and energy outputs, surely my successes and failures should add up to one. The clearly add to two and so therefore the while loop has run twice. Why is it only giving my one set of outputs?

Thanks for any assistance you can offer een if it is just links or extra reading. The only reason I can think of is either I have misunderstood exit(0) and so the code is exiting before I think it is, or I have misunderstood the order in which my code is being read and therefore expect more outputs than there should be.

Note: My code does get it right sometimes, but only if there is a fail on the first go. Then I get this output:

Energy=275.8325665982
energy=313.1149926643
E=275.8325665982
Successes=0 Failures=1
Terminated

(Obviously the Energy and energy values differ every time you run the code, but that is an example).

Thanks,

A.
 My failsafe is at line 213

You might want to use the comparison operator (==) as opposed to the assignment operator in that if condition.
Excellent, such a stupid mistake. Thanks for that!
Topic archived. No new replies allowed.