Correct order of FOR loops, given order of data in external files

The dimensioned variable HOMEPAT is a stock or inventory variable in a system dynamics simulation model. Initial values of this stock are read into the model at HOMPTNA, by a MS C subprogram PARAMID2B.

HOMEPAT[MDSV,MPNT]=Integ((HOMEIN1[MDSV,MPNT]+HOMEIN2[MDSV,MPNT]+
HOMEIN3[MDSV,MPNT]+HOMEIN4[MDSV,MPNT]-HOMEOUT[MDSV,MPNT])/TIME_STEP,
HOMPTNA[MDSV,MPNT])~ p ~| [MDSV=6; MPNT=20]
HOMPTNA[MDSV,MPNT]=INITIAL(PARAMID2B(HOMPTN[MDS1,MPN1],ONDTM78A[MDS1,
MPN1],OSTTM78A[MDS1,MPN1],XNDTM78A[MDS1,MPN1],XSTTM78A[MDS1,MPN1],
RUNOP1[O790],ILONOF[O790],RUNNO,Time,790,255,356,582,684,MEDSA,MLPTA,
NTABN1,NTABN2,OLISTA,NTABNA,1,PNDST))~ p ~|

The external data file for initial values of the stock variable (HOMPTNA) is:

1 0 1 1 0 0 [all MDSV for MPNT=0]
0 1 1 0 0 0 [all MDSV for MPNT=1]
0 0 0 1 0 1
1 0 1 0 1 0
0 0 1 0 0 0
0 1 0 1 1 0
0 0 0 0 1 1
0 1 0 1 0 0
1 0 1 0 0 1
0 0 1 0 1 0
0 1 0 1 0 1
1 0 0 0 1 0
0 1 0 1 0 0
1 0 0 1 0 0
0 0 1 0 0 1
1 0 0 1 1 0
0 1 1 0 0 0
1 0 0 1 0 0
0 0 0 0 1 1
1 0 1 1 0 0 [all MDSV for MPNT=19]

All data files for entities of 2 or more dimensions follow this order. There are many instances of
reading from, and writing to, such data files in this and other models. The issue is: are those
instances correctly programmed in the subprograms concerned?

Listed below are examples of the programming. For each example. please indicate whether the
programming is consistent with the data order in the above file. If not, please correct the code.
________________________________________________________________________
Read into model from paramid2b subprogram data for a parameter or initial value of a stock

for( ndm2 ...
for( ndm1 ...

if( EQUAL(type, ONE)){ // type=integer
paramid2b_v = (REAL)entout2[ndm1 - 1][ndm2 - 1];
} // read into model in same order as stock array dimensions

_______________________________________________________________________
Correction?

_______________________________________________________________________
Read into model from lopenwf.c subprogram data for a parameter or initial value of a stock

for( ndm2 ...
for( ndm1 ...

nnb = (long int)(ndn1*ndn2);
for( nn1 = 0; nn1 < nnb; ++nn1 ){
refi = fscanf_s( file1, " %hd \n", &nhold[nn1]);
assert(refi != EOF);
}
name[nn1][nn2] = (REAL)nhold[nnb]; // model entity is float or REAL
openwf_v = name[ndm1 - 1][ndm2 - 1];

_______________________________________________________________________
Correction?

_______________________________________________________________________Read data from rdarr.c subprogram
narray[ndn1,ndn2, ,,,]

refi = fopen_s(&file1, filnam, "r" );
while(inLimit(ndim, pointer, dim)){
offset = getOffset(ndim, pointer, dim);
if( type == 3){ // long integer
fscanf( file1, "%ld \n", ((long*)narray + offset));
}
else if( type == 2){ // float
fscanf( file1, "%lf \n", ((float*)narray + offset));
}
else{ // integer
fscanf( file1, "%hd \n", ((short*)narray + offset));
}
pointFortranNext(ndim, pointer, acdim);
}
reti = fclose(file1);
assert(reti != EOF);

_______________________________________________________________________
Correction?

_______________________________________________________________________
Write to file

for( nn2 = 0; nn2 < dim2; ++nn2 ){
for( nn1 = 0; nn1 < dim1; ++nn1 ){
refi = fscanf_s( file1, " %hd \n", &buf);
assert(refi != EOF);
narray[ELEM2(nn1, nn2)] = (short)buf;
sprintf_s(err_buff, 512, "RDIARR2 ERROR %s", filnam);
}
}
reti = fclose(file1);
assert(reti != EOF);

______________________________________________________________________
Correction?

_______________________________________________________________________
for( nn2 = 0; nn2 < dim2; ++nn2 ){
for( nn1 = 0; nn1 < dim1; ++nn1 ){
holdl[nnm] = narray[ELEM2(nn1, nn2)];

______________________________________________________________________
Correction?

_______________________________________________________________________
Ptlistx

for( nn1 = 0; nn1 < meds; ++nn1 ){ // OPTX1[MDSV,HPNT] in the model
for( nn2 = 0; nn2 < hlpt; ++nn2 ){
refi = fscanf_s( file1, " %hd \n", &buf);
assert(refi != EOF);
optx[nn2][nn1] = (short)buf;
}
}

_____________________________________________________________________
Correction?

_______________________________________________________________________
for( nn2 = 0; nn2 < hlpt; ++nn2 ){ // PTNSTAT[HPIF,HPNT] in the model
for( nn1 = 0; nn1 < PTI; ++nn1 ){
refi = fscanf_s( file1, " %hd \n", &buf);
assert(refi != EOF);
ptnstat[nn1][nn2] = (short)buf;
}
}

____________________________________________________________________
Correction?

_______________________________________________________________________
Wtiarr2

for( nn2 = 0; nn2 < dim2; ++nn2 ){
for( nn1 = 0; nn1 < dim1; ++nn1 ){
hold[nnm] = narray[nn1, nn2];
++nnm;
}
}
fprintf( file1, "%12d %11d %11d %11d %11d %11d \n",
hold[0], hold[1], hold[2], hold[3], hold[4], hold[5] );

____________________________________________________________________
Correction?

_______________________________________________________________________
Ranab1

for( nn3 = 0; nn3 < dept; ++nn3 ){
for( nn2 = 0; nn2 < trad; ++nn2 ){
for( nn1 = 0; nn1 < scat; ++nn1 ){
offset = dept*trad*nn1 + dept*nn2 + nn3;
slvstf[nn1][nn2][nn3] = (REAL)( *(SLEAVS + offset));
} // SLEAVS[SFCT,PTRD,BSNS]
} // SCAT,TRAD,DEPT
}

______________________________________________________________________
Correction?

_______________________________________________________________________
for( nn2 = 0; nn2 < staf; ++nn2 ){
for( nn1 = 0; nn1 < STI; ++nn1 ){
refi = fscanf_s( file1, " %hd \n", &buf);
assert(refi != EOF);
stfstat[nn1][nn2] = (short)buf;
} // STFSTAT[STFI,STFF]
} // STI, STAF

_____________________________________________________________________
Correction?

_______________________________________________________________________
and your question is?
Correction?

Cut'n'pasting a ton of garbage unformatted code that is seriously incomplete and expecting others to clean up the mess is rather annoying and foolish.
The OP has a history of posting often incomplete or incomprehensible questions, and failing to reply when asked for clarifications.
The OP just PMed me and whined that he didn't like my feedback. Tough noogies.
Topic archived. No new replies allowed.