Don't know why my code crashes when j=317. I thought was the matrix dimensions, so tried some numbers. Nothing particular happened.
Every help would be very usefull.

``````    #include <stdio.h>
#include <math.h>

int main(){
int T,i,j,t,c,r[T],Jmax,Z,k;
float v[10],R[10],EBNo[10],trapserv[10],a[10],w[10],Pblock[10];
float L[j+1][j],B[T+1][j+1],b[T+1][1000],P[T+1][j+1],q[j+1],qnorm[j+1];
float sigma,mtimi,mtimic,CV;
float Nmax,g,W,EIother,C,Nown,No,totofcell,sumq;
float Gamma,bita,QA,LA,BA;
float x;
//Data input//
`    T=3;`
v[1]=0.45;v[2]=0.3;v[3]=0.8;
R[1]=12.2;R[2]=64.0;R[3]=144.0;
trapserv[1]=0.5;trapserv[2]=0.25;trapserv[3]=0.25;
EBNo[1]=5.0;EBNo[2]=4.0;EBNo[3]=3.0;
Nmax=0.8;g=0.005;
totofcell=0.1;W=3.84;CV=1.0;
EIother=2.0;No=3.98;
//Processing section//
//Calculate c, w[i], r[i] and a[i]//
C=Nmax/g;
for (i=1;i<T+1;i++){
w[i]=(pow(10,EBNo[i]/10)*R[i])/(W*1000+pow(10,EBNo[i]/10)*R[i]);
r[i]=floor((w[i]/g)+0.5);
a[i]=(0.1*trapserv[i])/(v[i]*w[i]);
printf("w[%d]= %f\n",i,w[i]);
printf("r[%d]= %f\n",i,floor(r[i]));
printf("a[%d]= %f\n",i,a[i]);
}
//Initialize b, B and q//
L[0][0]=1;
q[0]=1;
sigma=sqrt(log(1+pow(CV,2)));
mtimi=log(EIother*pow(10,-18))-0.5*log(1+pow(CV,2));
mtimic=mtimi+log(1-Nmax)-log(No*pow(10,-18));
for (t=1;t<T+1;t++){
x=0;
x=Nmax-w[t];
if (x>0){
Gamma=0;
Gamma=0.5+0.5*erf((log(x)-mtimic)/(sigma*sqrt(2)));
b[t][0]=1-Gamma;
}
else b[t][0]=1.0;
B[t][0]=b[t][0]; printf("b[%d][0]= %f\n",t,b[t][0]);
}
//Algorithm for b[t][j] calculation//
for (t=1;t<T+1;t++){
for (k=0;k<180;k++){
x=0;
x=Nmax-k*g-w[t];
if (x>0){
Gamma=0.0;
Gamma=0.5+0.5*erf((log(x)-mtimic)/(sigma*sqrt(2)));
b[t][k]=1.0-Gamma;
}
else b[t][k]=1;
B[t][k]=b[t][k];
}
}
//Basic algorithm for B[t][j], P[t][j], L[c][j], q[j] calculation//
j=0;
sumq=0;
while(B[1][j]<=0.999){
j=j+1;
QA=0;
for (t=1;t<T+1;t++){   //q[j] calculation//
r[t]=floor((w[t]/g)+0.5);
if (j-r[t]>=0) QA=QA+q[j-r[t]]*(1.0-B[t][j-r[t]])*a[t]*r[t];
else QA=QA+0;
}
q[j]=QA/j;
sumq=sumq+q[j];
for (t=1;t<T+1;t++){   //P[t][j] calculation//
if ((j-r[t]>=0)&&(q[j]>0)) P[t][j]=(q[j-r[t]]*(1.0-B[t][j-r[t]])*a[t]*r[t]) / (j*q[j]);
else P[t][j]=0;
}

for (c=0;c<j+1;c++){   //L[c][j] calculation//
LA=0;
for (t=1;t<T+1;t++){
if (c>t) L[c][t]=0;
if (j-r[t]>=0){
if (c-r[t]>=0) LA=LA+P[t][j]*(v[t]*L[c-r[t]][j-r[t]]+(1-v[t])*L[c][j-r[t]]);
else LA=LA+P[t][j]*((1-v[t])*L[c][j-r[t]]);
}
else LA=LA+0;
}
L[c][j]=LA; printf("L[%d][%d]= %f\n",c,j,L[c][j]);
}

for (t=1;t<T+1;t++){   //B[t][j] calculation//
BA=0;
for (c=0;c<j+1;c++){
BA=BA+L[c][j]*b[t][c];
}
B[t][j]=BA;
}
Jmax=j;
}

printf("Jmax= %d\n",Jmax);

//Normalize and print q[j]//
sumq=1+sumq;
for (i=0;i<Jmax+1;i++){
qnorm[i]=q[i]/sumq;
printf("q'[%d]= %f\n",i,qnorm[i]);printf("q[%d]= %f\n",i,q[i]);
}
//Print Pblock[t]//
for (t=1;t<T+1;t++){
for (i=0;i<Jmax+1;i++){
Pblock[t]=Pblock[t]+B[i][t]*qnorm[i];
}
printf("Pblock[%d]= %f\n",t,Pblock[t]);
}
scanf("\n");
}
``````

So let's have a look here...

`int T,i,j,t,c,r[T],Jmax,Z,k;` So j is an int, size undefined here (so it could be any value at all) at this point. I see you also make an array, `r`, which could be any size at all and there's no way to know.

`float L[j+1][j],B[T+1][j+1],b[T+1][1000],P[T+1][j+1],q[j+1],qnorm[j+1];` Now you start using j to create arrays, even though j could be any size at all, so the arrays you create could be any size at all.

You've got arrays of unknown size in your code that you then use, with no idea of what size they are. I'm not surprised it goes wrong in ways that are difficult to understand.

If you're going to make an array, you really should know in advance what size it's going to be.

The first thing I would do is name your variables something descriptive. I'm not sure of the rest of the people on this site but if your code isnt somewhat easy to read I'm not going to make the effort.

A few things to take note of:
* First off, this code appears to be C, not C++. Perhaps one of the moderators can move it over to the correct forum?
* There are two back-quote characters on line 13 that don't belong there.
* Variable-length arrays have to have to take a variable that is initialized before the array is declared (the main purpose of VLAs is to allow you to have array sizes set by a function parameter). Thus, you need to have initializers for `T` and `j`, before they are used. In any case, there is no reason to have some of these as VLAs, since the value of `T` is constant. As for the ones declared with `j`, well, that simply isn't going to work as you seem to expect.
* Arrays in C are zero-indexed, not one-indexed, and you seem to be going out of your way to treat them as one-indexed. It would be easier simply to use them as zero-indexed and be done with it.