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

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <stdlib.h>
using namespace std;
double Length (double x[101][4]){
double L;
int i,n;
for(i=1;i<=n;i++){
L=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
}
return(L);
}
int main()
{
int a,f,g,n,m,i,j,k,r,s;
double p,q,Energy,energy,y[101][4],x[101][4],L,Distance;
clock_t t1,t2;
t1=clock();
/* set the number of points */
n=10;
/* 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;
}
L=Length (x[i][1],x[i][2],x[i][3]);
for(i=1;i<=n;i++){
for (k=1;k<=3;k++){
x[i][k]=x[i][k]/L;
}
}
/* 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];
}
t2=clock();
float diff ((float)t2(float)t1);
float seconds = diff / CLOCKS_PER_SEC;
cout << fixed << setprecision(2) << "Run time: " << seconds << "(s)" << "\n";
return 0;
}
