Very long source code. Please help me. In this source code, I get an error saying “Array index out of range a1x” and “Array index out of range a1y”. Since cnt1 and cnt3 in this source code have exactly the same conditions, they should be the same number. And a1x [cntt1], which is cnt1 * 3. Since the number of 0 → (cnt3-1) * 3 + 2 is assigned to the array a1x, I don’t think it will exceed the range of the array. Is there any mistake? I’m new to programming, so the basics may be wrong.
{
gStyle->SetMarkerStyle(20);
gStyle->SetMarkerSize(0.7);
double R=1.18*pow(197.0,(1.0/3.0))-0.48;
double a=0.54;
int n=197;
double x11[197];
double y11[197];
double x22[197];
double y22[197];
double cntk[10000];
double ep[10000];
int total=10000;
double R1=1.2*pow(197,1.0/3.0);
double c=sqrt(4.2/3.14);
double c11=sqrt(0.81/3.14);
gRandom->SetSeed();
TGraphErrors *g1=new TGraphErrors;
g1->SetMarkerColor(kBlack);
for(int k=0.0;k<total;){
for(int l=0.0;l<1;){
double b=gRandom->Rndm()*22.0-11.0;
double b5=gRandom->Rndm()*22.0-11.0;
if(b*b+b5*b5<(R1+2.0)*(R1+2.0)){
l+=1;
for(int i=0.0;i<n;){
double x1=gRandom->Rndm()*16.0-8.0;
double x01=x1-b;
double y1=gRandom->Rndm()*16.0-8.0;
double y01=y1-b5;
double z1=gRandom->Rndm()*16.0-8.0;
double r1=sqrt(x1*x1+y1*y1+z1*z1);
double r3=gRandom->Rndm();
double rho1=1.0/(1+exp((r1-R)/a));
if(r3<rho1){
x11[i]=x01;
y11[i]=y01;
i+=1;
}
}
for(int j=0.0;j<n;){
double x2=gRandom->Rndm()*16.0-8.0;
double x02=x2+b;
double y2=gRandom->Rndm()*16.0-8.0;
double y02=y2+b5;
double z2=gRandom->Rndm()*16.0-8.0;
double r2=sqrt(x2*x2+y2*y2+z2*z2);
double r4=gRandom->Rndm();
double rho2=1.0/(1+exp((r2-R)/a));
if(r4<rho2){
x22[j]=x02;
y22[j]=y02;
j+=1;
}
}
int cnt1=0;
for(int i=0.0;i<197;i++){
for(int j=0.0;j<197;j++){
double r1111=(x11[i]-x22[j]);
double r1112=(y11[i]-y22[j]);
double r1113=sqrt(r1111*r1111+r1112*r1112);
if(r1113<c){
cnt1+=1.0;
break;
}
}
}
int cntt1=cnt1*3;
double a1x[cntt1];
double a1y[cntt1];
int cnt3=0.0;
for(int i=0.0;i<197;i++){
for(int j=0.0;j<197;j++){
double r1111=(x11[i]-x22[j]);
double r1112=(y11[i]-y22[j]);
double r1113=sqrt(r1111*r1111+r1112*r1112);
if(r1113<c){
cnt3+=1;
for(int l1=0.0;l1<1;){
double a01x=gRandom->Rndm()*1.752-0.876;
double a01y=gRandom->Rndm()*1.752-0.876;
double a01z=gRandom->Rndm()*1.752-0.876;
double sr11=a01x*a01x+a01y*a01y+a01z*a01z;
if(sqrt(sr11)<0.876){
l1+=1;
int i01=(cnt3-1)*3;
a1x[i01]=a01x;
a1y[i01]=a01y;
for(int l2=0.0;l2<1;){
double b01x=gRandom->Rndm()*1.752-0.876;
double b01y=gRandom->Rndm()*1.752-0.876;
double b01z=gRandom->Rndm()*1.752-0.876;
double sr12=b01x*b01x+b01y*b01y+b01z*b01z;
if(sqrt(sr12)<0.876){
l2+=1;
int i02=(cnt3-1)*3+1;
a1x[i02]=b01x;
a1y[i02]=b01y;
for(int l3=0.0;l3<1;){
double c01x=gRandom->Rndm()*1.752-0.876;
double c01y=gRandom->Rndm()*1.752-0.876;
double c01z=gRandom->Rndm()*1.752-0.876;
double sr13=c01x*c01x+c01y*c01y+c01z*c01z;
if(sqrt(sr13)<0.876){
l3+=1;
int i03=(cnt3-1)*3+2;
a1x[i03]=c01x;
a1y[i03]=c01y;
}
}
}
}
}
}
}
}
}
int cnt2=0;
for(int j=0.0;j<197;j++){
for(int i=0.0;i<197;i++){
double r01111=(x22[j]-x11[i]);
double r01112=(y22[j]-y11[i]);
double r01113=sqrt(r01111*r01111+r01112*r01112);
if(r01113<c){
cnt2+=1.0;
break;
}
}
}
int cntt2=cnt2*3;
int cnt4=0;
double a2x[cntt2];
double a2y[cntt2];
for(int j=0.0;j<197;j++){
for(int i=0.0;i<197;i++){
double r1111=(x22[j]-x11[i]);
double r1112=(y22[j]-y11[i]);
double r1113=sqrt(r1111*r1111+r1112*r1112);
if(r1113<c){
cnt4+=1;
for(int l4=0.0;l4<1;){
double a02x=gRandom->Rndm()*1.752-0.876;
double a02y=gRandom->Rndm()*1.752-0.876;
double a02z=gRandom->Rndm()*1.752-0.876;
double sr21=a02x*a02x+a02y*a02y+a02z*a02z;
if(sqrt(sr11)<0.876){
l4+=1;
int j01=(cnt4-1)*3;
a2x[j01]=a02x;
a2y[j01]=a02y;
for(int l5=0.0;l5<1;){
double b02x=gRandom->Rndm()*1.752-0.876;
double b02y=gRandom->Rndm()*1.752-0.876;
double b02z=gRandom->Rndm()*1.752-0.876;
double sr22=b02x*b02x+b02y*b02y+b02z*b02z;
if(sqrt(sr22)<0.876){
l5+=1;
int j02=(cnt4-1)*3+1;
a2x[j02]=b02x;
a2y[j02]=b02y;
for(int l6=0.0;l6<1;){
double c02x=gRandom->Rndm()*1.752-0.876;
double c02y=gRandom->Rndm()*1.752-0.876;
double c02z=gRandom->Rndm()*1.752-0.876;
double sr23=c02x*c02x+c02y*c02y+c02z*c02z;
if(sqrt(sr23)<0.876){
int j03=(cnt4-1)*3+2;
a2x[j03]=c02x;
a2y[j03]=c02y;
}
}
}
}
}
}
}
}
}
double sumxs1=0.0;
double sumxxs1=0.0;
double sumys1=0.0;
double sumyys1=0.0;
double sumxys1=0.0;
for(int i=0.0;i<cntt1;i++){
for(int j=0.0;j<cntt2;j++){
double sr01=(a1x[i]-a2x[j]);
double sr02=(a1y[i]-a2y[j]);
double sr03=sqrt(sr01*sr01+sr02*sr02);
if(sr03<c11){
double xs11=a1x[i];
sumxs1+=xs11;
double xxs11=(a1x[i])*(a1x[i]);
sumxxs1+=xxs11;
double ys11=a1y[i];
sumys1+=ys11;
double yys11=(a1y[i])*(a1y[i]);
sumyys1+=yys11;
double xys11=(a1x[i])*(a1y[i]);
sumxys1+=xys11;
break;
}
}
}
double sumxs2=0.0;
double sumxxs2=0.0;
double sumys2=0.0;
double sumyys2=0.0;
double sumxys2=0.0;
for(int j=0.0;j<cntt2;j++){
for(int i=0.0;i<cntt1;i++){
double sr04=(a2x[j]-a1x[i]);
double sr05=(a2y[j]-a1y[i]);
double sr06=sqrt(sr04*sr04+sr05*sr05);
if(sr06<c11){
double xs21=a2x[j];
sumxs2+=xs21;
double xxs21=(a2x[j])*(a2x[j]);
sumxxs2+=xxs21;
double ys21=a2y[j];
sumys2+=ys21;
double yys21=(a2y[j])*(a2y[j]);
sumyys2+=yys21;
double xys21=(a2x[j])*(a2y[j]);
sumxys2+=xys21;
break;
}
}
}
if((cnt1+cnt2)>0.0){
cntk[k]=cnt1+cnt2;
double xxmean=(sumxxs1+sumxxs2)/cntt1+cntt2;
double xmean=(sumxs1+sumxs2)/cntt1+cntt2;
double yymean=(sumyys1+sumyys2)/cntt1+cntt2;
double ymean=(sumys1+sumys2)/cntt1+cntt2;
double xymean=(sumxys1+sumxys2)/cntt1+cntt2;
double sigx=xxmean-(xmean)*(xmean);
double sigy=yymean-(ymean)*(ymean);
double sigxy=xymean-(xmean)*(ymean);
double x011=(sigy-sigx)*(sigy-sigx)+4.0*sigxy*sigxy;
double x01=sqrt(x011);
double y01=sigy+sigx;
double ip1=x01/y01;
ep[k]=ip1;
k+=1;
}
}
}
}
double dum1;
double dum2;
for(int k=0.0;k<total;++k){
for(int l=k+1;l<total;++l){
if(cntk[k]>cntk[l]){
dum1=cntk[k];
cntk[k]=cntk[l];
cntk[l]=dum1;
dum2=ep[k];
ep[k]=ep[l];
ep[l]=dum2;
}
}
}
double cntsepa[20];
for(int m=0.0;m<20;m++){
double sumcnt=0.0;
for(int n=500*m;n<500*(m+1);n++){
sumcnt+=cntk[n];
}
cntsepa[m]=sumcnt/500.0;
}
double epsepa[20];
for(int m=0.0;m<20;m++){
double sumep=0.0;
for(int n=500*m;n<500*(m+1);n++){
sumep+=ep[n];
}
epsepa[m]=sumep/500.0;
double sum11=0.0;
for(int o=500*m;o<500*(m+1);o++){
sum11+=(ep[o]-epsepa[m])*(ep[o]-epsepa[m]);
}
double sd=sqrt(sum11/(500.0-1.0)/500.0);
g1->SetPointError(19-m,0,sd);
g1->SetPoint(m,cntsepa[m],epsepa[m]);
}
g1->Draw("AP");
}